Search code examples
pythonbotsdiscord

How to stop Discord bot respond to itself/all other bots?


Because the command and the answer are the same my bot just spams the server with "k" until I shut it down. How do I stop it?

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) 

Solution

  • 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 to detect if the message author is this bot, or another bot. 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)