清除命令在 2.0 discord.py 不起作用



在过去的两个小时里,我一直在处理这个问题,不明白为什么它不起作用,我启用了消息意图,我已经正确地建立了命令,我使用了正确的前缀和数量。我已经看过我的代码很多次了,但仍然看不到或不明白为什么什么都没发生,真的什么都没有发生,哈哈。

from dis import dis
from importlib.metadata import requires
from sys import prefix
from unicodedata import name
from click import command, pass_context
import discord
from discord.ext import commands
import json
import requests
import asyncio
with open('badwords.json', 'r') as f:
data = json.load(f)
intents = discord.Intents.default()

intents.members = True
intents.message_content = True

print(discord.__version__)
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'{bot.user.name} is Online!')
@bot.event
async def on_message(msg):
if msg.author != bot.user:
for text in data['words']:
if text in msg.content or text.upper() in msg.content or text.capitalize() in msg.content:
await msg.delete()
return

@bot.command()
@commands.has_permissions(manage_messages=True)
async def purge(ctx, limit: int):
await ctx.message.delete()
await asyncio.sleep(1)
await ctx.channel.purge(limit=limit)

一般来说,"on_message"事件会阻止所有其他命令运行。

从文件

覆盖默认提供的on_message将禁止运行任何额外的命令。要解决此问题,请在on_message的末尾添加一行bot.process_commands(message(。例如:

async def on_message(message):
# do some extra stuff here
await bot.process_commands(message)

默认的on_message包含对此协程的调用,但当您用自己的on_mesage覆盖它时,您需要自己调用它。

此处为原始答案

编辑:关于你的评论,我认为你在错误的地方添加了"process_commands"。如果我没有错的话,那么你的代码应该是这样的:

@bot.event
async def on_message(msg):
if msg.author != bot.user:
for text in data['words']:
if text in msg.content or text.upper() in msg.content or text.capitalize() in msg.content:
await msg.delete()
return
await bot.process_commands(message)