Skip to content

Commit 442ad40

Browse files
authored
[commands] Add SoundboardSoundConverter
1 parent 5734996 commit 442ad40

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

discord/ext/commands/converter.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
'GuildChannelConverter',
8383
'GuildStickerConverter',
8484
'ScheduledEventConverter',
85+
'SoundboardSoundConverter',
8586
'clean_content',
8687
'Greedy',
8788
'Range',
@@ -951,6 +952,44 @@ async def convert(self, ctx: Context[BotT], argument: str) -> discord.ScheduledE
951952
return result
952953

953954

955+
class SoundboardSoundConverter(IDConverter[discord.SoundboardSound]):
956+
"""Converts to a :class:`~discord.SoundboardSound`.
957+
958+
Lookups are done for the local guild if available. Otherwise, for a DM context,
959+
lookup is done by the global cache.
960+
961+
The lookup strategy is as follows (in order):
962+
963+
1. Lookup by ID.
964+
2. Lookup by name.
965+
966+
.. versionadded:: 2.5
967+
"""
968+
969+
async def convert(self, ctx: Context[BotT], argument: str) -> discord.SoundboardSound:
970+
guild = ctx.guild
971+
match = self._get_id_match(argument)
972+
result = None
973+
974+
if match:
975+
# ID match
976+
sound_id = int(match.group(1))
977+
if guild:
978+
result = guild.get_soundboard_sound(sound_id)
979+
else:
980+
result = ctx.bot.get_soundboard_sound(sound_id)
981+
else:
982+
# lookup by name
983+
if guild:
984+
result = discord.utils.get(guild.soundboard_sounds, name=argument)
985+
else:
986+
result = discord.utils.get(ctx.bot.soundboard_sounds, name=argument)
987+
if result is None:
988+
raise SoundboardSoundNotFound(argument)
989+
990+
return result
991+
992+
954993
class clean_content(Converter[str]):
955994
"""Converts the argument to mention scrubbed version of
956995
said content.
@@ -1263,6 +1302,7 @@ def is_generic_type(tp: Any, *, _GenericAlias: type = _GenericAlias) -> bool:
12631302
discord.GuildSticker: GuildStickerConverter,
12641303
discord.ScheduledEvent: ScheduledEventConverter,
12651304
discord.ForumChannel: ForumChannelConverter,
1305+
discord.SoundboardSound: SoundboardSoundConverter,
12661306
}
12671307

12681308

discord/ext/commands/errors.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
'EmojiNotFound',
7676
'GuildStickerNotFound',
7777
'ScheduledEventNotFound',
78+
'SoundboardSoundNotFound',
7879
'PartialEmojiConversionFailure',
7980
'BadBoolArgument',
8081
'MissingRole',
@@ -564,6 +565,24 @@ def __init__(self, argument: str) -> None:
564565
super().__init__(f'ScheduledEvent "{argument}" not found.')
565566

566567

568+
class SoundboardSoundNotFound(BadArgument):
569+
"""Exception raised when the bot can not find the soundboard sound.
570+
571+
This inherits from :exc:`BadArgument`
572+
573+
.. versionadded:: 2.5
574+
575+
Attributes
576+
-----------
577+
argument: :class:`str`
578+
The sound supplied by the caller that was not found
579+
"""
580+
581+
def __init__(self, argument: str) -> None:
582+
self.argument: str = argument
583+
super().__init__(f'SoundboardSound "{argument}" not found.')
584+
585+
567586
class BadBoolArgument(BadArgument):
568587
"""Exception raised when a boolean argument was not convertable.
569588

docs/ext/commands/api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,9 @@ Exceptions
708708
.. autoexception:: discord.ext.commands.ScheduledEventNotFound
709709
:members:
710710

711+
.. autoexception:: discord.ext.commands.SoundboardSoundNotFound
712+
:members:
713+
711714
.. autoexception:: discord.ext.commands.BadBoolArgument
712715
:members:
713716

@@ -800,6 +803,7 @@ Exception Hierarchy
800803
- :exc:`~.commands.EmojiNotFound`
801804
- :exc:`~.commands.GuildStickerNotFound`
802805
- :exc:`~.commands.ScheduledEventNotFound`
806+
- :exc:`~.commands.SoundboardSoundNotFound`
803807
- :exc:`~.commands.PartialEmojiConversionFailure`
804808
- :exc:`~.commands.BadBoolArgument`
805809
- :exc:`~.commands.RangeError`

0 commit comments

Comments
 (0)