由于on_message事件,Discord.py命令不起作用

时间:2020-10-30 12:11:17

标签: python discord.py

我的代码有问题。它不运行任何命令:(我知道问题出在on_message部分,但我不知道如何解决它。

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='//')
bdl = open('*Filelocation*', 'r')
badwordslist = bdl.read().split()
bdl.close()
   
@bot.command()
async def hello(context):
    print('it works')
    context.send('Hello!')

@bot.event
async def on_connect():
    print('Connected.')
    
@bot.event
async def on_ready():
    print('READY.')
    
@bot.event
async def on_message(message):
    for badword in message.content.lower().split():
        if badword in badwordslist:
            await message.channel.send(f'Hey! {message.author.mention} ! Don\'t be rude!')
            print(f'{message.author} Said A Bad Word.')
            break
        else:
            return
    
bot.run("*Token*")

1 个答案:

答案 0 :(得分:0)

on_message事件会阻止其他命令。如果要防止这种情况,则应使用await bot.process_commands(message)

处理命令
@bot.event
async def on_message(message):
    for badword in message.content.lower().split():
        if badword in badwordslist:
            await message.channel.send(f'Hey! {message.author.mention} ! Don\'t be rude!')
            print(f'{message.author} Said A Bad Word.')
            break
    await bot.process_commands(message)