如何阻止不和谐的bot对自身/所有其他bot做出响应[Python 3.6中的Discord Bot] [英] How to stop discord bot respond to itself/all other bots [Discord Bot in Python 3.6]

查看:96
本文介绍了如何阻止不和谐的bot对自身/所有其他bot做出响应[Python 3.6中的Discord Bot]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程和学习非常陌生.我觉得做一个不和谐的机器人是学习的好方法,而且我很喜欢它,只是有点卡住了.所以我的机器人是私人的,在我们的不和谐服务器中有个玩笑,每当用户发送"k"时,所有机器人都会以"k"响应.在ATM上,我们有我朋友的私人机器人Dyno,也希望是我的.我执行了所有代码,除了命令和答案相同外,我的机器人只向服务器发送了"k"垃圾邮件,直到我关闭了它,如何停止它?

I am very new to programming and just learning. I figured making a discord bot is a good way to learn and I'm enjoying it, I'm just a little stuck. So my bot is private and there's a running joke in our discord server that whenever a user sends "k" all bots respond with "k". ATM we have Dyno, my friend's private bot and hopefully mine. I got all my code working except because the command and the answer is the same my bot just spams the server with "k" until I shut down the bot, how do I stop it?

代码:

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

    await client.process_commands(message) 

推荐答案

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

It is ok, I finally figured it out. To those having the same issue you have to add

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

代码,它可以正常工作,因此我的现在看起来像这样:

to the code and it works so mine looks like this now:

@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)

这篇关于如何阻止不和谐的bot对自身/所有其他bot做出响应[Python 3.6中的Discord Bot]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆