discord bot sends infinite messages regardless of user input

Question:

I’m trying to make a discord bot respond when someone makes laughing remarks but it infinitely sends gifs whenever someone types anything
code is as follows

import os
import discord
import random
from discord.ext import commands
import keep_alive
Bot_Token = os.environ['Bot_Token']
bot = discord.Client()

@bot.event
async def on_ready():
  guild_count = 0

  for guild in bot.guilds:
    print(f"- {guild.id} (name: {guild.name})")
    guild_count = guild_count + 1

  print("AGOP_Bot is in " + str(guild_count) + " guilds.")

@bot.event
async def on_message(message):
  if message.content == "AGOP~hello":
    await message.channel.send("https://c.tenor.com/tTXwGpHrqUcAAAAC/summoned.gif")

  if message.content == "Lmao" or "Lol" or "lmao" or "lol" and message.author.id != bot_id:
response_funny = ["https://c.tenor.com/mUAgLfICUC0AAAAC/i-didnt-get-the-joke-abish-mathew.gif","https://c.tenor.com/zdoxFdx2wZQAAAAd/not-funny-joke.gif","https://i.pinimg.com/originals/f5/53/97/f55397a7de1c82b37d6d62e655a0e915.gif","https://jutsume.com/images2/2022/04/16/is-this-some-peasant-joke-meme.png","https://c.tenor.com/FnASqUdvJH4AAAAC/whats-so-funny-john.gif"]
    await message.channel.send(random.choice(response_funny))

bot.run(Bot_Token)
keep_alive.py
Asked By: destructer77

||

Answers:

import os
import discord
import random
from discord.ext import commands
# import keep_alive
Bot_Token = os.environ['Bot_Token']
bot = discord.Client()
...
@bot.event
async def on_message(message):
  if message.content == "AGOP~hello":
    await message.channel.send("https://c.tenor.com/tTXwGpHrqUcAAAAC/summoned.gif")

  if message.content in ("Lmao" or "Lol" or "lmao" or "lol") and message.author.id != bot.user:
    response_funny = ["https://c.tenor.com/mUAgLfICUC0AAAAC/i-didnt-get-the-joke-abish-mathew.gif","https://c.tenor.com/zdoxFdx2wZQAAAAd/not-funny-joke.gif","https://i.pinimg.com/originals/f5/53/97/f55397a7de1c82b37d6d62e655a0e915.gif","https://jutsume.com/images2/2022/04/16/is-this-some-peasant-joke-meme.png","https://c.tenor.com/FnASqUdvJH4AAAAC/whats-so-funny-john.gif"]
    await message.channel.send(random.choice(response_funny))

bot.run(Bot_Token)
# keep_alive.py

In addition to some formatting, I changed some of the variables for the API calls. I commented out the keep_alive.py as I assume you are using that to keep your code hosted on Repl.it or something, and you can just comment it back in. I was also able to get this code to work with my bot and execute as you want.

Answered By: MacItaly

Your if statement is messing you up. This doesn’t mean what you think it means:

if message.content == "Lmao" or "Lol" or "lmao" or "lol"

It’s checking to see if message.content equals "Lmao", but after that it’s not checking the others. It’s evaluating them as a boolean. In Python, if I say if "Hello", it equals True. Therefore, "Lol", "lmao" and "lol" all equal True.

Here’s a way to do it that will produce the correct result:

if message.content in ["Lmao", "Lol", "lmao", "lol"]

Now if message.content is in the list, it’ll send a gif, but not otherwise.

Answered By: MillerTime
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.