Discord.py 如何使回复按钮运行相同的命令

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

我正在制作一个不和谐的机器人,我有一个命令,它可以通过一个基本上再次运行该命令的按钮给出随机回复,但我不知道如何获得回复以使用相同的按钮再次运行该命令,基本上就像一个循环,如果这有意义的话。如果可能的话,是否有办法添加,以便只有具有特定角色的成员才能使用该命令?

randomitem = None
@bot.command()
async def butt(ctx):
    global randomitem
    items = [
        discord.Embed(title="this is for a test"),
        discord.Embed(title="this is the second test"),
        discord.Embed(title="this is the third test"),
        discord.Embed(title="this is the fourth test"),
        discord.Embed(title="this is the fifth test"),
        discord.Embed(title="this is the sixth test"),
        discord.Embed(title="this is the seventh test"),
        discord.Embed(title="this is the eighth test")
    ]
    
    randomitem = random.choice(items)
    msg = await ctx.reply("Drawing a card...", embed=randomitem)
    
    async def button_callback(interaction):
        global randomitem
        try:
            await interaction.response.defer()
        except:
            pass
        next_embed = random.choice(items)
        while next_embed == randomitem:
            next_embed = random.choice(items)
        randomitem = next_embed
        await msg.reply("Drawing a card...", embed=next_embed)
        
    view = discord.ui.View()
    button = discord.ui.Button(label="!valshot", custom_id="!valshot")
    button.callback = button_callback
    view.add_item(button)

    await ctx.send(view=view)
python discord discord.py
1个回答
0
投票

基本上与您上一篇文章的操作相同:

view = None
randomitem = None
@bot.command()
async def butt(ctx):
    global randomitem
    items = [
        discord.Embed(title="this is for a test"),
        discord.Embed(title="this is the second test"),
        discord.Embed(title="this is the third test"),
        discord.Embed(title="this is the fourth test"),
        discord.Embed(title="this is the fifth test"),
        discord.Embed(title="this is the sixth test"),
        discord.Embed(title="this is the seventh test"),
        discord.Embed(title="this is the eighth test")
    ]
    
    randomitem = random.choice(items)
    global view
    view = discord.ui.View()
    button = discord.ui.Button(label="!valshot", custom_id="!valshot")
    view.add_item(button)
    msg = await ctx.reply("Drawing a card...", embed=randomitem,view=view)
    
    async def button_callback(interaction):
        global randomitem
        global view
        try:
            await interaction.response.defer()
        except:
            pass
        next_embed = random.choice(items)
        while next_embed == randomitem:
            next_embed = random.choice(items)
        randomitem = next_embed
        await msg.reply("Drawing a card...", embed=next_embed,view=view)
    button.callback = button_callback

工作原理:

  • 全局视图变量

  • 传递查看回复

  • 使用前先创建视图

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