Skip to content

Commit 0d1e460

Browse files
committed
Pull and improve modmail-dev#3333
1 parent 248de1c commit 0d1e460

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

cogs/utility.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from textwrap import indent
1515
from typing import Union
1616

17+
import aiohttp
1718
import discord
1819
from aiohttp import ClientResponseError
1920
from discord.enums import ActivityType, Status
@@ -25,7 +26,7 @@
2526
from core.changelog import Changelog
2627
from core.models import HostingMethod, InvalidConfigError, PermissionLevel, UnseenFormatter, getLogger
2728
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
2930

3031
logger = getLogger(__name__)
3132

@@ -2191,6 +2192,55 @@ def paginate(text: str):
21912192

21922193
await self.bot.add_reaction(ctx.message, "\u2705")
21932194

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+
21942244

21952245
async def setup(bot):
21962246
await bot.add_cog(Utility(bot))

0 commit comments

Comments
 (0)