discord_slash中是否存在仅适用于语音频道的专属选项类型?
解答
在discord_slash库中可以实现仅选择语音频道的选项效果,无需使用独立的专属选项类型,只需要在定义频道类型的斜杠命令选项时,额外指定channel_types过滤参数即可。
配置完成后,用户唤起命令的选项弹窗时,下拉列表就只会展示服务器内的所有语音频道,和你截图展示的效果完全一致。
示例代码参考如下:
from discord_slash import SlashCommand from discord_slash.model import SlashCommandOptionType from discord import ChannelType # 此处为已初始化的bot实例 slash = SlashCommand(bot, sync_commands=True) @slash.slash( name="pick_voice_channel", description="测试选择语音频道", options=[ { "name": "target_channel", "description": "选择一个语音频道", "type": SlashCommandOptionType.CHANNEL, "required": True, # 仅过滤展示语音频道 "channel_types": [ChannelType.voice.value] } ] ) async def pick_voice_channel(ctx, target_channel): await ctx.send(f"已选中语音频道:{target_channel.name}")
内容的提问来源于stack exchange,提问作者Omkaar
相关产品推荐
相关产品推荐

