为什么我的不和谐机器人发送垃圾邮件?

问题描述 投票:0回答:3

我之前发布过这个相同的机器人,感谢回应的人们。但是,虽然它最终变为现实并开启,但它开始发送垃圾信息的原因并不明显。我查看了拼写错误的代码,但找不到任何错误。这是代码:

import discord
from discord.ext.commands import bot
from discord.ext import commands
import asyncio
import time

Client = discord.Client()
client = commands.Bot (command_prefix = discord)

@client.event
async def on_ready() :
        print("Bepis machine fixed")

@client.event
   async def on_message(message) :
           if message.content == "bepis" :
                await client.send_message (message.channel, "bepis")



client.run("Censored Bot Token")

@ client.event之后是我需要帮助的地方。如果罚款这个时候还有底线!事实证明我在括号前击中了空格键并且不喜欢这样。非常感谢帮助,所以我可以继续添加到这个真棒机器人。

python bots discord spam-prevention
3个回答
1
投票

看起来你正在发送消息“bepis”以响应第一个,然后是每个消息“bepis” - 大概你的第一个响应将显示为输入提要的一个条目,它将触发一秒钟,等等。


0
投票

结果我没有使用适当的格式化我的机器人。每当你在不和谐服务器中说“bepis”时,机器人都会看到它,然后按照预期的方式说“bepis”,但是,由于我的格式不正确,机器人看到自己说“bepis”并作出回应,好像别人在说“bepis”。

旧线:

if message.content == "bepis" :
    await client.send_message(message.channel, "bepis")

新线:

if message.content.startswith('bepis'):
    await client.send_message(message.channel, "bepis")

因此,如果您正在制作机器人,请确保使用正确的格式!


0
投票

您似乎已经知道问题是什么,来自您发布的this answer。但是你的解决方案远远不能解决问题。

只要将新消息发送到机器人可以访问的任何地方,就会调用on_message;因此,当您在discord中键入“bepis”时,机器人回复“bepis”,然后机器人发送的消息进入on_message,机器人重新回复“bepis”,依此类推......

简单的解决方案是检查消息的作者是否是任何机器人帐户,或者如果您想要,如果消息的作者是您的机器人。

from discord.ext import commands

client = commands.Bot(command_prefix=None)

@client.event
async def on_ready():
    print("Bepis machine fixed")

@client.event
async def on_message(message):
    # or `if message.author.bot:`   # which checks for any bot account
    if message.author == client.user:
        return
    if message.content == "bepis":
        await client.send_message(message.channel, "bepis")

client.run("Token")

注意:我还解决了许多其他问题,例如多个未使用的导入,另一个Client和缩进。

并且FYI,command_prefix仅在功能命令处理命令时使用。当你使用on_message它没有用,这意味着你可以将它设置为None。

© www.soinside.com 2019 - 2024. All rights reserved.