discord.py

Discord bot has suddenly stopped listening to commands or responding to appropriate messages


Until maybe a few weeks ago my discord bot had been responding appropriately to messages people send, it has a function where there is a chance when certain individuals speak there is a chance it responds with a picture, as well as it has a command that it is not responding to. I have a sort of logging where the bot prints to console when it attempts to perform these and it doesn't even appear to be trying. the bot does have an on ready and a scheduled event that both appear to be working with no problem. code looks like this:

from discord.ext import commands, tasks
import discord
import re
import subprocess
import datetime
import random
import json


BOT_TOKEN = "haha not today"

intents = discord.Intents.all()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)

@bot.event
async def on_ready():
    print("Hello, here for 40k Events!")
    post_events.start()

@bot.command()
async def getlist(ctx):
    print("updating events list")
    with open("EventsSchedule.txt") as f:
                t = f.read()

    eventurls = re.findall(r'"eventurl":"[^"]*"', t)
    locations = re.findall(r'"location":"[^"]*"', t)
    dates = re.findall(r'"date":"[^"]*"', t)
    allPlayers = re.findall(r'"players":"[^"]*"', t)
    allRounds = re.findall(r'"rounds":"[^"]*"', t)
    eventNames = re.findall(r'"name":"[^"]*"', t)
    
    i = 1
    while i < len(eventurls):
        name = eventNames[i-1]
        name = name[8:-1]
        date = dates[i-1]
        date = date[8:-1]
        location = locations[i-1]
        location = location[12:-1]
        eventurl = eventurls[i-1]
        eventurl = eventurl[12:-1]
        # players = allPlayers[i-1]
        # players = players[10:-1]
        # rounds = allRounds[i-1]
        # rounds = rounds[10:-1]

        await ctx.send(name + '\n' + date + '\n' + location + '\n' + eventurl)
        print(date + " : " + str(i))
        i += 1

@tasks.loop(time=datetime.time(hour=16, minute=0))
async def post_events():
    print("updating events list")
    subprocess.run(["node", "scraper.js"], capture_output=True, text=True, check=True)

    with open('guilds.json', 'r') as g:
        guildsList = json.load(g)
    for guild in bot.guilds:
        if str(guild.id) in guildsList['guilds']:
            channelId = guildsList['guilds'][str(guild.id)]["channel"]
            channel = bot.get_channel(int(channelId))
            await channel.purge()

            with open("EventsSchedule.txt") as f:
                t = f.read()

            eventurls = re.findall(r'"eventurl":"[^"]*"', t)
            locations = re.findall(r'"location":"[^"]*"', t)
            dates = re.findall(r'"date":"[^"]*"', t)
            allPlayers = re.findall(r'"players":"[^"]*"', t)
            allRounds = re.findall(r'"rounds":"[^"]*"', t)
            eventNames = re.findall(r'"name":"[^"]*"', t)

            i = 1
            while i < len(eventurls):
                name = eventNames[i-1]
                name = name[8:-1]
                date = dates[i-1]
                date = date[8:-1]
                location = locations[i-1]
                location = location[12:-1]
                eventurl = eventurls[i-1]
                eventurl = eventurl[12:-1]
                # players = allPlayers[i-1]
                # players = players[10:-1]
                # rounds = allRounds[i-1]
                # rounds = rounds[10:-1]

                await channel.send(name + '\n' + date + '\n' + location + '\n' + eventurl)
                print(date + " : " + str(i))
                i += 1

@bot.event #bother that guy
async def on_message(message):
    channel = message.channel
    if message.author.id == hidingpersonalinfo:
        gamble = random.randint(1, 100)
        print("someone rolled a: " + str(gamble))
        if gamble <= 10: 
            print("Get the bottle")
            await channel.send(file=discord.File('spray_bottle.gif'))
        else: await bot.process_commands(message)
    await bot.process_commands(message)

@bot.event #viva la resistance
async def on_message(message):
    channel = message.channel
    if message.author.id in (personalinfooffearlessleaders):
        gamble = random.randint(1, 100)
        print("our glorious leader rolled a: " + str(gamble))
        if gamble <= 5: 
            print("Throw the book")
            await channel.send(file=discord.File('book_throw.gif'))
        else: await bot.process_commands(message)
    await bot.process_commands(message)

@bot.event #birthday announcements
async def on_message(message):
    channel = message.channel
    if message.author.id in (includedoneotherandnotthebot):
        if ("happy birthday") in message.content.lower(): 
            print("happy birthday!")
            await channel.send("Happy Birthday!")
            await channel.send(file = discord.File('birthday_cake.gif'))
        else: await bot.process_commands(message)
    await bot.process_commands(message)


bot.run(BOT_TOKEN)

based off of some other posts i had found for similar problems i tried a few fixes/ made sure i already had them in place.

i had my intents set to all before, and now ive also added a statement to specifically set the Intents.message_content to True. ive also checked in the portal and message intents is set to true.

i had a bot.process_commands(message) at the end of my on message events and have also added them in at some return statements that i though might be causing an issue.

while in the portal i did briefly see a rate limited message on my bot but have not seen the message since, unsure if its resolved or if that might be connected, but im unsure how to test it.

dev portal pic


Solution

  • Should be same as issue here (I didn't flag the problem as a duplicate because it wouldn't let me since the issue I'm linking doesn't have an accepted answer): In discord.py when I use more than 1 `on_message` it does not works, only the last one works

    You have too many @bot.event 's using on_message. The last on_message will overwrite all the previous ones. I'd like to know why you used so many of them in the first place, considering you only need an if statement to check for messages you want.

    Merge them like so:

    @bot.event #Register events for the bot
    async def on_message(message): #Register a 'on_message' event
        channel = message.channel #Since you need the channel for all of your checks, you only need it once.
    
    
        if message.author.id == hidingpersonalinfo: #Your first on_message event code
            gamble = random.randint(1, 100)
            print("someone rolled a: " + str(gamble))
            if gamble <= 10: 
                print("Get the bottle")
                await channel.send(file=discord.File('spray_bottle.gif'))
            else: await bot.process_commands(message)
    
    
        elif message.author.id in (personalinfooffearlessleaders): #Second one
            gamble = random.randint(1, 100)
            print("our glorious leader rolled a: " + str(gamble))
            if gamble <= 5: 
                print("Throw the book")
                await channel.send(file=discord.File('book_throw.gif'))
            else: await bot.process_commands(message)
    
    
        elif message.author.id in (includedoneotherandnotthebot): #Third one
            if ("happy birthday") in message.content.lower(): 
                print("happy birthday!")
                await channel.send("Happy Birthday!")
                await channel.send(file = discord.File('birthday_cake.gif'))
            else: await bot.process_commands(message)
    
    
        await bot.process_commands(message) #Your checks all end with this, so if you just call it once at the end it should make no difference.