Search code examples
pythondiscorddiscord.py

Bot responding multiple times to a message based on the amount of trigger words discord.py


Basically I want the bot to only respond to one trigger at a time.

For example, if I ping the bot and also put in a trigger word, it will respond twice. Same goes for if I send multiple trigger words it will also respond back with however many trigger words there were. I'm not sure where I'm going wrong or how to make it stop once it sees one trigger word or when it gets pinged.

banned = ['x', 'y', 'z']
insults = ['x', 'y', 'z']
mentions = ["x", "y", "z"]

@client.event
async def on_ready():
  await client.change_presence(status=discord.Status.online, activity=discord.Game('x'))
  print('Bot is ready')
  print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if client.user.mentioned_in(message):
      if message.mention_everyone:
        return
      else:
        await message.channel.send(random.choice(mentions))
    for word in banned:
      if word in message.content.lower():
        await message.channel.send(random.choice(insults))

Solution

  • You can break out of the loop after sending the insult. Or you can use the any() function.

    To prevent responding to both the ping and trigger words, return from the function after responding to the ping.

    @client.event
    async def on_message(message):
        if message.author == client.user:
            return
        if client.user.mentioned_in(message):
            if not message.mention_everyone:
                await message.channel.send(random.choice(mentions))
            return
        if any(word in message.content.lower() for word in banned):
            await message.channel.send(random.choice(insults))