【问题标题】:Attribute Error: 'Client' object has no attribute 'command'属性错误:“客户端”对象没有属性“命令”
【发布时间】:2020-10-25 14:47:45
【问题描述】:
from discord.ext import commands

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

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

我对 Python 非常陌生,目前正在学习如何使用 discord.py rewrite 编写一个不和谐机器人。我从一个名叫 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'

【问题讨论】:

    标签: python pycharm attributeerror discord.py-rewrite


    【解决方案1】:

    据我所知,如果您希望继续使用 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,因为大多数其他人也使用它,那么在 stackoverflow 上找到帮助要容易得多。但是,回到你想使用@client.command 我不知道。但是,如果您想切换到 @bot.command,那么我相信您可以在 google 或 stackoverflow 上查找您想要执行的操作,然后查找直到找到 @bot.command 的内容,这不应该那么长。

    【讨论】:

      猜你喜欢
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 2019-10-15
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      相关资源
      最近更新 更多