Pycord削减命令的参数类型与上下文相关的汽车完成



为了提供一个简单的示例,假设我想要一个bot,它提供命令/ping_channel <channel>,该命令以通道作为参数,然后bot向该通道写入消息。使用Pycord,我可以像这样轻松实现:

@bot.slash_command(name="ping_channel", guild_ids=[...])
async def ping_channel(ctx, channel: discord.TextChannel):
await channel.send("ping")
await ctx.respond(f"I sent a message to: {channel}")

这为用户提供了一个很好的自动完成通道列表。然而,我想让用户使用聊天机器人ping私人频道,他们不是成员。我知道我可以像这样提供一个频道列表:

@bot.slash_command(name="ping_channel", guild_ids=[...])
async def ping_channel(
ctx,
channel_name: Option(str, "channel", choices=[OptionChoice("general"), OptionChoice("lobby")]),
):

不幸的是,使用像

这样的东西
choices=[OptionChoice(channel.name) for channel in bot.guilds[0].channels]

获取每个现有通道的OptionChoice失败,因为这显然是在bot知道任何公会(即bot)之前评估的。

所以这是我的问题:是否有一种方法可以根据上下文(例如,给出所有通道的列表)获得斜杠命令的自定义自动完成?

如果你想要公会的频道,你可以简单地使用discord.abc.GuildChannel类型作为你的选项。

@bot.slash_command(name="test_command", guild_ids=[...])
@option("channel", discord.abc.GuildChannel)
async def test_cmd(ctx, channel):
pass

然而,这也计算类别(以及StageChannels和ForumChannels),所以您可能想要使用typing.Union[discord.TextChannel, discord.VoiceChannel]

最新更新