首页 文章

如何阻止discord bot响应自己/所有其他机器人[Python 3.6中的Discord Bot]

提问于
浏览
0

我是编程和学习的新手 . 我想制作一个不和谐的机器人是一个很好的学习方式,我很享受它,我只是有点卡住了 . 所以我的僵尸程序是私有的,我们的不和服务器中有一个正在运行的笑话,每当用户发送“k”时,所有机器人都以“k”响应 . ATM我们有Dyno,我朋友的私人机器人,希望我的 . 我得到了所有的代码,除了因为命令和答案是相同的,我的机器人只是用“k”垃圾邮件服务器,直到我关闭机器人,我该如何阻止它?

The code:

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

    await client.process_commands(message)

2 回答

  • 0

    您可以在您的消息事件上运行此操作:

    if(!message.author.user.bot) return; //not the exact code
    

    因为消息事件可以 return 用户,并且用户具有默认bot check . message.author 返回一个成员,因此您可以调用memberuser 对象,然后像我上面那样执行检查 .

    Pseudocode:

    If the author of the message (user) is a bot: return; 
    This will prevent an infinite loop from occuring
    
  • 0

    没关系,我终于明白了 . 对于那些具有相同问题的人,您必须添加

    if message.author == client.user:
            return
        if message.author.bot: return
    

    代码,它工作,所以我现在看起来像这样:

    @client.event
    async def on_message(message):
        # we do not want the bot to reply to itself
        if message.author == client.user:
            return
        if message.author.bot: return
        if message.content.startswith('k'):
            msg = 'k'.format(message)
            await client.send_message(message.channel, msg)
    

相关问题