我想让这个按钮只能由消息的作者使用

问题描述 投票:0回答:1
class HangmanButton(discord.ui.View):
    def __init__(self, *, timeout=180):
        super().__init__(timeout=timeout)


    @discord.ui.button(label="Начать Игру", style=discord.ButtonStyle.gray)
    async def button(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_modal(HangmanStartModal())

if interaction.user is not interaction.creator.author: return await interaction.response.send_message(embed = discord.Embed(title='❌| Ошибка', description='Вы не можете играть в своей собственной игре.', color=discord.Color.darker_grey()), ephemeral=True)

我用过类似的东西,但它不起作用

button discord.py bots
1个回答
0
投票

discord.Interaction 没有名为

creator
的属性。要获得原作者,您可以进行争论。然后,你可以用你自己的
interaction_check
覆盖discord的检查。

class HangmanButton(discord.ui.View):
    def __init__(self, author, timeout=180):
        super().__init__(timeout=timeout)
        self.author = author


    @discord.ui.button(label="Начать Игру", style=discord.ButtonStyle.gray)
    async def button(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_modal(HangmanStartModal())

    async def interaction_check(self, interaction: discord.Interaction):
        return interaction.user.id == self.author.id

因此,调用视图时:

HangmanButton(interaction.user) # or ctx.author/message.author where applicable
© www.soinside.com 2019 - 2024. All rights reserved.