Attribute Error: 'Client' object has no attribute 'command'

Attribute Error: 'Client' object has no attribute 'command'

from discord.ext import commands

client = commands.Bot(command_prefix='{')

@client.command()
async def ping(ctx):
    await ctx.send("Pong!")

我是 Python 的新手,我目前正在学习如何使用 discord.py 重写来编写不和谐机器人程序。我遵循了一个名叫 Lucas 的 YouTuber 的代码。这是他的确切代码。他的似乎有效,但出于某种原因,我的 PyCharm 仍然说客户端没有属性命令。有人可以教我如何解决吗?

这是错误

Traceback (most recent call last):
  File "C:/Users/danie/PycharmProjects/Discord Tutorial/bot.py", line 93, in <module>
    @client.command()
AttributeError: 'Client' object has no attribute 'command'

据我所知,如果您想继续使用 discord.py 的 client 部分,那么您应该尝试使用它的 on_message 部分,例如:

async def on_message(message):
    if message.content.startswith('{ping'):
        await message.channel.send('pong')

但是如果你想要一个相对简单的替代方案,(至少它对我来说更有意义)你可以尝试 @bot.command 示例:

import discord
from discord.ext import commands
TOKEN = ''
bot = commands.Bot(command_prefix='{')

@bot.command()
async def ping(ctx):
    await ctx.send('pong')

@bot.command 的一大优点是更容易获得输入。

如果你想知道用户在执行命令后说了什么,那么你可以这样做。

@bot.command()
async def ping(ctx, *, variableName):
    await ctx.send(f'You said {variableName}')

如果你想在他们搞砸时收到错误消息,你可以这样做

@bot.command()
async def ping(ctx, *, variableName):
    await ctx.send(f'You said {variableName}')
@ping.error
async def ping_error(ctx, error):
    ctx.send("Please enter something after the command")

根据我的经验,如果您使用 @bot.command,则在 Whosebug 上找到帮助要容易得多,因为大多数其他人也使用它。但是,回到你想使用 @client.command 我不知道。但是,如果您想切换到 @bot.command,那么我确定您可以在 google 或 Whosebug 上查找您想要执行的操作,然后查找直到找到 @bot.command 哪个应该不会那么长。