mirror of
https://github.com/brandons209/Red-bot-Cogs.git
synced 2024-09-30 00:57:42 +13:00
58 lines
2.2 KiB
Python
58 lines
2.2 KiB
Python
import re
|
|
from typing import Union
|
|
|
|
import discord
|
|
from discord.ext.commands.converter import IDConverter
|
|
from discord.ext.commands.errors import BadArgument
|
|
from redbot.core import commands
|
|
from redbot.core.i18n import Translator
|
|
|
|
_ = Translator("Translate", __file__)
|
|
|
|
|
|
class ChannelUserRole(IDConverter):
|
|
"""
|
|
This will check to see if the provided argument is a channel, user, or role
|
|
|
|
Guidance code on how to do this from:
|
|
https://github.com/Rapptz/discord.py/blob/rewrite/discord/ext/commands/converter.py#L85
|
|
https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/redbot/cogs/mod/mod.py#L24
|
|
"""
|
|
|
|
async def convert(
|
|
self, ctx: commands.Context, argument: str
|
|
) -> Union[discord.TextChannel, discord.Role, discord.Member]:
|
|
guild = ctx.guild
|
|
result = None
|
|
id_match = self._get_id_match(argument)
|
|
channel_match = re.match(r"<#([0-9]+)>$", argument)
|
|
member_match = re.match(r"<@!?([0-9]+)>$", argument)
|
|
role_match = re.match(r"<@&([0-9]+)>$", argument)
|
|
for converter in ["channel", "role", "member"]:
|
|
if converter == "channel":
|
|
match = id_match or channel_match
|
|
if match:
|
|
channel_id = match.group(1)
|
|
result = guild.get_channel(int(channel_id))
|
|
else:
|
|
result = discord.utils.get(guild.text_channels, name=argument)
|
|
if converter == "member":
|
|
match = id_match or member_match
|
|
if match:
|
|
member_id = match.group(1)
|
|
result = guild.get_member(int(member_id))
|
|
else:
|
|
result = guild.get_member_named(argument)
|
|
if converter == "role":
|
|
match = id_match or role_match
|
|
if match:
|
|
role_id = match.group(1)
|
|
result = guild.get_role(int(role_id))
|
|
else:
|
|
result = discord.utils.get(guild._roles.values(), name=argument)
|
|
if result:
|
|
break
|
|
if not result:
|
|
msg = _("{arg} is not a valid channel, user or role.").format(arg=argument)
|
|
raise BadArgument(msg)
|
|
return result
|