【问题标题】:Discord bot looping foreverDiscord 机器人永远循环
【发布时间】:2020-08-12 12:32:52
【问题描述】:

所以我正在使用 Python 制作一个不和谐的机器人,我想让它循环直到满足某些条件,这是一个简单的例子,只是为了让我想要做的事情变得清晰。

@bot.command(name='join')
async def Yacine(ctx):
user = bot.get_user(ID)
await user.send("What is your name ?")
@bot.event
async def on_message(message):
    if message.content == "Yacine":
        user = bot.get_user(ID)
        await user.send("Great name")

所以在它回答我(“伟大的名字”)之后,我希望它在我尝试时从顶部再次询问

@bot.command(name='join')
async def Yacine(ctx):
user = bot.get_user(ID)
await user.send("What is your name ?")
@bot.event
async def on_message(message):
    if message.content == "Yacine":
        user = bot.get_user(ID)
        await user.send("Great name")
Yacine(ctx)

它一直在问(“你叫什么名字?”),甚至在我回答之前。 希望你能帮我解决这个问题。

【问题讨论】:

  • 您的代码存在缩进问题。请更新问题。

标签: python loops bots discord discord.py


【解决方案1】:

你把事情复杂化了。这样做:

@bot.command(name='join')
async def Yacine(ctx):
    user = bot.get_user(ID)
    await user.send("What is your name ?")
    msg = await client.wait_for('message', check=lambda message: message.author == ctx.author)
    if “Yacine” in msg.content:
        await user.send(“Great name”)

一般就是这样,这是在我的手机上写的,所以可能是几个语法问题,但没什么大不了的。

【讨论】:

    【解决方案2】:

    您应该能够通过添加返回或循环、中断来修复它

    async def Yacine(ctx):
    user = bot.get_user(ID)
    await user.send("What is your name ?")
    @bot.event
    async def on_message(message):
        if message.content == "Yacine":
            user = bot.get_user(ID)
            await user.send("Great name")
            return
    Yacine(ctx)
    

    【讨论】:

      猜你喜欢
      • 2019-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 2017-10-12
      • 2021-04-01
      • 2021-02-02
      • 2021-11-18
      相关资源
      最近更新 更多