Discord机器人on_message事件不会使命令正常运行*

问题描述

曾经浏览过以解决此问题,并实际上尝试将bot.process_commands(message)插入我的代码中,但似乎无济于事。到目前为止,这是代码。只有on_message事件起作用,而命令无效。

import discord
from discord.ext import commands
from discord.ext.commands import CommandNotFound
 
bot = commands.Bot(command_prefix='/')

@bot.event
async def on_message(message):
    if bot.user == message.author:
        return
    message.content = message.content.lower()
    if message.content.startswith('hello') and str(message.channel) != 'images':
        await  message.channel.send('Hi')
    if str(message.channel) == 'images' and message.content != '':
        await message.channel.purge(limit=1)
    
    await bot.process_commands(message)
    

@bot.event
async def on_command_error(ctx,error):
    if isinstance(error,CommandNotFound):
        return
    raise error

@bot.command()
async def Hamdii (ctx,arg):
    await ctx.send(arg)

    
bot.run('token') 

解决方法

解决此问题的一种方法是检查on_message()事件中的命令,如下所示:

@bot.event
async def on_message(message):

    if bot.user == message.author:
        return

    message.content = message.content.lower()

    if message.content.startswith('hello') and str(message.channel) != 'images':
        await  message.channel.send('Hi')

    if str(message.channel) == 'images' and message.content != '':
        await message.channel.purge(limit=1)

    if message.content.startswith('/hamdii'):
        await message.channel.send(message.content[7:])

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...