|
14 | 14 | from textwrap import indent
|
15 | 15 | from typing import Union
|
16 | 16 |
|
| 17 | +import aiohttp |
17 | 18 | import discord
|
18 | 19 | from aiohttp import ClientResponseError
|
19 | 20 | from discord.enums import ActivityType, Status
|
|
25 | 26 | from core.changelog import Changelog
|
26 | 27 | from core.models import HostingMethod, InvalidConfigError, PermissionLevel, UnseenFormatter, getLogger
|
27 | 28 | from core.paginator import EmbedPaginatorSession, MessagePaginatorSession
|
28 |
| -from core.utils import DummyParam, trigger_typing, truncate |
| 29 | +from core.utils import DummyParam, is_image_url, trigger_typing, truncate |
29 | 30 |
|
30 | 31 | logger = getLogger(__name__)
|
31 | 32 |
|
@@ -2191,6 +2192,55 @@ def paginate(text: str):
|
2191 | 2192 |
|
2192 | 2193 | await self.bot.add_reaction(ctx.message, "\u2705")
|
2193 | 2194 |
|
| 2195 | + @commands.command(name="avatar") |
| 2196 | + @commands.cooldown(3, 10, commands.BucketType.default) |
| 2197 | + @checks.has_permissions(PermissionLevel.OWNER) |
| 2198 | + @trigger_typing |
| 2199 | + async def avatar(self, ctx: commands.Context, url: Union[str, None]): |
| 2200 | + """ |
| 2201 | + Updates the bot's avatar within discord. |
| 2202 | + """ |
| 2203 | + if not ctx.message.attachments and (url is None or not is_image_url(url)): |
| 2204 | + embed = discord.Embed( |
| 2205 | + title="Error", |
| 2206 | + description="You need to upload or link a valid image file.", |
| 2207 | + color=self.bot.error_color, |
| 2208 | + ) |
| 2209 | + return await ctx.send(embed=embed) |
| 2210 | + dc_avatar = None |
| 2211 | + if ctx.message.attachments: |
| 2212 | + dc_avatar = await ctx.message.attachments[0].read() |
| 2213 | + elif url: |
| 2214 | + async with aiohttp.ClientSession() as session: |
| 2215 | + async with session.get(url) as resp: |
| 2216 | + dc_avatar = await resp.read() |
| 2217 | + |
| 2218 | + embed = None |
| 2219 | + if dc_avatar: |
| 2220 | + try: |
| 2221 | + await self.bot.user.edit(avatar=dc_avatar) |
| 2222 | + logger.info("Bot Avatar updated.") |
| 2223 | + embed = discord.Embed( |
| 2224 | + title="Successfully updated", |
| 2225 | + description="Successfully updated avatar.", |
| 2226 | + color=self.bot.main_color, |
| 2227 | + ) |
| 2228 | + except Exception as e: |
| 2229 | + logger.error(f"Uploading the avatar to discord failed: {e}") |
| 2230 | + embed = discord.Embed( |
| 2231 | + title="Error", |
| 2232 | + description=f"Could not upload avatar to Discord: {e}.", |
| 2233 | + color=self.bot.error_color, |
| 2234 | + ) |
| 2235 | + await ctx.send(embed=embed) |
| 2236 | + else: |
| 2237 | + embed = discord.Embed( |
| 2238 | + title="Error", |
| 2239 | + description="Could not fetch an image file from given URL.", |
| 2240 | + color=self.bot.error_color, |
| 2241 | + ) |
| 2242 | + await ctx.send(embed=embed) |
| 2243 | + |
2194 | 2244 |
|
2195 | 2245 | async def setup(bot):
|
2196 | 2246 | await bot.add_cog(Utility(bot))
|
0 commit comments