gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:47:19 24 4
gpt4 key购买 nike

标题真的很难用语言表达。基本上我有一个名为 #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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com