首页 > 解决方案 > 每次我发送消息时,我的不和谐机器人都会不断发送垃圾邮件

问题描述

我的目标是让我的机器人每次都发送一条消息,但在我发送一条消息后,我的机器人会发疯并开始发送垃圾邮件。

from discord.ext import commands
from discord import *
from discord.utils import *


bot = commands.Bot(command_prefix=".")

@bot.event
async def on_ready():
    print("The bot is online")
    await bot.change_presence(status=discord.Status.online, activity=discord.Game('.bothelp'))

@bot.event
async def on_message(message):
    repeat = True
    while repeat:
        await message.channel.send(f"random text")
        break

bot.run("Bot Token Censored")

标签: pythondiscorddiscord.py

解决方案


您添加了一个中断,但on_message只要有人发送消息,事件就会运行。在这种情况下,机器人会发送一条消息,这会导致无限循环。为了防止这种情况,您可以检查消息作者是否为机器人。此外,除非您正在做的不是问题中的代码,否则使用 while 循环是没有意义的。

@bot.event
async def on_message(message):
    if message.author.bot:
        return
    await message.channel.send(f"random text")

推荐阅读