async def send_ban_confirmation(self, ctx, user): content = discord.Embed(title=":hammer: Ban user?", color=int("f4900c", 16)) content.description = f"{user.mention}\n**{user.name}#{user.discriminator}**\n{user.id}" msg = await ctx.send(embed=content) async def confirm_ban(): try: await ctx.guild.ban(user, delete_message_days=0) content.title = ":white_check_mark: Banned user" except discord.errors.Forbidden: content.title = discord.Embed.Empty content.description = f":no_entry: It seems I don't have the permission to ban **{user}** {user.mention}" content.colour = int("be1931", 16) await msg.edit(embed=content) async def cancel_ban(): content.title = ":x: Ban cancelled" await msg.edit(embed=content) functions = {"✅": confirm_ban, "❌": cancel_ban} asyncio.ensure_future( util.reaction_buttons(ctx, msg, functions, only_author=True, single_use=True))
async def typing_clear(self, ctx: commands.Context): """Clear your typing data""" content = discord.Embed(title=":warning: Are you sure?", color=int("ffcc4d", 16)) content.description = ( "This action will delete *all* of your saved typing data and is **irreversible**." ) msg = await ctx.send(embed=content) async def confirm(): await self.bot.db.execute( "DELETE FROM typing_stats WHERE user_id = %s", ctx.author.id) await self.bot.db.execute( "DELETE FROM typing_race WHERE user_id = %s", ctx.author.id) content.title = ":white_check_mark: Cleared your data" content.color = int("77b255", 16) content.description = "" await msg.edit(embed=content) async def cancel(): content.title = ":x: Action cancelled" content.description = "" content.color = int("dd2e44", 16) await msg.edit(embed=content) functions = {"✅": confirm, "❌": cancel} asyncio.ensure_future( util.reaction_buttons(ctx, msg, functions, only_author=True, single_use=True))
async def divorce(self, ctx): """End your marriage.""" partner = "" to_remove = [] for el in self.bot.cache.marriages: if ctx.author.id in el: to_remove.append(el) pair = list(el) if ctx.author.id == pair[0]: partner = ctx.guild.get_member( pair[1]) or self.bot.get_user(pair[1]) else: partner = ctx.guild.get_member( pair[0]) or self.bot.get_user(pair[0]) if partner == "": return await ctx.send(":thinking: You are not married!") content = discord.Embed( description= f":broken_heart: Divorce **{util.displayname(partner)}**?", color=int("dd2e44", 16), ) msg = await ctx.send(embed=content) async def confirm(): for x in to_remove: self.bot.cache.marriages.remove(x) await self.bot.db.execute( "DELETE FROM marriage WHERE first_user_id = %s OR second_user_id = %s", ctx.author.id, ctx.author.id, ) await ctx.send(embed=discord.Embed( color=int("ffcc4d", 16), description= f":pensive: You and **{util.displayname(partner)}** are now divorced...", )) async def cancel(): pass functions = {"✅": confirm, "❌": cancel} asyncio.ensure_future( util.reaction_buttons(ctx, msg, functions, only_author=True, single_use=True))
async def gfycat(self, ctx, *, query): """Search for a random gif.""" scripts = [] async with aiohttp.ClientSession() as session: tasks = [] if len(query.split(" ")) == 1: tasks.append( extract_scripts(session, f"https://gfycat.com/gifs/tag/{query}")) tasks.append( extract_scripts(session, f"https://gfycat.com/gifs/search/{query}")) scripts = sum(await asyncio.gather(*tasks), []) urls = [] for script in scripts: try: data = json.loads(script.contents[0]) for x in data["itemListElement"]: if "url" in x: urls.append(x["url"]) except json.JSONDecodeError: continue if not urls: return await ctx.send("Found nothing!") msg = await ctx.send(f"**{query}** {random.choice(urls)}") async def randomize(): await msg.edit(content=f"**{query}** {random.choice(urls)}") async def done(): return True buttons = {"❌": msg.delete, "🔁": randomize, "🔒": done} asyncio.ensure_future( util.reaction_buttons(ctx, msg, buttons, only_author=True))
async def database_query(self, ctx, *, statement): """Execute something against the local MariaDB instance.""" data = await self.bot.db.execute(statement) try: if data: content = "\n".join(str(r) for r in data) await ctx.send(f"```py\n{content}\n```") else: await ctx.send(":white_check_mark:") except discord.errors.HTTPException: # too long, page it pages = [] this_page = "```py\n" for row in data: if len(this_page + str(row)) < 1993: this_page += "\n" + str(row) else: this_page += "\n```" pages.append(this_page) this_page = "```py\n" page_iterator = util.TwoWayIterator(pages) msg = await ctx.send(page_iterator.current()) async def switch_page(new_page): await msg.edit(content=new_page) async def previous_page(): content = page_iterator.previous() if content is not None: await switch_page(content) async def next_page(): content = page_iterator.next() if content is not None: await switch_page(content) functions = {"⬅": previous_page, "➡": next_page} asyncio.ensure_future(util.reaction_buttons(ctx, msg, functions))
async def command_clear(self, ctx: commands.Context): """Delete all custom commands on this server""" count = (await self.bot.db.execute( "SELECT COUNT(*) FROM custom_command WHERE guild_id = %s", ctx.guild.id, one_value=True, ) or 0) if count < 1: raise exceptions.CommandWarning( "This server has no custom commands yet!") content = discord.Embed(title=":warning: Are you sure?", color=int("ffcc4d", 16)) content.description = f"This action will delete all **{count}** custom commands on this server and is **irreversible**." msg = await ctx.send(embed=content) async def confirm(): await self.bot.db.execute( "DELETE FROM custom_command WHERE guild_id = %s", ctx.guild.id, ) content.title = f":white_check_mark: Cleared commands in {ctx.guild}" content.description = "" content.color = int("77b255", 16) await msg.edit(embed=content) async def cancel(): content.title = ":x: Action cancelled" content.description = "" content.color = int("dd2e44", 16) await msg.edit(embed=content) functions = {"✅": confirm, "�": cancel} asyncio.ensure_future( util.reaction_buttons(ctx, msg, functions, only_author=True, single_use=True))