命令。检查按钮的模拟吗?

问题描述 投票:0回答:1

我有一个函数可以检查公会是否在我与命令一起使用的白名单中。检查我的每个命令:

async def is_active(ctx):
    return ctx.guild.id in get_whitelist()

是否有某种类似的命令。检查按钮,或者我必须手动向每个按钮处理程序写入 is_active 检查?

discord discord.py discord-buttons
1个回答
0
投票

如果您使用

interaction_check
方法在 View 类中创建一个检查,而不是将 if 语句放在每个按钮中,会容易得多:

class MyView(discord.ui.View):
    async def interaction_check(self, discord.Interaction) -> bool:
        return interaction.guild.id in get_whitelist()

    @discord.ui.button(label="Example")
    async def button(self, interaction: discord.Interaction) -> None:
        await interaction.response.send_message("This guild is not whitelisted")

参考:https://discordpy.readthedocs.io/en/latest/interactions/api.html#discord.ui.View.interaction_check

© www.soinside.com 2019 - 2024. All rights reserved.