简体   繁体   中英

Discord.py, bot.event works but not bot.command

So if I have implemented bot.event, bot.command doesn't work but if I comment or remove bot.event, bot.command works fine.

In here, bot.command doesn't work but bot.event does:

# bot.py
import os
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

bot = commands.Bot(command_prefix='dedmu ')


@bot.event
async def on_ready():
    print(f'{bot.user.name} has connected to Discord!')


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

    if message.content.lower() == 'testt':
        await message.channel.send('it works')


@bot.command(name='test')
async def test(ctx, arg):
    await ctx.send(arg)


bot.run(TOKEN)

If i comment both functions with bot.event, bot.command works perfectly. What's wrong with it? :<

Because you have an on_message event. When you have on_message event in your bot, you need to add await bot.process_commands(message) at the last line of your on_message event code. Otherwise it will block commands from working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM