Skip to content
Advertisement

discord bot sends infinite messages regardless of user input

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement