首页 > 解决方案 > discord.py 命令没有响应

问题描述

代码:

import discord
from discord.ext.commands import Bot
from discord.ext import commands

bot = Bot(command_prefix = ".")


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



@bot.command(name="lol")
async def _lol(ctx):
    print("lelele")
    print(ctx.author)
    await ctx.send("lelele")
    

标签: pythonpython-3.xdiscord.py

解决方案


覆盖提供的默认值会on_message禁止运行任何额外的命令。要解决此问题,bot.process_commands(message)请在on_message.

import discord
from discord.ext.commands import Bot
from discord.ext import commands

bot = Bot(command_prefix=".")


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

    if message.content.startswith("hello"):
        await message.channel.send('hello!')
        
    await bot.process_commands(message)


@bot.command(name="lol")
async def _lol(ctx):
    print("lelele")
    print(ctx.author)
    await ctx.send("lelele")

为什么 on_message 使我的命令停止工作?


推荐阅读