pythondiscord.py

Discord.py commands aren't responding


  1. I am trying to make a Discord bot using discord.py.

  2. I created two commands.

  3. When I sent ?hello bot didn't respond.

Here's the code:

import discord
import asyncio
import ast
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix='?', intents=intents)
    
@bot.event
async def on_ready():
    print(f'logged in baby {bot.user}')

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    if 'nigg' in message.content or 'wigg' in message.content:
        author = message.author
        guild = message.guild
        await author.add_roles(discord.utils.get(guild.roles, name='Racist'))
        await message.delete()

@bot.command()
async def calculate(ctx, *, expression: str):
    await ctx.send(f'{expression} = {eval(expression)}')

@bot.command()
async def hello(ctx):
    print('hi')
    await ctx.send('hello guys')

bot.run('token')

And yes – the bot was running while I typed that command.

I tried using @bot.command() and expected bot to respond but nothing happened.


Solution

  • I found that adding await bot.process_commands(message) in the on_message() worked.

    @bot.event
    async def on_message(message):
        if message.author == bot.user:
            return
        await bot.process_commands(message)
        if 'nigg' in message.content or 'wigg' in message.content:
            author = message.author
            guild = message.guild
            await author.add_roles(discord.utils.get(guild.roles, name='Racist'))
            await message.delete()
    

    It will check if there are comments in that message.