按钮仅可由消息的作者使用。

huangapple go评论90阅读模式
英文:

I want to make that the button could be used only by the author of the message

问题

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)
英文:
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)

I used something like this but it doesn't work

答案1

得分: 1

[discord.Interaction][1] 没有名为 `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
因此,在调用您的视图时:
@tree.command()
async def testcommand(interaction: discord.Interaction):
    view = HangmanButton(interaction.user) # 或者在适用的情况下使用 ctx.author/message.author
    await interaction.response.send_message(view=view)

<details>
<summary>英文:</summary>

[discord.Interaction][1] doesn&#39;t have an attribute called `creator`. To get the original author you can take an argument. Then, you can override discord&#39;s check with your own `interaction_check`.

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

@discord.ui.button(label=&quot;Начать Игру&quot;, 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
Hence, when calling your view:

@tree.command()
async def testcommand(interaction: discord.Interaction):
view = HangmanButton(interaction.user) # or ctx.author/message.author where applicable
await interaction.response.send_message(view=view)



  [1]: https://discordpy.readthedocs.io/en/stable/interactions/api.html#discord.Interaction

</details>



huangapple
  • 本文由 发表于 2023年3月10日 01:35:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定