【问题标题】:How do I make the repeat function work for my discord bot?如何使重复功能适用于我的不和谐机器人?
【发布时间】:2021-05-25 08:20:57
【问题描述】:

所以我是 discord.py 的新手,我对 Lua(另一种编码语言)有一点经验。我试图让我的机器人在等待十分钟后重复它所做的事情。但是错误告诉我没有定义“重复”。我知道定义重复是什么,但我不知道要定义什么或导入什么。我的代码如下所示。

@bot.event
async def on_reaction_add(reaction, user):
    emoji = reaction.emoji

    if user.bot:
        return

    if emoji == '1️⃣':

        await user.send('Reminding you every ten minutes.')
        await asyncio.sleep(600)
        await user.send('Reminding you to stop procrastinating!')
        repeat()

应该发生什么:

当用户以"1️⃣" 对消息做出反应时(我没有显示消息代码,因为它没有问题),我的机器人会 DM 用户说“每十分钟提醒你一次”。然后,机器人将等待十分钟,然后再次向用户发送 DM,说“提醒您停止拖延!”除非用户说“pp!remove”,否则它将重复该过程。我只是无法使重复功能正常工作。

在此先感谢您。 :)

【问题讨论】:

  • 如何调用未定义的函数?
  • 尝试使用while循环或递归

标签: python discord discord.py


【解决方案1】:

这将按照您的要求执行,但永远不会退出。不知何故,你需要让你的“停止这个!”命令设置while 循环检查的标志。

@bot.event
async def on_reaction_add(reaction, user):
    emoji = reaction.emoji

    if user.bot:
        return

    if emoji == '1️⃣':

        await user.send('Reminding you every ten minutes.')
        while True:
            await asyncio.sleep(600)
            await user.send('Reminding you to stop procrastinating!')

【讨论】:

  • 非常感谢您的回答!很抱歉打扰你,但是你知道如何发出一个命令来设置一个标志来阻止机器人每十分钟左右发出 DMing 吗?
  • 我不知道不和谐。如果您可以在一个机器人中注册两个函数,那么您所要做的就是设置一个像Running = False 这样的全局变量并在该循环中使用while Running:
猜你喜欢
  • 2021-10-26
  • 2021-02-01
  • 2018-02-25
  • 2023-02-14
  • 2021-09-28
  • 2022-01-03
  • 1970-01-01
  • 2021-11-16
  • 1970-01-01
相关资源
最近更新 更多