簡體   English   中英

斜線命令的 Pycord 參數類型與上下文相關的自動完成

[英]Pycord parameter type for slash command with context-dependent auto completion

為了一個簡單的例子,假設我想要一個提供命令/ping_channel <channel>的機器人,該命令將通道作為參數,然后機器人將消息寫入該通道。 使用 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.guilds 為空)。

所以這是我的問題:有沒有辦法讓斜線命令的自定義自動完成取決於上下文(例如,給出所有通道的列表)?

如果您想要的只是公會的頻道,您可以簡單地使用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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM