英文:
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't have an attribute called `creator`. To get the original author you can take an argument. Then, you can override discord'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="Начать Игру", 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论