如何停止不和谐机器人对自身/所有其他机器人的响应 [Python 3.6 中的不和谐机器人]

Luk*_*kim 0 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) 
Run Code Online (Sandbox Code Playgroud)

Luk*_*kim 6

没关系,我终于想通了。对于那些有同样问题的人,你必须添加

if message.author == client.user:
        return
    if message.author.bot: return
Run Code Online (Sandbox Code Playgroud)

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

@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)
Run Code Online (Sandbox Code Playgroud)