Search code examples
pythondiscorddiscord.pybots

My Discord bot doesn't send message when I type a command


I'm currently developing a Discord bot, but when I call the bot.process_command member function with several arguments, it does not respond and does not give an error message.

My code is as follows:

import discord  #Importation des paquets et des modules nécessaires au bon fonctionnement du bot
from discord.ext import commands

intents = discord.Intents(messages=True, guilds=True)#intents enabling

class MyClient(discord.Client): #Connection message
    async def on_ready(self):
        print(f'Logged on as {self.user}!')

bot = commands.Bot(command_prefix='$', intents=intents) #Setting command prefix

@bot.event #command function definition
async def on_message(message):
    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

client = MyClient(intents=intents) #starts the bot
client.run('TOKEN')

I am testing the bot in an empty server with only the general channel. I already checked the Discord developer portal; the message intent is enabled.

I'm on Ubuntu 23.04 with python 3.8.0 installed in my venv with only discord.py package

Why isn't my Discord bot responding?


Solution

  • You can remove client since bot can do everything client can, but easier. You can also use the bot.command() decorator to define commands easily. You only need to use bot.process_command() if you also have on_message().

    import discord
    from discord.ext import commands
    
    intents = discord.Intents.default()
    intents.message_content = True
    
    bot = commands.Bot(command_prefix="$",intents=intents)
    
    @bot.command("hello")
    async def hello(ctx):
       await ctx.send("Hello!")
    
    bot.run("TOKEN")