Python discord.py消息被发送多次,每次增加1

Python discord.py消息被发送多次,每次增加1,python,bots,discord,discord.py,Python,Bots,Discord,Discord.py,真的很难用一个词来形容一个标题。基本上,我有一个名为#start的函数,当在特定时间调用该函数时,会在discord上发送一条消息,要求我修改直到(再次)特定时间。但当我回到“不和谐”的话题时,我发现这个信息已经被发送了两次。然后我尝试执行#start命令,它会发送相同的消息3次。如果我输入“重新开始”,它会出现4次。这是我的代码: if hour == 14: await bot.send_message(message.channel, "<@258621320898543616

真的很难用一个词来形容一个标题。基本上,我有一个名为#start的函数,当在特定时间调用该函数时,会在discord上发送一条消息,要求我修改直到(再次)特定时间。但当我回到“不和谐”的话题时,我发现这个信息已经被发送了两次。然后我尝试执行#start命令,它会发送相同的消息3次。如果我输入“重新开始”,它会出现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
命令是否正在运行

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")

问题是我键入的整个代码(以及一天中不同时间的许多其他while循环和if语句)都在一个
if message.content.startswith('#start'):
语句中。而这个if语句位于一个
async def on_message()
coroutine中。我是这样做的如果我在一天中的任何时候都输入“开始”,它会回复为该时间设置的任何任务,并且这样做会阻止我使用任何与
消息相关的内容
是否有任何方法使
等待消息()
使用用户ID来确定消息是否已被接收?@brandone您可以使用消息上的
轻松完成此操作,请参见上面的示例。如果该命令只是对当时的任务进行响应,那么为什么会有一个循环呢?因此,我只需要让它不要删除一条写着“开始”的消息
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")