如何停止discord bot响应自身/所有其他bot[Python3.6中的discord bot]

2024-05-19 07:56:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编程很陌生,只是学习而已。我觉得做一个不和谐的机器人是一个很好的学习方式,我很享受,我只是有点卡住。所以我的机器人是私有的,在我们的不和谐服务器上有一个笑话,每当用户发送“k”时,所有的机器人都会用“k”响应。我们有迪诺,我朋友的私人机器人,希望是我的。我让我所有的代码工作,除了因为命令和答案是一样的我的机器人只是垃圾邮件服务器与“k”直到我关闭机器人,我该如何停止它?

代码:

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

    await client.process_commands(message) 

Tags: 答案代码用户命令服务器clientmessage编程
2条回答

没关系,我终于明白了。对那些有同样问题的人

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)

您只需在消息事件上运行此命令:

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

相关问题 更多 >

    热门问题