python - Discord.py 消息被发送多次,每次增加 1

标签 python bots discord discord.py

标题真的很难用语言表达。基本上我有一个名为 #start 的函数,当在特定时间调用它时,它会在不和谐上发送一条消息,要求我修改直到(再次)特定时间。但当我回到discord时,发现这条消息已经发送了两次。然后我尝试执行 #start 命令,它发送相同的消息 3 次。如果我再次输入 #start,它会出现 4 次。这是我的代码:

if hour == 14:
   await bot.send_message(message.channel, "<@258621320898543616> Why don't you try some science revision now?")
   science = random.choice(sciences) 
   asyncio.sleep(0.5)
   await bot.send_message(message.channel, "<@258621320898543616> lemme see, how about " +science+"? Look over some of that")
   asyncio.sleep(1)
   await bot.send_message(message.channel, "you can take a break at 3:00")
while hour >= 14 and hour < 15:
   msg = await bot.wait_for_message(timeout=3, author=message.author)
   if msg:
      await bot.delete_message(msg)
   hour = int(time.strftime("%H"))

在我第四次输入 #start 后,它会抛出一个错误:

discord.errors.NotFound: NOT FOUND (status code: 404): Unknown Message

不确定代码有什么问题或者如何阻止它发生。请帮忙?

最佳答案

添加一个全局值,指示#start 命令是否正在运行。

from discord.ext.commands import Bot

bot = Bot(command_prefix='#')
start_running = False

@bot.event
async def on_message(message):
    global start_running
    if message.content.startswith('#start'):
        if not start_running:                
            start_running = True
            # do stuff
            start_running = False

bot.run("token")

关于python - Discord.py 消息被发送多次,每次增加 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53249712/

相关文章:

java - 在 JAVA 上与 Telegram BOT 进行对话

javascript - 预期收到 SlashCommandChannelOption 构建器,但未定义

c# - Azure Bot Framework FormFlow 复杂表单

javascript - 开 Jest 测试不和谐机器人命令

python - 基于Python的discord机器人的随机数生成器

python - 如何使用python从mysql表数据写入excel

python - block 中的 Django 模板翻译是不可能的

python - 使用 Selenium 发送多个 tab 键

node.js - 等待 Facebook Messenger 机器人的短信回复

javascript - Discord js 震耳欲聋的用户