python - discord.py 发送到多条消息

python - discord.py sends to many messages

我正在构建一个 discord 机器人,我将其设置为响应包含“_hello”的消息,但作为响应,它会发送 1 到 3 条始终相同的消息。

@client.event
async def on_message(message):
    if '_hello' in message.content.lower():
        print('message received')
        words = message.content.split(' ')
        if len(words) < 3:
            await message.channel.send('incorect syntax')
        else:
            name = words[1]
            userMessage = ""
            for word in words[2:]:
                userMessage = userMessage + word + ' '

            await message.channel.send('hello: ' + name + "\nwith message: " + userMessage)

导致此问题的原因可能是不小心 运行多次启动机器人。这是一个小故障,可能发生在某些 IDE 上,例如 PyCharm if you close the application while your bot is running. This happens to me sometimes and the only fix I really found that works is changing your bot's token in the Discord Developer Portal 并将新令牌放入您的 运行 行。尽管这很烦人,但这是由于机器人被 运行 多次,如果您在它发生时关闭应用程序,您将无法在关闭应用程序后结束代码。

祝你有美好的一天!如果这不起作用,您可以在 discord killrebeest#7187 上与我联系或回复此答案。编码愉快!

当程序多次 运行 时,有时会发生这种情况。检查你的任务管理器并检查你是否只有一个 python 程序 运行