discord.Client 与 discord.ext.commands 兼容 discord.py 吗?

Is discord.Client compatible with discord.ext.commands for discord.py?

我有一个非常幼稚的机器人,我想添加一个 automod 功能。但是,我的机器人是用 discord.ext.commands 编写的,要扫描所有消息,我需要 discord.Client(我认为)。我不确定它们是否可以同时 运行,但它们兼容吗?

您不需要 discord.Client。如果您已经在使用discord.ext.commands,您可以按如下方式进行。

import discord
from discord.ext import commands


intents = discord.Intents.all()

bot = commands.Bot(command_prefix=".", intents=intents)


@bot.event
async def on_ready():
    print(f"Bot is ready")
    await bot.change_presence(status=discord.Status.online, activity=discord.Game(name="example"))

@bot.event
async def on_message(message):
    forbidden_word = "word"
    if forbidden_word in message.content:
        await message.delete()

@bot.command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member):
    await member.kick()
    await ctx.send(f"Member {member} has been kicked")


bot.run("token")

on_message 在创建和发送消息时调用。 discord.Intents.messages 必须启用或者您使用 intents = discord.Intents.all() 来启用所有 Intents