python - 每次我发送消息时,我的不和谐机器人都会不断发送垃圾邮件
问题描述
我的目标是让我的机器人每次都发送一条消息,但在我发送一条消息后,我的机器人会发疯并开始发送垃圾邮件。
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")
解决方案
您添加了一个中断,但on_message
只要有人发送消息,事件就会运行。在这种情况下,机器人会发送一条消息,这会导致无限循环。为了防止这种情况,您可以检查消息作者是否为机器人。此外,除非您正在做的不是问题中的代码,否则使用 while 循环是没有意义的。
@bot.event
async def on_message(message):
if message.author.bot:
return
await message.channel.send(f"random text")
推荐阅读
- python - 使用 Lambda-python 对 DynamoDB 表的 sed 识别响应
- visual-studio-code - VSCode Latex-workshop:onFileChange 没有 AutoBuild
- git - 找不到 GIT 日志漂亮格式的任何文件占位符
- python - reportlab SimpleDocTemplate - 设置具有可变行数的表格的固定高度
- c++ - 创建从 sql 到 Picture 的 blob 流
- amazon-web-services - AWS 中 Cassandra 的节点连接问题
- reactjs - RN - 在样式声明中使用道具
- drupal - Drupal 8,打印表单并将数据发送到数据库
- performance - StatelessWidget 与在性能方面返回 Widget 的函数
- python - 启动一个简单的批处理文件来运行 python 脚本然后终止 cmd 窗口