pythondiscorddiscord.pybotspycord

Discord bot doesn't gets triggered on using commands


import os
import discord
from discord.ext import commands
from keep_alive import keep_alive
from replit import db


#setting up bot
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
intents.voice_states = True
intents.presences = True
bot = commands.Bot(intents=intents , command_prefix='!')


@bot.event
async def on_ready():
    print('We have logged in as {0.user}'.format(bot))
    await bot.change_presence(activity=discord.Game(name="your mom"))

   
@bot.command()
async def balance(ctx):
  await ctx.reply("your balance is $0")

@bot.event
async def on_message(message):
    msg = message.content.lower()
    if message.author == bot.user:
        return
    if msg.startswith('hello'):
      await message.channel.send('hello')

This is the code, and I have double checked everything. The bot is online and it does replies to Hello but when typing !balance it doesn't gets triggered at all. There is no error in console either. I even tried Try: to check for some hidden errors but none. I also checked dev portals and the bot does have all the required permissions.

what should I do?


Solution

  • It sounds like you just need to tell the bot to process the commands in your message. It does this by default, but you have overridden that with the on_message() event. All you need is to include bot.process_commands(message) in the on_message event.

    @bot.event
    async def on_message(message):
        await bot.process_commands(message)
        msg = message.content.lower()
        if message.author == bot.user:
            return
        if msg.startswith('hello'):
            await message.channel.send('hello')