From a58bcddcacf7cddb314fccb1b13c878605005855 Mon Sep 17 00:00:00 2001 From: Lemon Rose Date: Fri, 26 May 2023 01:05:03 +0530 Subject: [PATCH 1/2] [Admin] add a simple `[p]role icon` command --- redbot/cogs/admin/admin.py | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/redbot/cogs/admin/admin.py b/redbot/cogs/admin/admin.py index 73e2e5f8da7..fd15434ca1e 100644 --- a/redbot/cogs/admin/admin.py +++ b/redbot/cogs/admin/admin.py @@ -1,4 +1,5 @@ import asyncio +import aiohttp import logging from typing import Tuple, Union @@ -323,6 +324,69 @@ async def edit_role_name(self, ctx: commands.Context, role: discord.Role, name: else: log.info(reason) await ctx.send(_("Done.")) + + @editrole.command(name="icon") + async def edit_role_icon( + self, + ctx: commands.Context, + role: discord.Role, + icon: Union[discord.Emoji, discord.PartialEmoji, str], + ): + """ + Edit a role's icon. + + Use double quotes if the role contains spaces. + + Example: + `[p]editrole icon "The Transistor" <:red:230319279424143360>` + `[p]editrole icon "The Transistor" https://cdn.discordapp.com/emojis/230319279424143360.webp` + """ + author = ctx.message.author + reason = "{}({}) changed the icon of role '{}'".format( + author.name, author.id, role.name + ) + + if "ROLE_ICONS" not in ctx.guild.features: + await ctx.send(_("This server does not have support for the role icons feature.")) + return + + if not self.pass_user_hierarchy_check(ctx, role): + await ctx.send(_(ROLE_USER_HIERARCHY_ISSUE).format(role=role)) + return + if not self.pass_hierarchy_check(ctx, role): + await ctx.send(_(ROLE_HIERARCHY_ISSUE).format(role=role)) + return + if not ctx.guild.me.guild_permissions.manage_roles: + await ctx.send(_(NEED_MANAGE_ROLES)) + return + + if ctx.message.attachments: + image = await ctx.message.attachments[0].read() + elif isinstance(icon, discord.Emoji): + image = await icon.read() + elif isinstance(icon, discord.PartialEmoji) and icon.is_custom_emoji(): + image = await icon.read() + elif isinstance(icon, str) and icon.startswith("http"): + try: + async with aiohttp.ClientSession() as session: + async with session.get(url=icon, raise_for_status=True) as response: + image = await response.read() + except aiohttp.ClientResponseError as error: + await ctx.send(_("Invalid url provided: {}").format(error.message)) + return + else: + await ctx.send( + _(f"Please provide a valid emoji or link for the role icon.") + ) + return + + try: + await role.edit(reason=reason, display_icon=image) + except discord.Forbidden: + await ctx.send(_(GENERIC_FORBIDDEN)) + else: + log.info(reason) + await ctx.send(_("Done.")) @commands.group(invoke_without_command=True) @commands.is_owner() From f5b967e8116bc1a53dd9618f211ad42e9b5dfb2f Mon Sep 17 00:00:00 2001 From: Lemon Rose Date: Fri, 26 May 2023 01:10:49 +0530 Subject: [PATCH 2/2] [Black] style formatting --- redbot/cogs/admin/admin.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/redbot/cogs/admin/admin.py b/redbot/cogs/admin/admin.py index fd15434ca1e..efc4aaa6cb3 100644 --- a/redbot/cogs/admin/admin.py +++ b/redbot/cogs/admin/admin.py @@ -324,7 +324,7 @@ async def edit_role_name(self, ctx: commands.Context, role: discord.Role, name: else: log.info(reason) await ctx.send(_("Done.")) - + @editrole.command(name="icon") async def edit_role_icon( self, @@ -334,22 +334,20 @@ async def edit_role_icon( ): """ Edit a role's icon. - + Use double quotes if the role contains spaces. - + Example: `[p]editrole icon "The Transistor" <:red:230319279424143360>` `[p]editrole icon "The Transistor" https://cdn.discordapp.com/emojis/230319279424143360.webp` """ author = ctx.message.author - reason = "{}({}) changed the icon of role '{}'".format( - author.name, author.id, role.name - ) - + reason = "{}({}) changed the icon of role '{}'".format(author.name, author.id, role.name) + if "ROLE_ICONS" not in ctx.guild.features: await ctx.send(_("This server does not have support for the role icons feature.")) return - + if not self.pass_user_hierarchy_check(ctx, role): await ctx.send(_(ROLE_USER_HIERARCHY_ISSUE).format(role=role)) return @@ -359,7 +357,7 @@ async def edit_role_icon( if not ctx.guild.me.guild_permissions.manage_roles: await ctx.send(_(NEED_MANAGE_ROLES)) return - + if ctx.message.attachments: image = await ctx.message.attachments[0].read() elif isinstance(icon, discord.Emoji): @@ -375,11 +373,9 @@ async def edit_role_icon( await ctx.send(_("Invalid url provided: {}").format(error.message)) return else: - await ctx.send( - _(f"Please provide a valid emoji or link for the role icon.") - ) + await ctx.send(_(f"Please provide a valid emoji or link for the role icon.")) return - + try: await role.edit(reason=reason, display_icon=image) except discord.Forbidden: