0

I'm actually trying to make a bot with discord.py for me and my friends, just for fun and practice python, but, when i try to make a bot command, that can be use with prefix + [name_of_the_command], it doesn't work.

import discord
from discord.ext import commands

intents = discord.Intents(messages=True, guilds=True)
intents.message_content = True
intents.messages = True
intents.guild_messages = True

bot = commands.Bot(command_prefix="$", intents=intents)

@bot.event
async def on_ready():
    print(f'Logged on as {bot.user}!')

@bot.event
async def on_message(message):
    print(f'Message from {message.author}: {message.content}')
    
#     if message.author == bot.user :   
#       return
    
#     if message.channel == discord.DMChannel:
#         return

        
@bot.command()
async def ping(ctx):
    print("test")
    await ctx.channel.send("pong")


bot.run('MY_BOT_TOKEN')

I search on lot's of tuto, website, forum ... to see if there is an explaination but i didn't found, so expect someone could tell me what i make wrong to this simple thing doesn't work; idk if it's change something but i'm using python 3.11.1 and the last update of discord.py (i just dl discord.py today)

That what i get in my cmd, as you can see, i have the message content, but after, that make nothing ty and have a nice day

GreGre
  • 1
  • 1
  • Hey there, your post seems to be a duplicate of https://stackoverflow.com/questions/49331096/why-does-on-message-stop-commands-from-working, look there for your answer – Raymus Dec 14 '22 at 08:28

1 Answers1

0

You have to process the command in the on_message event by using process_commands:

import discord
from discord.ext import commands

intents = discord.Intents(messages=True, guilds=True)
intents.message_content = True
intents.messages = True
intents.guild_messages = True

bot = commands.Bot(command_prefix="$", intents=intents)

@bot.event
async def on_ready():
    print(f'Logged on as {bot.user}!')

@bot.event
async def on_message(message):
    print(f'Message from {message.author}: {message.content}')
    await bot.process_commands(message)
#     if message.author == bot.user :   
#       return
    
#     if message.channel == discord.DMChannel:
#         return

        
@bot.command()
async def ping(ctx):
    print("test")
    await ctx.channel.send("pong")


bot.run('MY_BOT_TOKEN')
vlone
  • 33
  • 7