Converters
NAFF_MODEL_TO_CONVERTER: dict
¶
A dictionary mapping of naff objects to their corresponding converters.
class
NoArgumentConverter (Converter)
¶
An indicator class for special type of converters that only uses the Context.
This is mainly needed for prefixed commands, as arguments will be "eaten up" by converters otherwise.
Source code in naff/models/naff/converters.py
class NoArgumentConverter(Converter[T_co]):
"""
An indicator class for special type of converters that only uses the Context.
This is mainly needed for prefixed commands, as arguments will be "eaten up" by converters otherwise.
"""
async
inherited
method
convert(self, ctx, argument)
¶
The function that converts an argument to the appropriate type.
This should be overridden by subclasses for their conversion logic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
Any |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
Any |
The converted argument. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: "Context", argument: Any) -> T_co:
"""
The function that converts an argument to the appropriate type.
This should be overridden by subclasses for their conversion logic.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
Any: The converted argument.
"""
raise NotImplementedError("Derived classes need to implement this.")
class
IDConverter (Converter)
¶
The base converter for objects that have snowflake IDs.
Source code in naff/models/naff/converters.py
class IDConverter(Converter[T_co]):
"""The base converter for objects that have snowflake IDs."""
@staticmethod
def _get_id_match(argument: str) -> Optional[re.Match[str]]:
return _ID_REGEX.match(argument)
async
inherited
method
convert(self, ctx, argument)
¶
The function that converts an argument to the appropriate type.
This should be overridden by subclasses for their conversion logic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
Any |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
Any |
The converted argument. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: "Context", argument: Any) -> T_co:
"""
The function that converts an argument to the appropriate type.
This should be overridden by subclasses for their conversion logic.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
Any: The converted argument.
"""
raise NotImplementedError("Derived classes need to implement this.")
class
SnowflakeConverter (IDConverter)
¶
Converts a string argument to a SnowflakeObject.
Source code in naff/models/naff/converters.py
class SnowflakeConverter(IDConverter[SnowflakeObject]):
"""Converts a string argument to a SnowflakeObject."""
async def convert(self, ctx: Context, argument: str) -> SnowflakeObject:
"""
Converts a given string to a SnowflakeObject.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By role or channel mention.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
SnowflakeObject: The converted object.
"""
match = self._get_id_match(argument) or re.match(r"<(?:@(?:!|&)?|#)([0-9]{15,})>$", argument)
if match is None:
raise BadArgument(argument)
return SnowflakeObject(int(match.group(1))) # type: ignore
async
method
convert(self, ctx, argument)
¶
Converts a given string to a SnowflakeObject.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By role or channel mention.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
SnowflakeObject |
The converted object. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> SnowflakeObject:
"""
Converts a given string to a SnowflakeObject.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By role or channel mention.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
SnowflakeObject: The converted object.
"""
match = self._get_id_match(argument) or re.match(r"<(?:@(?:!|&)?|#)([0-9]{15,})>$", argument)
if match is None:
raise BadArgument(argument)
return SnowflakeObject(int(match.group(1))) # type: ignore
class
ChannelConverter (IDConverter)
¶
The base converter for channel objects.
Source code in naff/models/naff/converters.py
class ChannelConverter(IDConverter[T_co]):
"""The base converter for channel objects."""
def _check(self, result: BaseChannel) -> bool:
return True
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
async
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
BaseChannelConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class BaseChannelConverter(ChannelConverter[BaseChannel]):
pass
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
DMChannelConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class DMChannelConverter(ChannelConverter[DMChannel]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, DMChannel)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
DMConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class DMConverter(ChannelConverter[DM]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, DM)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
DMGroupConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class DMGroupConverter(ChannelConverter[DMGroup]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, DMGroup)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
GuildChannelConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class GuildChannelConverter(ChannelConverter[GuildChannel]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, GuildChannel)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
GuildNewsConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class GuildNewsConverter(ChannelConverter[GuildNews]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, GuildNews)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
GuildCategoryConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class GuildCategoryConverter(ChannelConverter[GuildCategory]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, GuildCategory)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
GuildTextConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class GuildTextConverter(ChannelConverter[GuildText]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, GuildText)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
ThreadChannelConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class ThreadChannelConverter(ChannelConverter[ThreadChannel]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, ThreadChannel)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
GuildNewsThreadConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class GuildNewsThreadConverter(ChannelConverter[GuildNewsThread]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, GuildNewsThread)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
GuildPublicThreadConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class GuildPublicThreadConverter(ChannelConverter[GuildPublicThread]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, GuildPublicThread)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
GuildPrivateThreadConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class GuildPrivateThreadConverter(ChannelConverter[GuildPrivateThread]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, GuildPrivateThread)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
VoiceChannelConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class VoiceChannelConverter(ChannelConverter[VoiceChannel]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, VoiceChannel)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
GuildVoiceConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class GuildVoiceConverter(ChannelConverter[GuildVoice]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, GuildVoice)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
GuildStageVoiceConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class GuildStageVoiceConverter(ChannelConverter[GuildStageVoice]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, GuildStageVoice)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
MessageableChannelConverter (ChannelConverter)
¶
Source code in naff/models/naff/converters.py
class MessageableChannelConverter(ChannelConverter[TYPE_MESSAGEABLE_CHANNEL]):
def _check(self, result: BaseChannel) -> bool:
return isinstance(result, _MESSAGEABLE_CHANNEL_TYPES)
async
inherited
method
convert(self, ctx, argument)
¶
Converts a given string to a Channel object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By channel mention.
-
By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
BaseChannel |
The converted object. The channel type will be of the type the converter represents. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> T_co:
"""
Converts a given string to a Channel object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By channel mention.
3. By name - the bot will search in a guild if the context has it, otherwise it will search globally.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
BaseChannel: The converted object.
The channel type will be of the type the converter represents.
"""
match = self._get_id_match(argument) or re.match(r"<#([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_channel(int(match.group(1)))
elif ctx.guild:
result = next((c for c in ctx.guild.channels if c.name == argument), None)
else:
result = next((c for c in ctx.bot.cache.channel_cache.values() if c.name == argument), None)
if not result:
raise BadArgument(f'Channel "{argument}" not found.')
if self._check(result):
return result # type: ignore
raise BadArgument(f'Channel "{argument}" not found.')
class
UserConverter (IDConverter)
¶
Converts a string argument to a User object.
Source code in naff/models/naff/converters.py
class UserConverter(IDConverter[User]):
"""Converts a string argument to a User object."""
async def convert(self, ctx: Context, argument: str) -> User:
"""
Converts a given string to a User object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By mention.
3. By username + tag (ex User#1234).
4. By username.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
User: The converted object.
"""
match = self._get_id_match(argument) or re.match(r"<@!?([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_user(int(match.group(1)))
else:
if len(argument) > 5 and argument[-5] == "#":
result = next((u for u in ctx.bot.cache.user_cache.values() if u.tag == argument), None)
if not result:
result = next((u for u in ctx.bot.cache.user_cache.values() if u.username == argument), None)
if not result:
raise BadArgument(f'User "{argument}" not found.')
return result
async
method
convert(self, ctx, argument)
¶
Converts a given string to a User object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By mention.
-
By username + tag (ex User#1234).
-
By username.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
User |
The converted object. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> User:
"""
Converts a given string to a User object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By mention.
3. By username + tag (ex User#1234).
4. By username.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
User: The converted object.
"""
match = self._get_id_match(argument) or re.match(r"<@!?([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.bot.fetch_user(int(match.group(1)))
else:
if len(argument) > 5 and argument[-5] == "#":
result = next((u for u in ctx.bot.cache.user_cache.values() if u.tag == argument), None)
if not result:
result = next((u for u in ctx.bot.cache.user_cache.values() if u.username == argument), None)
if not result:
raise BadArgument(f'User "{argument}" not found.')
return result
class
MemberConverter (IDConverter)
¶
Converts a string argument to a Member object.
Source code in naff/models/naff/converters.py
class MemberConverter(IDConverter[Member]):
"""Converts a string argument to a Member object."""
def _get_member_from_list(self, members: list[Member], argument: str) -> Optional[Member]:
# sourcery skip: assign-if-exp
result = None
if len(argument) > 5 and argument[-5] == "#":
result = next((m for m in members if m.user.tag == argument), None)
if not result:
result = next((m for m in members if m.display_name == argument or m.user.username == argument), None)
return result
async def convert(self, ctx: Context, argument: str) -> Member:
"""
Converts a given string to a Member object. This will only work in guilds.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By mention.
3. By username + tag (ex User#1234).
4. By nickname.
5. By username.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
Member: The converted object.
"""
if not ctx.guild:
raise BadArgument("This command cannot be used in private messages.")
match = self._get_id_match(argument) or re.match(r"<@!?([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.guild.fetch_member(int(match.group(1)))
elif ctx.guild.chunked:
result = self._get_member_from_list(ctx.guild.members, argument)
else:
query = argument
if len(argument) > 5 and argument[-5] == "#":
query, _, _ = argument.rpartition("#")
members = await ctx.guild.search_members(query, limit=100)
result = self._get_member_from_list(members, argument)
if not result:
raise BadArgument(f'Member "{argument}" not found.')
return result
async
method
convert(self, ctx, argument)
¶
Converts a given string to a Member object. This will only work in guilds.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By mention.
-
By username + tag (ex User#1234).
-
By nickname.
-
By username.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
Member |
The converted object. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> Member:
"""
Converts a given string to a Member object. This will only work in guilds.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By mention.
3. By username + tag (ex User#1234).
4. By nickname.
5. By username.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
Member: The converted object.
"""
if not ctx.guild:
raise BadArgument("This command cannot be used in private messages.")
match = self._get_id_match(argument) or re.match(r"<@!?([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.guild.fetch_member(int(match.group(1)))
elif ctx.guild.chunked:
result = self._get_member_from_list(ctx.guild.members, argument)
else:
query = argument
if len(argument) > 5 and argument[-5] == "#":
query, _, _ = argument.rpartition("#")
members = await ctx.guild.search_members(query, limit=100)
result = self._get_member_from_list(members, argument)
if not result:
raise BadArgument(f'Member "{argument}" not found.')
return result
class
MessageConverter (Converter)
¶
Converts a string argument to a Message object.
Source code in naff/models/naff/converters.py
class MessageConverter(Converter[Message]):
"""Converts a string argument to a Message object."""
# either just the id or <chan_id>-<mes_id>, a format you can get by shift clicking "copy id"
_ID_REGEX = re.compile(r"(?:(?P<channel_id>[0-9]{15,})-)?(?P<message_id>[0-9]{15,})")
# of course, having a way to get it from a link is nice
_MESSAGE_LINK_REGEX = re.compile(
r"https?://[\S]*?discord(?:app)?\.com/channels/(?P<guild_id>[0-9]{15,}|@me)/(?P<channel_id>[0-9]{15,})/(?P<message_id>[0-9]{15,})\/?$"
)
async def convert(self, ctx: Context, argument: str) -> Message:
"""
Converts a given string to a Message object.
The lookup strategy is as follows:
1. By raw snowflake ID. The message must be in the same channel as the context.
2. By message + channel ID in the format of "{Channel ID}-{Message ID}". This can be obtained by shift clicking "Copy ID" when Developer Mode is enabled.
3. By message link.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
Message: The converted object.
"""
match = self._ID_REGEX.match(argument) or self._MESSAGE_LINK_REGEX.match(argument)
if not match:
raise BadArgument(f'Message "{argument}" not found.')
data = match.groupdict()
message_id = data["message_id"]
channel_id = int(data["channel_id"]) if data.get("channel_id") else ctx.channel.id
# this guild checking is technically unnecessary, but we do it just in case
# it means a user cant just provide an invalid guild id and still get a message
guild_id = data["guild_id"] if data.get("guild_id") else ctx.guild_id
guild_id = int(guild_id) if guild_id != "@me" else None
try:
# this takes less possible requests than getting the guild and/or channel
mes = await ctx.bot.cache.fetch_message(channel_id, message_id)
if mes._guild_id != guild_id:
raise BadArgument(f'Message "{argument}" not found.')
return mes
except Forbidden as e:
raise BadArgument(f"Cannot read messages for <#{channel_id}>.") from e
except HTTPException as e:
raise BadArgument(f'Message "{argument}" not found.') from e
async
method
convert(self, ctx, argument)
¶
Converts a given string to a Message object.
The lookup strategy is as follows:
-
By raw snowflake ID. The message must be in the same channel as the context.
-
By message + channel ID in the format of "{Channel ID}-{Message ID}". This can be obtained by shift clicking "Copy ID" when Developer Mode is enabled.
-
By message link.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
Message |
The converted object. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> Message:
"""
Converts a given string to a Message object.
The lookup strategy is as follows:
1. By raw snowflake ID. The message must be in the same channel as the context.
2. By message + channel ID in the format of "{Channel ID}-{Message ID}". This can be obtained by shift clicking "Copy ID" when Developer Mode is enabled.
3. By message link.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
Message: The converted object.
"""
match = self._ID_REGEX.match(argument) or self._MESSAGE_LINK_REGEX.match(argument)
if not match:
raise BadArgument(f'Message "{argument}" not found.')
data = match.groupdict()
message_id = data["message_id"]
channel_id = int(data["channel_id"]) if data.get("channel_id") else ctx.channel.id
# this guild checking is technically unnecessary, but we do it just in case
# it means a user cant just provide an invalid guild id and still get a message
guild_id = data["guild_id"] if data.get("guild_id") else ctx.guild_id
guild_id = int(guild_id) if guild_id != "@me" else None
try:
# this takes less possible requests than getting the guild and/or channel
mes = await ctx.bot.cache.fetch_message(channel_id, message_id)
if mes._guild_id != guild_id:
raise BadArgument(f'Message "{argument}" not found.')
return mes
except Forbidden as e:
raise BadArgument(f"Cannot read messages for <#{channel_id}>.") from e
except HTTPException as e:
raise BadArgument(f'Message "{argument}" not found.') from e
class
GuildConverter (IDConverter)
¶
Converts a string argument to a Guild object.
Source code in naff/models/naff/converters.py
class GuildConverter(IDConverter[Guild]):
"""Converts a string argument to a Guild object."""
async def convert(self, ctx: Context, argument: str) -> Guild:
"""
Converts a given string to a Guild object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By name.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
Guild: The converted object.
"""
match = self._get_id_match(argument)
result = None
if match:
result = await ctx.bot.fetch_guild(int(match.group(1)))
else:
result = next((g for g in ctx.bot.guilds if g.name == argument), None)
if not result:
raise BadArgument(f'Guild "{argument}" not found.')
return result
async
method
convert(self, ctx, argument)
¶
Converts a given string to a Guild object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
Guild |
The converted object. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> Guild:
"""
Converts a given string to a Guild object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By name.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
Guild: The converted object.
"""
match = self._get_id_match(argument)
result = None
if match:
result = await ctx.bot.fetch_guild(int(match.group(1)))
else:
result = next((g for g in ctx.bot.guilds if g.name == argument), None)
if not result:
raise BadArgument(f'Guild "{argument}" not found.')
return result
class
RoleConverter (IDConverter)
¶
Converts a string argument to a Role object.
Source code in naff/models/naff/converters.py
class RoleConverter(IDConverter[Role]):
"""Converts a string argument to a Role object."""
async def convert(self, ctx: Context, argument: str) -> Role:
"""
Converts a given string to a Role object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By mention.
3. By name.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
Role: The converted object.
"""
if not ctx.guild:
raise BadArgument("This command cannot be used in private messages.")
match = self._get_id_match(argument) or re.match(r"<@&([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.guild.fetch_role(int(match.group(1)))
else:
result = next((r for r in ctx.guild.roles if r.name == argument), None)
if not result:
raise BadArgument(f'Role "{argument}" not found.')
return result
async
method
convert(self, ctx, argument)
¶
Converts a given string to a Role object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By mention.
-
By name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
Role |
The converted object. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> Role:
"""
Converts a given string to a Role object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By mention.
3. By name.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
Role: The converted object.
"""
if not ctx.guild:
raise BadArgument("This command cannot be used in private messages.")
match = self._get_id_match(argument) or re.match(r"<@&([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.guild.fetch_role(int(match.group(1)))
else:
result = next((r for r in ctx.guild.roles if r.name == argument), None)
if not result:
raise BadArgument(f'Role "{argument}" not found.')
return result
class
PartialEmojiConverter (IDConverter)
¶
Converts a string argument to a PartialEmoji object.
Source code in naff/models/naff/converters.py
class PartialEmojiConverter(IDConverter[PartialEmoji]):
"""Converts a string argument to a PartialEmoji object."""
async def convert(self, ctx: Context, argument: str) -> PartialEmoji:
"""
Converts a given string to a PartialEmoji object.
This converter only accepts emoji strings.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
PartialEmoji: The converted object.
"""
if match := re.match(r"<a?:[a-zA-Z0-9\_]{1,32}:([0-9]{15,})>$", argument):
emoji_animated = bool(match.group(1))
emoji_name = match.group(2)
emoji_id = int(match.group(3))
return PartialEmoji(id=emoji_id, name=emoji_name, animated=emoji_animated) # type: ignore
raise BadArgument(f'Couldn\'t convert "{argument}" to {PartialEmoji.__name__}.')
async
method
convert(self, ctx, argument)
¶
Converts a given string to a PartialEmoji object.
This converter only accepts emoji strings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
PartialEmoji |
The converted object. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> PartialEmoji:
"""
Converts a given string to a PartialEmoji object.
This converter only accepts emoji strings.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
PartialEmoji: The converted object.
"""
if match := re.match(r"<a?:[a-zA-Z0-9\_]{1,32}:([0-9]{15,})>$", argument):
emoji_animated = bool(match.group(1))
emoji_name = match.group(2)
emoji_id = int(match.group(3))
return PartialEmoji(id=emoji_id, name=emoji_name, animated=emoji_animated) # type: ignore
raise BadArgument(f'Couldn\'t convert "{argument}" to {PartialEmoji.__name__}.')
class
CustomEmojiConverter (IDConverter)
¶
Converts a string argument to a CustomEmoji object.
Source code in naff/models/naff/converters.py
class CustomEmojiConverter(IDConverter[CustomEmoji]):
"""Converts a string argument to a CustomEmoji object."""
async def convert(self, ctx: Context, argument: str) -> CustomEmoji:
"""
Converts a given string to a CustomEmoji object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By the emoji string format.
3. By name.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
CustomEmoji: The converted object.
"""
if not ctx.guild:
raise BadArgument("This command cannot be used in private messages.")
match = self._get_id_match(argument) or re.match(r"<a?:[a-zA-Z0-9\_]{1,32}:([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.guild.fetch_custom_emoji(int(match.group(1)))
else:
if ctx.bot.cache.enable_emoji_cache:
emojis = ctx.bot.cache.emoji_cache.values() # type: ignore
result = next((e for e in emojis if e.name == argument))
if not result:
emojis = await ctx.guild.fetch_all_custom_emojis()
result = next((e for e in emojis if e.name == argument))
if not result:
raise BadArgument(f'Emoji "{argument}" not found.')
return result
async
method
convert(self, ctx, argument)
¶
Converts a given string to a CustomEmoji object.
The lookup strategy is as follows:
-
By raw snowflake ID.
-
By the emoji string format.
-
By name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ctx |
Context |
The context to use for the conversion. |
required |
argument |
str |
The argument to be converted. |
required |
Returns:
Type | Description |
---|---|
CustomEmoji |
The converted object. |
Source code in naff/models/naff/converters.py
async def convert(self, ctx: Context, argument: str) -> CustomEmoji:
"""
Converts a given string to a CustomEmoji object.
The lookup strategy is as follows:
1. By raw snowflake ID.
2. By the emoji string format.
3. By name.
Args:
ctx: The context to use for the conversion.
argument: The argument to be converted.
Returns:
CustomEmoji: The converted object.
"""
if not ctx.guild:
raise BadArgument("This command cannot be used in private messages.")
match = self._get_id_match(argument) or re.match(r"<a?:[a-zA-Z0-9\_]{1,32}:([0-9]{15,})>$", argument)
result = None
if match:
result = await ctx.guild.fetch_custom_emoji(int(match.group(1)))
else:
if ctx.bot.cache.enable_emoji_cache:
emojis = ctx.bot.cache.emoji_cache.values() # type: ignore
result = next((e for e in emojis if e.name == argument))
if not result:
emojis = await ctx.guild.fetch_all_custom_emojis()
result = next((e for e in emojis if e.name == argument))
if not result:
raise BadArgument(f'Emoji "{argument}" not found.')
return result
class
Greedy (list, Generic)
¶
A special marker class to mark an argument in a prefixed command to repeatedly convert until it fails to convert an argument.
Source code in naff/models/naff/converters.py
class Greedy(List[T]):
"""A special marker class to mark an argument in a prefixed command to repeatedly convert until it fails to convert an argument."""