Converter¶
To convert string to actual discord.* class objects.
Example:
"nkpro" => <Member id=1234567890 name='nkpro' discriminator='3766' bot=False nick=None guild=<Guild id=0987654321 name='GUILD' shard_id=None chunked=True member_count=2>>discord.ext.commands.Bot do this convertions while calling the commands callback based on their annotations (callback.__annotations__).
We can do the same with discordRebot.converter.Converter which uses discord.ext.commands.Command._actual_conversion() for conversions.
But it requires context which, needs both bot and message to lookup for conversions.
So the usage of discordRebot.converter.Converter would like:
Convert = Converter(bot=client)
member = Convert(msg, "nkpro", discord.Member) # msg is the :obj:`discord.Message` passed to every callback
Convertions¶
MemberConvertions (string -> discord.Member)¶
The lookup strategy is as follows (in order):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name#discrim
4. Lookup by name
5. Lookup by nickname
Example:
@key(re.compile(r"^\?joined (\S*)$"))
async def joined_at(msg, member):
"""Says when a member joined."""
member = await Convert(msg, member, discord.Member)
return str(member.joined_at)
# "?joined nkpro" -> "2020-05-18 17:36:52.843000"
UserConvertions (string -> discord.User)¶
The lookup strategy is as follows (in order):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name#discrim
4. Lookup by name
Example:
@key(re.compile(r"^\?discriminator (\S*)$"))
async def discriminator(msg, user):
"""Gives discriminator of user"""
user = await Convert(msg, user, discord.User)
return user.discriminator
# "?discriminator nkpro" -> "3766"
MessageConvertions (string -> discord.Message)¶
The lookup strategy is as follows (in order):
1. Lookup by “{channel ID}-{message ID}” (retrieved by shift-clicking on “Copy ID”)
2. Lookup by message ID (the message must be in the context channel)
3. Lookup by message URL
Example:
@key(re.compile(r"^\?reactions (\S*)$"))
async def reactions(msg, message):
"""Shows all reactions to a given message"""
message = await Convert(msg, message, discord.Message)
return str(message.reactions)
# "?reactions 1234567890-0987654321" -> "[<Reaction emoji=':smiley:' me=False count=1>]"
TextChannelConvertions (string -> discord.TextChannel)¶
The lookup strategy is as follows (in order):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name
Example:
@key(re.compile(r"^>sendHI (\S*)$"))
async def send_hi(msg, channel):
"""Sends HI message to given channel"""
channel = await Convert(msg, channel, discord.TextChannel)
await channel.send("HI")
# ">sendHI general" -> "HI" (to the channel named 'general')
VoiceChannelConvertions (string -> discord.VoiceChannel)¶
The lookup strategy is as follows (in order):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name
Example:
@key(re.compile(r"^\?connectednos (\S*)$"))
async def n_connected_members(msg, channel):
"""Gives no of connected members in given voice channel"""
channel = await Convert(msg, channel, discord.VoiceChannel)
return len(channel.voice_states.keys())
# "?connectednos General" -> "1"
CategoryChannelConvertions (string -> discord.CategoryChannel)¶
The lookup strategy is as follows (in order):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name
Example:
@key(re.compile(r"^\?channelnos (\S*)$"))
async def n_channels(msg, category):
"""Gives no of channels in given category"""
category = await Convert(msg, category, discord.CategoryChannel)
return len(category.channels)
# "?channelnos category1" -> "2"
RoleConvertions (string -> discord.Role)¶
The lookup strategy is as follows (in order):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name
Example:
@key(re.compile(r"^\?members (\S*)$"))
async def members_in_role(msg, role):
"""Gives members name in given role"""
role = await Convert(msg, role, discord.Role)
return str([m.name for m in role.members])
# "?members Admin" -> "['nkpro', 'mybot']"
OtherConvertions¶
InviteConverter
GameConverter
ColourConverter
EmojiConverter
PartialEmojiConverter