【问题标题】:Discord.py bot - ping command not workingDiscord.py bot - ping 命令不起作用
【发布时间】:2019-07-23 21:06:52
【问题描述】:

我刚刚开始使用不和谐的机器人,也没有很长时间使用 python。我正在制作一个货币机器人,货币为 ep,它跟踪用户的财富并将所有内容保存在 json 文件中。我之前也做过这个工作,但想用不同的方式来写它。

我最初的方式-

@client.event
async def on_message(message):
    if message.content.upper().startswith('EP.PING'):
        await client.send_message(message.channel, "Ping.")

我的(希望是更好的方式)-

@client.command()
async def ping():
    await client.say('Pong')

错误信息 -

File "f:/Python Programs/EP Bot/EP Bot V2.py", line 19, in <module>
    @client.command()
  File "F:\Python 3.6.4\lib\site-packages\discord\client.py", line 296, in __getattr__
    raise AttributeError(msg.format(self.__class__, name))
AttributeError: '<class 'discord.client.Client'>' object has no attribute 'command'
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x000001E73CDBBDA0>
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x000001E73CDCE0B8>

非常感谢您提供帮助,如果您认为我的初始方法更好,那也没关系,我只是认为如果可行,这会容易得多。

如果您知道任何参考代码或模板,那就太好了!

【问题讨论】:

  • 我不是 discord.py 或装饰器方面的专家,但为了保持一致性,@client.command() 不应该是@client.command吗?
  • @RandomDavis No. client.command() 是一个返回装饰器的函数。

标签: python python-3.x discord discord.py


【解决方案1】:

您需要使用discord.ext.commands.Bot 而不是discord.ClientBotClient 的子类,因此您应该可以将其作为替代品放入其中,一切都会开始工作

from discord.ext.commands import Bot

client = Bot('!')

# Rest of your code is unchanged

请记住,如果您想拥有on_messagecommands,您需要修改您的on_message 以支持它们。见Why does on_message stop commands from working?

【讨论】:

  • 谢谢!这真的让我很困惑
【解决方案2】:

我知道我迟到了,但答案是您总是需要在自定义命令中使用 ctx。您的代码应如下所示: @client.command() async def ping(**ctx**): await client.say('Pong')

【讨论】:

    猜你喜欢
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 2022-01-25
    • 2020-11-21
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多