Search code examples
python-3.xdiscorddiscord.py

Discord.py not responding to @client.event commands


I am not getting a "ping" response with this code. It was working before, but I am not sure what changed. There are no errors on my end, just no response.

Any feedback is appreciated.

import os
import random
import discord
from dotenv import load_dotenv
from discord.ext import commands

load_dotenv()
PREFIX = os.getenv("PREFIX")
TOKEN = os.getenv("TOKEN")
intents = discord.Intents().all()
bot = commands.Bot(command_prefix=PREFIX, intents=intents)


@bot.event
async def on_message(message):
    if message.author == bot.user:  # tells the bot not to respond to itself
        return

@bot.event  # ping-with-latency
async def on_message(message):
    if message.content.startswith(PREFIX + 'ping'):
        await message.channel.send(f'pong! {bot.latency}ms')

@bot.event
async def on_ready():  # display if online/ready
    print("Bot is ready and logged in as {0.user}!".format(bot))

# run bot on server
bot.run(TOKEN)

I have checked all permissions and privileged gateway intents. I know I could be using client.command, but that also doesnt work.


Solution

  • You're defining two different callbacks for these events - this is probably the problem. Just put the author check in the main on_message.

    @bot.event
    async def on_message(message):
        if message.author == bot.user:  # tells the bot not to respond to itself
            return
    
        if message.content.startswith(PREFIX + 'ping'):
            await message.channel.send(f'pong! {bot.latency}ms')