discord.py 如何使按钮仅对发出命令的人可用

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

好吧,所以我正在尝试制作一个具有多个有趣命令的机器人,无论如何,所以我一切都很好,但后来我注意到其他人正在单击按钮而不是制作按钮的原始人,我试图到处看看如何使其只有执行该命令的人才能单击该按钮,但无法弄清楚。请提供帮助,因为人们确实滥用了这一点。 按钮 src + 命令 src :

class Menu(discord.ui.View):
    def __init__(self):
        super().__init__()
        self.value = None

    @discord.ui.button(label="Yes, i am sure", style=discord.ButtonStyle.green)
    async def yesimsure(self, interaction: discord.Interaction, button: discord.ui.Button):

        if random.randint(1, 10) == 1:
            embed = discord.Embed(
                title="Roulette Result - You Lost!",
                description="You got timed out for 5 minutes.",
                color=discord.Color.red()
            )
            await interaction.message.delete()
            duration = timedelta(minutes=5)
            await interaction.user.timeout(duration, reason="lost roulette looooooserrrrrr")
            await interaction.response.send_message(embed=embed)
        
        else:
            embed = discord.Embed(
                title="Roulette Result - You Won!",
                description="Congratulations! You didn't get timed out.",
                color=discord.Color.green()
            )
            await interaction.message.delete()
            await interaction.response.send_message(embed=embed)

    
    @discord.ui.button(label="No, cancel", style=discord.ButtonStyle.red)
    async def nuhuh(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_message("Roulette canceled.")



@tree.command(guild = None, name="roulette", description="1 chance in 10 to get timeed out for 5 mins")
async def roulette(interaction: discord.Interaction):
    user_id = interaction.user.id

    if user_id in cooldowns and cooldowns[user_id] > datetime.utcnow():
        await interaction.response.send_message("Nuh uh, you're on cooldown for the roulette!", ephemeral=True)
        return
    
    embed = discord.Embed(
        title="Roulette Verification",
        description="Are you sure you want to play the roulette?\nYou have 1 chance in 10 to get timed out for 5 minutes.",
        color=discord.Color.blue()
    )

    view = Menu()
    await interaction.response.send_message(embed=embed, view=view)

    cooldowns[user_id] = datetime.utcnow() + timedelta(seconds=30)

所以我尝试在网络和这个网站上搜索是否有人与我有同样的问题,但找不到任何东西,最后我尝试询问chatgpt,但因为我认为chatgpt对于方法来说是垃圾,只能给我一些根本不起作用或只是破坏整个事情的东西。我还尝试将其设为短暂的,这很有效,但我希望其他人也能看到嵌入内容。

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

最推荐的方法是重写视图的 interaction_check 方法。在执行视图的任何交互之前调用此方法。如果返回

False
,执行将停止。

class Menu(discord.ui.View):
    def __init__(self, original_interaction: discord.Interaction):
        super().__init__()
        self.original_interaction = original_interaction
        self.value = None

async def interaction_check(interaction: discord.Interaction) -> bool:
    if interaction.user != self.original_interaction.user:
        await interaction.response.send_message("Only the author of the command can perform this action.")
        return False
    return True

# append your buttons/selects


@tree.command()
async def roulette(interaction: discord.Interaction):
    # ...
    view = Menu()
    await interaction.response.send_message(embed=embed, view=view)

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.