Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin] add a simple [p]editrole icon command #6177

Open
wants to merge 3 commits into
base: V3/develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 60 additions & 0 deletions redbot/cogs/admin/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import aiohttp
import logging
from typing import Tuple, Union

Expand Down Expand Up @@ -324,6 +325,65 @@ async def edit_role_name(self, ctx: commands.Context, role: discord.Role, name:
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
Comment on lines +367 to +374
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should follow similar logic as [p]set bot avatar follows for processing the str link input, namely filtering out <link> syntax and being slightly more verbose as to what exception the get raised.

elif url is not None:
if url.startswith("<") and url.endswith(">"):
url = url[1:-1]
async with aiohttp.ClientSession() as session:
try:
async with session.get(url) as r:
data = await r.read()
except aiohttp.InvalidURL:
return await ctx.send(_("That URL is invalid."))
except aiohttp.ClientError:
return await ctx.send(_("Something went wrong while trying to get the image."))

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()
async def announce(self, ctx: commands.Context, *, message: str):
Expand Down