【问题标题】:Trying to use a while loop to repeatedly scan for a user response in a Discord bot but not working尝试使用 while 循环重复扫描 Discord 机器人中的用户响应但无法正常工作
【发布时间】:2021-12-20 20:52:28
【问题描述】:

我正在为我所在的 D&D 服务器开发一个掷骰子机器人,它应该循环并反复测试来自用户说掷骰子或结束战斗的消息,但出于某种原因它只是行不通。没有错误信息,什么都没有。有人知道怎么解决吗?

@bot.command()
async def battle(ctx, arg, arg2):
    await ctx.send("**A battle has begun between "+arg+" and "+arg2+"!**")
    await ctx.send("_ _")
    await ctx.send("Would you like to use dice roll mechanics for this battle? [yes/no]")
    @bot.event
    async def on_message(message):
        if message.content == "no" or message.content == "No" or message.content == "NO" or message.content == "n" or message.content == "N":
            await ctx.send("Ok! Pin the battle message to archive this fight and get to RPing!")
        if message.content == "yes" or message.content == "Yes" or message.content == "YES" or message.content == "y" or message.content == "Y":
            await ctx.send("Alright! Whenever your character makes a move or performs an action, say 'roll die' to roll a 20-sided die and see how effective your action was! To end the battle, say 'end battle'!")
            fight = 0
            while fight == 0:
                @bot.event
                async def on_message(message):
                    if message.content == "roll die" or message.content == "ROLL DIE" or message.content == "Roll die" or message.content == "Roll Die":
                        roll = random.randint(1, 20)
                        await ctx.send("You rolled: "+str(roll))
                    if message.content == "end battle" or message.content == "END BATTLE" or message.content == "End battle" or message.conent == "End Battle":
                        await ctx.send("Battle is over! Pin the starting battle message I sent to archive this fight!")
                        fight+=1
                        

【问题讨论】:

  • 你为什么要定义一个函数on_message()一个名为on_message()的函数?在while循环内?并且从不调用它?
  • 查看 discord.py 的 Client.wait_for() 方法。

标签: python while-loop discord discord.py bots


【解决方案1】:

我强烈建议您逐步重新格式化您的代码。这意味着您的机器人仅在需要时才寻找响应。在您当前的设置下,如果您在任何时间任何地方输入“否”,您的机器人就会执行该场景。

这是您的代码的重新格式化版本,我认为它可以更好地满足您的需求:

import asyncio
import random

@bot.command()
async def battle(ctx, arg1, arg2):
    await ctx.send(f"**A battle has begun between {arg1} and {arg2}!**")
    await ctx.send("_ _")
    await ctx.send("Would you like to use dice roll mechanics for this battle? [yes/no]")

    def check(message):
        if message.channel != ctx.channel:
            return False

        if message.author != ctx.author:
            return False

        return True

    try:
        response = await client.wait_for("message", check=check, timeout=30)
    except asyncio.TimeoutError:
        return await ctx.send("You didn't answer in time.")

    if response.content == "no" or response.content == "No" or response.content == "NO" or response.content == "n" or response.content == "N":
        await ctx.send("Ok! Pin the battle message to archive this fight and get to RPing!")
    if response.content == "yes" or response.content == "Yes" or response.content == "YES" or response.content == "y" or response.content == "Y":
        await ctx.send("Alright! Whenever your character makes a move or performs an action, say 'roll die' to roll a 20-sided die and see how effective your action was! To end the battle, say 'end battle'!")
        fight = 0

        while fight == 0:
            try:
                response = await client.wait_for("message", check=check, timeout=30)
            except asyncio.TimeoutError:
                return await ctx.send("You didn't answer in time.")

            if response.content == "roll die" or response.content == "ROLL DIE" or response.content == "Roll die" or response.content == "Roll Die":
                roll = random.randint(1, 20)
                await ctx.send(f"You rolled: {roll}")
            if response.content == "end battle" or response.content == "END BATTLE" or response.content == "End battle" or response.content == "End Battle":
                await ctx.send("Battle is over! Pin the starting battle message I sent to archive this fight!")
                fight += 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-19
    • 2021-12-31
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    相关资源
    最近更新 更多