多次发送discord.py邮件,每次增加1

时间:2018-11-11 14:30:55

标签: python bots discord discord.py

很难用标题表达。基本上,我有一个名为#start的函数,该函数在特定时间调用时会发送不一致的消息,要求我修改(直到)特定时间。但是当我回到不和谐状态时,事实证明消息已发送两次。因此,然后尝试执行#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"))

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

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

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

1 个答案:

答案 0 :(得分:1)

添加一个全局值,该值指示#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")