首页 > 解决方案 > discord.Client 是否与 discord.py 的 discord.ext.commands 兼容?

问题描述

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

标签: pythondiscord.py-rewrite

解决方案


You don't need discord.Client. If you are already using discord.ext.commands, you can do it as follows.

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 is called when a Message is created and sent. discord.Intents.messages must be enabled or you use intents = discord.Intents.all() to enable all Intents


推荐阅读