From 5ecc6eb9af075a836dae2635f29ce19ea4ae9d57 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 20 Feb 2024 23:24:27 +0100 Subject: [PATCH 1/3] Avatar Command This PR implements a command for owners to update their bot avatar directly in discord. Co-Authored-By: Cyrus Yip <54488650+RealCyGuy@users.noreply.github.com> --- cogs/utility.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/cogs/utility.py b/cogs/utility.py index b892ffc01b..79aa8d846a 100644 --- a/cogs/utility.py +++ b/cogs/utility.py @@ -1,4 +1,5 @@ import asyncio +import aiohttp import inspect import os import random @@ -2120,6 +2121,34 @@ def paginate(text: str): await self.bot.add_reaction(ctx.message, "\u2705") + @commands.command(name="avatar") + @checks.has_permissions(PermissionLevel.OWNER) + async def avatar(self, ctx: commands.Context, url: str = None): + """ + Updates the bots avatar within discord. + """ + if not ctx.message.attachments and url is None: + embed = discord.Embed(title='Error', description='You need to upload or link a image file.', color=self.bot.error_color) + return await ctx.send(embed=embed) + dc_avatar = None + if ctx.message.attachments: + dc_avatar = await ctx.message.attachments[0].read() + elif url: + async with aiohttp.ClientSession() as session: + async with session.get(url) as resp: + dc_avatar = await resp.read() + + if dc_avatar is None: + embed = discord.Embed(title='Error', description='Reading the attachment failed, is it valid?', color=self.bot.error_color) + return await ctx.send(embed=embed) + try: + await self.bot.user.edit(avatar=dc_avatar) + logger.info('Bot Avatar updated.') + except Exception: + raise ValueError('Uploading the avatar to discord failed.') + embed = discord.Embed(title='Successfully updated', description='Successfully updated avatar.', color=self.bot.main_color) + await ctx.send(embed=embed) + async def setup(bot): await bot.add_cog(Utility(bot)) From 8e304e9edcc446d9fcbdb8f1d28bbba48946123b Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 21 Feb 2024 00:05:27 +0100 Subject: [PATCH 2/3] fix Avatar Command CL/CD errors CL/CD errors fix Co-Authored-By: Cyrus Yip <54488650+RealCyGuy@users.noreply.github.com> --- cogs/utility.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/cogs/utility.py b/cogs/utility.py index 79aa8d846a..66265bcf15 100644 --- a/cogs/utility.py +++ b/cogs/utility.py @@ -2128,7 +2128,11 @@ async def avatar(self, ctx: commands.Context, url: str = None): Updates the bots avatar within discord. """ if not ctx.message.attachments and url is None: - embed = discord.Embed(title='Error', description='You need to upload or link a image file.', color=self.bot.error_color) + embed = discord.Embed( + title="Error", + description="You need to upload or link a image file.", + color=self.bot.error_color + ) return await ctx.send(embed=embed) dc_avatar = None if ctx.message.attachments: @@ -2139,14 +2143,23 @@ async def avatar(self, ctx: commands.Context, url: str = None): dc_avatar = await resp.read() if dc_avatar is None: - embed = discord.Embed(title='Error', description='Reading the attachment failed, is it valid?', color=self.bot.error_color) + embed = discord.Embed( + title="Error", + description="Reading the attachment failed, is it valid?", + color=self.bot.error_color + ) return await ctx.send(embed=embed) try: await self.bot.user.edit(avatar=dc_avatar) - logger.info('Bot Avatar updated.') + logger.info("Bot Avatar updated.") except Exception: - raise ValueError('Uploading the avatar to discord failed.') - embed = discord.Embed(title='Successfully updated', description='Successfully updated avatar.', color=self.bot.main_color) + raise ValueError("Uploading the avatar to discord failed.") + + embed = discord.Embed( + title="Successfully updated", + description="Successfully updated avatar.", + color=self.bot.main_color + ) await ctx.send(embed=embed) From 7cde38da6495432ef480f44a017e19e0f3adede5 Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 21 Feb 2024 00:10:07 +0100 Subject: [PATCH 3/3] fix Avatar command CL/CD errors 2 Co-Authored-By: Cyrus Yip <54488650+RealCyGuy@users.noreply.github.com> --- cogs/utility.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/cogs/utility.py b/cogs/utility.py index 66265bcf15..f2e489e0c3 100644 --- a/cogs/utility.py +++ b/cogs/utility.py @@ -2129,10 +2129,10 @@ async def avatar(self, ctx: commands.Context, url: str = None): """ if not ctx.message.attachments and url is None: embed = discord.Embed( - title="Error", - description="You need to upload or link a image file.", - color=self.bot.error_color - ) + title="Error", + description="You need to upload or link a image file.", + color=self.bot.error_color, + ) return await ctx.send(embed=embed) dc_avatar = None if ctx.message.attachments: @@ -2141,25 +2141,25 @@ async def avatar(self, ctx: commands.Context, url: str = None): async with aiohttp.ClientSession() as session: async with session.get(url) as resp: dc_avatar = await resp.read() - + if dc_avatar is None: embed = discord.Embed( - title="Error", - description="Reading the attachment failed, is it valid?", - color=self.bot.error_color - ) + title="Error", + description="Reading the attachment failed, is it valid?", + color=self.bot.error_color, + ) return await ctx.send(embed=embed) try: await self.bot.user.edit(avatar=dc_avatar) logger.info("Bot Avatar updated.") except Exception: raise ValueError("Uploading the avatar to discord failed.") - + embed = discord.Embed( - title="Successfully updated", - description="Successfully updated avatar.", - color=self.bot.main_color - ) + title="Successfully updated", + description="Successfully updated avatar.", + color=self.bot.main_color, + ) await ctx.send(embed=embed)