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

时间:2018-01-18 11:55:17

标签: python bots discord

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

代码:

@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 :(得分:0)

您只需在邮件事件中运行此操作:

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

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

<强>伪代码:

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

答案 1 :(得分: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)
相关问题