Skip to content

Commit

Permalink
Pull and improve modmail-dev#3333
Browse files Browse the repository at this point in the history
  • Loading branch information
raidensakura committed Mar 31, 2024
1 parent 248de1c commit 0d1e460
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from textwrap import indent
from typing import Union

import aiohttp
import discord
from aiohttp import ClientResponseError
from discord.enums import ActivityType, Status
Expand All @@ -25,7 +26,7 @@
from core.changelog import Changelog
from core.models import HostingMethod, InvalidConfigError, PermissionLevel, UnseenFormatter, getLogger
from core.paginator import EmbedPaginatorSession, MessagePaginatorSession
from core.utils import DummyParam, trigger_typing, truncate
from core.utils import DummyParam, is_image_url, trigger_typing, truncate

logger = getLogger(__name__)

Expand Down Expand Up @@ -2191,6 +2192,55 @@ def paginate(text: str):

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

@commands.command(name="avatar")
@commands.cooldown(3, 10, commands.BucketType.default)
@checks.has_permissions(PermissionLevel.OWNER)
@trigger_typing
async def avatar(self, ctx: commands.Context, url: Union[str, None]):
"""
Updates the bot's avatar within discord.
"""
if not ctx.message.attachments and (url is None or not is_image_url(url)):
embed = discord.Embed(
title="Error",
description="You need to upload or link a valid 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()

embed = None
if dc_avatar:
try:
await self.bot.user.edit(avatar=dc_avatar)
logger.info("Bot Avatar updated.")
embed = discord.Embed(
title="Successfully updated",
description="Successfully updated avatar.",
color=self.bot.main_color,
)
except Exception as e:
logger.error(f"Uploading the avatar to discord failed: {e}")
embed = discord.Embed(
title="Error",
description=f"Could not upload avatar to Discord: {e}.",
color=self.bot.error_color,
)
await ctx.send(embed=embed)
else:
embed = discord.Embed(
title="Error",
description="Could not fetch an image file from given URL.",
color=self.bot.error_color,
)
await ctx.send(embed=embed)


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

0 comments on commit 0d1e460

Please sign in to comment.