【问题标题】:Discord.py Commands not working because of a on_message eventDiscord.py 命令因 on_message 事件而不起作用
【发布时间】:2020-10-30 21:22:20
【问题描述】:

我的代码有问题。它不运行任何命令 :( 我知道问题出在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*")

【问题讨论】:

标签: python discord.py


【解决方案1】:

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)

【讨论】:

  • 没看懂await bot.process_commands(message)之后该怎么办?
  • bot.process_commands(message) 是您需要做的所有事情。有了它,你的命令就可以正常工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 2018-06-10
  • 2021-07-09
  • 2021-02-12
  • 1970-01-01
  • 1970-01-01
  • 2021-01-17
相关资源
最近更新 更多