Discord.py:命令不起作用,但事件起作用,并且没有错误

Discord.py: Commands don't work but events work, and no errors

我制作了一个 discord 机器人,但是当我 运行 它时,事件工作正常,所有齿轮都正确加载。但是命令不起作用,当我使用命令时,它不会显示输出。

我在版本 2.0.0

上使用了 discord.py

代码如下:

import config
import discord
from discord.ext import commands
import random
import json
import os

client = commands.Bot(command_prefix=config.PREFIX)
client.remove_command('help')

@client.event
async def on_ready():
    print('ONLINE')

@client.command()
async def ping(ctx):
    #This is not a actual command I  have but it wouldn't work anyway
    print("Pong")

client.run(config.TOKEN)

Discord.py 2.0 开始,您必须具有消息意图才能使正常命令正常工作。

在此示例中,选择了默认意图,其中包括 message_content 意图。

import config

from discord import Intents
from discord.ext import commands

client = commands.Bot(command_prefix=config.PREFIX, intents=Intents.default(), help_command=None)

@client.event
async def on_ready():
    print('ONLINE')

@client.command()
async def ping(ctx):
    #This is not a actual command I  have but it wouldn't work anyway
    print("Pong")

client.run(config.TOKEN)