【问题标题】:How to pause a command in discord.py while waiting for another command如何在等待另一个命令时暂停 discord.py 中的命令
【发布时间】:2021-09-19 12:48:04
【问题描述】:

我正在制作一个不和谐的迷你游戏,其中机器人允许我们在掷骰上下注,我需要让它在其他人接受赌注之前不会运行,所以我尝试制作一个 while 循环以打印不接受,直到其他人输入 !accept 但它不起作用,而且由于某种原因,当一个人获胜时它不会更新字典,所以即使在死亡滚动之后,无论输赢,一个人的积分仍然保持 1000

代码:

@client.command()
async def deathroll(ctx,user:discord.User,bet=100):
    roller1 = ctx.author.id
    roller2 = user.id
    accepted=False
    if roller1 not in keys:
        await ctx.send("make an account first")
    if roller2 not in keys:
        await ctx.send("please give a user for roller2")
    while accepted!=True:
        print("not accepted")
        time.sleep(10)
    if roller2 in keys:
        roll=100
        while roll!=1:
            roll=random.randint(1,roll)
            await ctx.send("roller1,rolled:"+str(roll))
            if roll==1:
                await ctx.send("the winner is roller1")
                keys[roller1]=keys[roller1]-bet
                keys[roller2]=keys[roller2]+bet
                break
            roll=random.randint(1,roll)
            await ctx.send("roller2,rolled:"+str(roll))
            if roll == 1:
                await ctx.send("the winner is roller1")
                keys[roller2]=keys[roller2]-bet
                keys[roller1]=keys[roller1]+bet
                break

@client.command()
async def accept(ctx):
    accepted=True

【问题讨论】:

  • 我怀疑它不起作用的原因是因为我认为它在尝试处理第二个命令之前等待第一个命令完成。这意味着 while 循环用一个它无法逃脱的循环阻塞了整个程序。您需要重写它以不阻塞程序,我建议将大部分逻辑移至接受函数,以便它仅在实际具有所有必要信息时尝试执行逻辑。

标签: python discord.py


【解决方案1】:

首先,您不想暂停命令。您可以这样做,但它会阻止机器人,并且它也不会执行任何其他任务,就像您的情况一样。由于重复的 sleep() 函数,机器人无法检测到 !accept 命令。您可以做的是设置一个环境,您可以在其中保存有关赌注的数据(它可以是数据库、txt 文件,任何东西),并为 !accept 命令创建一个侦听器,程序会在其中检查保存的数据之前。

【讨论】:

    【解决方案2】:

    你需要使用wait_for,它等待给定的事件并且也是异步的

    @client.command()
    async def deathroll(ctx,user:discord.User,bet=100):
        roller1 = ctx.author.id
        roller2 = user.id
        if roller1 not in keys:
            await ctx.send("make an account first")
        if roller2 not in keys:
            await ctx.send("please give a user for roller2")
        msg = await client.wait_for("message", check=lambda m: m.author.id == user.id) # make sure that it only goes ahead when the mentioned user sends !accept
        while msg.content != "!accept":
            msg = await bot.wait_for("message", check=lambda m: m.author.id == user.id)
    

    我假设keys 是在函数外部定义的字典,这就是为什么你不能在函数内部更新它 有两种方法可以解决这个问题:

    1. 在函数中添加global keys,全局不是最好的
    2. keys 设为机器人变量

    第一种方式:

    @client.command()
    async def deathroll(ctx,user:discord.User,bet=100):
        global keys
        ...
    

    第二种方式:

    # wherever u define keys = {} do this instead
    client.keys = {}
    # then use client.keys[roller1] = client.keys[roller1]-bet
    # basically replace `keys` with `client.keys`
    

    【讨论】:

    • 我在 bot.wait_for 中得到未解决的 bot 引用,我应该将 bot 变量的值设为什么?还是我需要导入的模块?
    • 别在意这个问题,我如何让用户在其他人完成 !accept 之前无法使用该功能?
    • 使用哪个函数?是的,将机器人更改为我的坏客户
    猜你喜欢
    • 2020-10-09
    • 2021-06-01
    • 2012-11-10
    • 2021-02-22
    • 2019-11-14
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 2021-03-26
    相关资源
    最近更新 更多