简体   繁体   English

Discord.py bot - ping 命令不起作用

[英]Discord.py bot - ping command not working

I'm just sarting out with discord bots and haven't been using python for very long either.我只是开始使用 discord bots 并且也没有很长时间使用 python。 I'm making a currency bot, the currency being ep, that keeps track of user's wealth and saves everything in a json file.我正在制作一个货币机器人,货币为 ep,它跟踪用户的财富并将所有内容保存在 json 文件中。 I got this working before but wanted to use a different way of writing it.我以前做过这个,但想用不同的方式来写它。

My initial way -我最初的方式——

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

My (hopefully better way) -我的(希望更好的方式)-

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

The error messages -错误信息 -

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>

Help with this would be much appreciated and if you think that my initial method is better then that's fine too, I just think that this is far easier if it works.对此的帮助将不胜感激,如果您认为我的初始方法更好,那也没关系,我只是认为如果它有效,这会容易得多。

If you know of any reference code or templates then that would be awesome!如果您知道任何参考代码或模板,那就太棒了!

You need to use discord.ext.commands.Bot instead of discord.Client .您需要使用discord.ext.commands.Bot而不是discord.Client Bot is a subclass of Client , so you should be able to just drop it in as a replacement and everything will start working BotClient的子类,因此您应该能够将其作为替代品放入,一切都会开始工作

from discord.ext.commands import Bot

client = Bot('!')

# Rest of your code is unchanged

Keep in mind that if you want to have on_message and command s, you need to modify your on_message to support them.请记住,如果您想要on_messagecommand ,您需要修改您的on_message以支持它们。 See Why does on_message stop commands from working?请参阅为什么 on_message 会停止命令工作?

I know that I am late but the answer is that you always require ctx in custom command.我知道我迟到了,但答案是您总是需要在自定义命令中使用 ctx。 Your code should look like this: @client.command() async def ping(**ctx**): await client.say('Pong')您的代码应如下所示: @client.command() async def ping(**ctx**): await client.say('Pong')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM