Is there an option type for only Voice Channels in discord_slash?

Viewed 676

Is there an option type for only Voice Channels in discord_slash, similar to the following? this

2 Answers

Although I don’t have any experience with discord.py-interactions in particular (the library of discord-slash, renamed from discord-py-slash-command), but I’ve done some research and testing on this library. Here’s my findings:

Is it possible to specify only voice channels through Discord API? Yes definitely. But not natively with discord-slash. You’ll need to apply the following workaround:

only_vc_option = create_option(
    name="choice",
    description="You can only choose a VC!",
    option_type=SlashCommandOptionType.CHANNEL,
    required=True
)
only_vc_option['channel_types'] = [2]


@slash.slash(name="test",
             description="This is just a test command.",
             options=[only_vc_option])
async def test(ctx, choice: discord.VoiceChannel):
    await ctx.send(content=f"You chose {choice}, which is a {type(choice)}!")

discord-slash doesn't have any support to specify the channel type explicitly, but it also doesn't sanitize the options dict that gets passed to Discord API. Therefore, it is possible to create the option as usual, then add the channel_types key and set it to a list of only 2 (voice channel type). And when you type-hint discord.VoiceChannel, discord.py does the proper channel conversion for you.

As a footnote, discord.py-interactions isn't well maintained nor documented, perhaps try out some other active libraries such as disnake which is discord.py combined with dislash. (I have no affiliations, just a recommendation)

Related