async def startcount(self, ctx, channel: discord.TextChannel = None): if channel is None: embed = util.log_embed( "Please ping a channel you want to start counting game on", "failed") await ctx.send(embed=embed) return cursor = self.bot.conn.cursor() query = "UPDATE servers SET count_game_channel_id = %s WHERE server_id = %s" cursor.execute(query, (channel.id, ctx.guild.id)) self.bot.conn.commit() try: query_insert = "INSERT INTO count_game(server_id, count_game_channel_id, last_count_member_id, last_count_number, last_count_status, last_count_fee, total_fee, created_at, last_modified_at) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)" data_insert = (ctx.guild.id, channel.id, 1, 0, 'good', 300, 0, datetime.datetime.now(), datetime.datetime.now()) cursor.execute(query_insert, data_insert) except: self.bot.conn.commit() query_update = "UPDATE count_game SET count_game_channel_id = %s, last_count_member_id = 1, last_count_number = %s, last_count_status = %s, last_count_fee = 300, total_fee = 0, last_modified_at = current_timestamp WHERE server_id = %s" data_update = (channel.id, 0, "good", ctx.guild.id) cursor.execute(query_update, data_update) self.bot.conn.commit() log_embed = util.log_embed( f"Successfully started counting game on {channel.mention}", "success") await ctx.send(embed=log_embed) embed = discord.Embed( title="Apollo's Chain Counting Game", description="Alright! Let's start all over again!", color=discord.Color.teal()) embed.set_thumbnail(url="https://i.imgur.com/DMvoEus.png") await channel.send(embed=embed)
async def reload(ctx, extension): if ctx.author.id == 544155147572609024 or ctx.author.guild_permissions.administrator: client.unload_extension(f"cogs.{extension}") client.load_extension(f"cogs.{extension}") embed = util.log_embed(f"{extension} module has been reloaded!", "success") await ctx.send(embed=embed)
async def stopcount(self, ctx): cursor = self.bot.conn.cursor() query = "UPDATE servers SET count_game_channel_id = %s WHERE server_id = %s" cursor.execute(query, (1, ctx.guild.id)) self.bot.conn.commit() try: query_update = "UPDATE count_game SET count_game_channel_id = %s WHERE server_id = %s" data_update = (1, ctx.guild.id) cursor.execute(query_update, data_update) self.bot.conn.commit() self.bot.logger.info("Updated channel count (stop_count)") response = f"Counting game has been stopped" embed = util.log_embed(response, "success") except: response = f"You haven't set a count channel" embed = util.log_embed(response, "failed") await ctx.send(embed=embed)
async def resetcount(self, ctx, count_number: int = None, fee: int = None): if count_number is None or fee is None: embed = util.log_embed("Please input the count number and fee", "failed") await ctx.send(embed=embed) return cursor = self.bot.conn.cursor() try: query = "UPDATE count_game SET last_count_member_id = 1, last_count_number = %s, last_count_fee = %s, last_count_status = %s WHERE server_id = %s" cursor.execute(query, (count_number, fee, "good", ctx.guild.id)) self.bot.conn.commit() self.bot.logger.info("Updated channel count (reset_count)") response = f"Number and fee have been reset to {count_number} and {fee:,}" embed = util.log_embed(response, "success") except: response = f"You haven't set a count channel" embed = util.log_embed(response, "failed") await ctx.send(embed=embed)
async def addcoins(self, ctx, member: discord.Member = None, coins: int = None): if member is None or coins is None: embed = util.log_embed( "Please ping a player and input coins amount", "failed") await ctx.send(embed=embed) return cursor = self.bot.conn.cursor() query_add_coins = "UPDATE players SET coins = coins + %s, last_modified_at = current_timestamp where player_id = %s" data_add_coins = (coins, member.id) cursor.execute(query_add_coins, data_add_coins) self.bot.conn.commit() await ctx.message.add_reaction("✅") response = f"{ctx.author.mention} has added {coins:,} coins to {member.mention}" embed = util.log_embed(response, "success") await ctx.send(embed=embed)
async def setup(self, ctx): guild = ctx.guild db_servers = util.get_db("db_servers.json") referral_log_channel = await guild.create_text_channel( name="referral-log") db_servers.update({"referral_channel_id": referral_log_channel.id}, Query().server_id == guild.id) response = f"Referral log channel has been set! ({referral_log_channel.mention})" response = util.log_embed(response, "success") await ctx.send(embed=response)
async def updaterank(self, ctx): db_referrals = util.get_db('db_referrals.json') db_users = util.get_db('db_users.json') table_referral = db_referrals.table(str(ctx.guild.id)) table_users = db_users.table(str(ctx.guild.id)) for row in table_users.all(): member_id = row['member_id'] try: member = await ctx.guild.fetch_member(member_id) except discord.errors.NotFound: table_users.remove(Query().member_id == member_id) table_referral.remove((Query().member_id == member_id) & (Query().referrer_id == member_id)) self.bot.logger.info( f"Removed data for member ID: {member_id}") continue referral_count = len( table_referral.search(Query().member_id == member_id)) ranks_ascending = sorted(RANKS, key=lambda k: k['limit'], reverse=True) for rank in ranks_ascending: if referral_count >= rank['limit']: referral_rank = rank['name'] break table_users.update( { "member_name": str(member), "referral_count": referral_count, "referral_rank": referral_rank }, Query().member_id == member_id) role_name = referral_rank other_roles = [rank['name'] for rank in RANKS if rank != role_name] for other_role_name in other_roles: other_role = discord.utils.get(member.roles, name=other_role_name) if other_role is not None: await member.remove_roles(other_role) role = discord.utils.get(ctx.guild.roles, name=role_name) if role is not None: await member.add_roles(role) response = f"Successfully updated {len(table_referral)} referrals row and {len(table_users)} users row" self.bot.logger.info(f"{ctx.author} has {response}") embed = util.log_embed(response, "success") await ctx.send(embed=embed)
async def stop(self, ctx, task_name: str = None): if ctx.author.guild_permissions.administrator: task_list = ["freecoins"] if task_name is None: return if task_name in task_list: if task_name == "freecoins": self.free_coins.stop() embed = util.log_embed(f"{task_name} task has been stopped", "success") await ctx.send(embed=embed) else: await ctx.send(f"There's only {' and '.join(task_list)} task") await ctx.message.delete()
async def donate(self, ctx, member: discord.Member = None, donation_amount: int = None): donater = ctx.author if member is None or donation_amount is None: response = f"Sorry, you need to ping the recipient and type the donation amount" embed = util.log_embed(response, "failed") await ctx.send(embed=embed) return if donation_amount < 0: await ctx.message.add_reaction("❌") response = f"Sorry, {ctx.author.mention}. Please input correct amount of coins" cursor = self.bot.conn.cursor() query = "SELECT coins FROM players WHERE player_id = %s" data = (donater.id, ) cursor.execute(query, data) result = cursor.fetchall() player_coin = [e[0] for e in result] player_coin = player_coin[0] query = "SELECT next_donation_time FROM players WHERE player_id = %s" data = (member.id, ) cursor.execute(query, data) result = cursor.fetchall() if donation_amount > player_coin: await ctx.message.add_reaction("❌") response = f"Sorry {donater.mention}. Looks like you don't have that much coins" return response query_donater = "UPDATE players SET coins = coins - %s, last_modified_at = %s where player_id = %s" data_donater = (donation_amount, datetime.datetime.now(), donater.id) cursor.execute(query_donater, data_donater) query_donate_to = "UPDATE players SET coins = coins + %s, last_modified_at = %s where player_id = %s" data_donate_to = (donation_amount, datetime.datetime.now(), member.id) cursor.execute(query_donate_to, data_donate_to) self.bot.conn.commit() await ctx.message.add_reaction("✅") response = f"{donater.mention} successfully donated {donation_amount:,} coins to {member.mention}" await ctx.send(response)
async def setup(self, ctx): guild = ctx.guild cursor = self.bot.conn.cursor() query = "SELECT * FROM servers WHERE server_id = %s" cursor.execute(query, (guild.id, )) result = cursor.fetchall() if not result: return new_category = await guild.create_category(name="Minigames") await new_category.set_permissions(guild.default_role, read_messages=True) count_game_channel = await new_category.create_text_channel( name="count-game") free_coins_channel = await new_category.create_text_channel( name="free-coins") query = "UPDATE servers SET count_game_channel_id = %s, free_coins_channel_id = %s WHERE server_id = %s" data = (count_game_channel.id, free_coins_channel.id, guild.id) cursor.execute(query, data) self.bot.conn.commit() query_insert = "INSERT INTO count_game(server_id, count_game_channel_id, last_count_member_id, last_count_number, last_count_status, last_count_fee, total_fee, created_at, last_modified_at) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s)" data_insert = (guild.id, count_game_channel.id, 1, 0, 'good', 300, 0, datetime.datetime.now(), datetime.datetime.now()) cursor.execute(query_insert, data_insert) self.bot.conn.commit() embed = discord.Embed( title="Count Game", description="Let the count game begin! Starts from 1", color=discord.Color.teal()) embed.set_thumbnail(url="https://i.imgur.com/DMvoEus.png") await count_game_channel.send(embed=embed) response = f"{count_game_channel.mention} and {free_coins_channel.mention} have been created!" response = util.log_embed(response, "success") await ctx.send(embed=response)
async def info(self, ctx): db_users = util.get_db('db_users.json') table_user = db_users.table(str(ctx.guild.id)) users_data = table_user.search(Query().member_id == ctx.author.id) if len(users_data) == 0: response = f"Looks like {ctx.author.mention} doesn't have any refferals so far. Got any friends you can bring over?" embed = util.log_embed(response, "failed") await ctx.send(embed=embed) return user_row = users_data[0] embed = discord.Embed( title="Referral Info", color=discord.Color.blue() ) embed.set_thumbnail(url=ctx.author.avatar_url) embed.add_field(name="Member Name", value=str(ctx.author), inline=False) embed.add_field(name="Referral Count", value=user_row['referral_count'], inline=False) embed.add_field(name="Referral Rank", value=user_row['referral_rank'], inline=False) await ctx.send(embed=embed)
async def unrefer(self, ctx): db_referrals = util.get_db('db_referrals.json') db_users = util.get_db('db_users.json') table_referral = db_referrals.table(str(ctx.guild.id)) table_users = db_users.table(str(ctx.guild.id)) referrals_data = table_referral.search(Query().referrer_id == ctx.author.id) if len(referrals_data) == 0: return referral_row = referrals_data[0] member = await ctx.guild.fetch_member(referral_row['member_id']) if member is None: return table_referral.remove(Query().referrer_id == ctx.author.id) referral_count = len(table_referral.search(Query().member_id == member.id)) table_users.update({"member_name":str(member), "referral_count":referral_count}, Query().member_id == member.id) ranks_ascending = sorted(RANKS, key=lambda k: k['limit']) for idx, rank in enumerate(ranks_ascending): if idx + 1 == len(ranks_ascending): continue if referral_count >= rank['limit'] and referral_count < ranks_ascending[idx+1]['limit']: role_names = [row['name'] for row in RANKS if row['name'] != rank['name']] for role_name in role_names: role = discord.utils.get(member.roles, name=role_name) if role is not None: await member.remove_roles(role) table_users.update({"referral_rank":rank['name']}, Query().member_id == member.id) role = discord.utils.get(ctx.guild.roles, name=rank['name']) if role is not None: await member.add_roles(role) break response = f"{ctx.author} has unreferred {member}" embed = util.log_embed(response, "success") await ctx.send(embed=embed)
async def purchase(self, ctx, itemcode: str = None, quantity: int = 1): if itemcode is None: response = f"Sorry {ctx.author.mention}. You need to type an item code" embed = util.log_embed(response, "failed") await ctx.send(embed=embed) return conn = self.bot.conn member = ctx.author itemcode = itemcode.lower() ITEMS = util.read_json('items.json') ITEMS_SHOP = ITEMS['shop'] ITEM_CODE = [row['item_code'] for row in ITEMS_SHOP] shop_roles = { "role_1": 12313231323, # role id example } if itemcode not in ITEM_CODE and itemcode not in shop_roles: await ctx.message.add_reaction("❌") response = f"Item code not found. Please try again" return response item_dict = next(e for e in ITEMS_SHOP if e["item_code"] == itemcode) item_price = item_dict["item_price"] item_name = item_dict["item_name"] item_code = item_dict["item_code"] cursor = conn.cursor() query_coin = "SELECT coins FROM players WHERE player_id = %s" data_coin = (member.id, ) cursor.execute(query_coin, data_coin) result = cursor.fetchall() result_list = result[0] player_coin = result_list[0] if player_coin < item_price * quantity: response = f"Sorry {member.mention}. You don't seem to have that much coins to purchase this item" embed = util.log_embed(response, "failed") await ctx.send(embed=embed) return has_role = False if itemcode not in shop_roles: for _ in range(0, quantity): query_item = "INSERT INTO items (player_id,item_name,item_code,created_at,last_modified_at) VALUES (%s,%s,%s,%s,%s)" data_item = (member.id, item_name, item_code, datetime.datetime.now(), datetime.datetime.now()) query_coin = "UPDATE players SET coins = coins - %s, last_modified_at = %s WHERE player_id = %s" data_coin = (item_price, datetime.datetime.now(), member.id) cursor.execute(query_item, data_item) conn.commit() cursor.execute(query_coin, data_coin) conn.commit() elif itemcode in shop_roles: if discord.utils.get(member.roles, id=shop_roles[itemcode]) is not None: has_role = True else: query_coin = "UPDATE players SET coins = coins - %s, last_modified_at = %s WHERE player_id = %s" data_coin = (item_price, datetime.datetime.now(), member.id) cursor.execute(query_coin, data_coin) conn.commit() await member.add_roles( discord.utils.get(member.guild.roles, id=shop_roles[itemcode])) if has_role is False: response = f"{member.mention} has purchased `{item_name}`!" response = util.log_embed(response, "success") else: response = f"{member.mention}, you already have the role!" response = util.log_embed(response, "failed") await ctx.send(embed=response)
async def refer(self, ctx, member:discord.Member=None): if member is None: response = f"Well, {ctx.author.mention}, looks like you didn't mention a friend. Be sure to mention them!" embed = util.log_embed(response, "failed") await ctx.send(embed=embed) return db_referrals = util.get_db('db_referrals.json') db_users = util.get_db('db_users.json') db_servers = util.get_db('db_servers.json') servers_data = db_servers.search(Query().server_id == ctx.guild.id) if not servers_data: return server_row = servers_data[0] referral_channel_id = None if "referral_channel_id" not in server_row else server_row['referral_channel_id'] if referral_channel_id is None: return if ctx.channel.id != referral_channel_id: response = f"Sorry, you can only use this command in <#{referral_channel_id}>" embed = util.log_embed(response, "failed") await ctx.send(embed=embed) return referrer = ctx.author table_referral = db_referrals.table(str(ctx.guild.id)) table_users = db_users.table(str(ctx.guild.id)) referrals_data = table_referral.search(Query().referrer_id == referrer.id) users_data = table_users.search(Query().member_id == member.id) if (member.id == referrer.id): response = f"Well, {referrer.mention}, you cannot refere to yourself!" embed = util.log_embed(response, "failed") await ctx.send(embed=embed) return if len(referrals_data) == 1: response = f"{referrer.mention}, looks like you've already reffered someone. You can always unrefer someone by using `{ctx.prefix}unrefer`" embed = util.log_embed(response, "failed") await ctx.send(embed=embed) return new_referral = dict( member_id=member.id, referrer_id=referrer.id ) table_referral.insert(new_referral) referral_count = len(table_referral.search(Query().member_id == member.id)) if len(users_data) == 1: table_users.update({"member_name":str(member), "referral_count":referral_count}, Query().member_id == member.id) try: rank_name = next(rank['name'] for rank in RANKS if referral_count == rank['limit']) except StopIteration: rank_name = None if rank_name is not None: role_names = [row['name'] for row in RANKS if row['name'] != rank_name] for role_name in role_names: if discord.utils.get(member.roles, name=role_name) is not None: await member.remove_roles(discord.utils.get(member.roles, name=role_name)) table_users.update({"referral_rank":rank_name}, Query().member_id == member.id) role = discord.utils.get(ctx.guild.roles, name=rank_name) if role is not None: await member.add_roles(role) else: table_users.insert(dict(member_id=member.id, member_name=str(member), referral_count=1, referral_rank="Unranked")) self.bot.logger.info(f"Member {referrer} has referred to {member}") await ctx.message.add_reaction("✅")
async def reload(ctx, extension): client.unload_extension(f"cogs.{extension}") client.load_extension(f"cogs.{extension}") embed = util.log_embed(f"{extension} module has been reloaded!", "success") await ctx.send(embed=embed)