我只是在使用不和谐的机器人,而且使用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>
如果你认为我的初始方法更好,那也没问题,我只是认为如果它有效的话,这会容易得多。
如果你知道任何参考代码或模板,那就太棒了!
发布于 2019-03-01 20:23:09
您需要使用discord.ext.commands.Bot
而不是discord.Client
。Bot
是Client
的子类,因此您应该能够将其作为替代,一切都将开始工作
from discord.ext.commands import Bot
client = Bot('!')
# Rest of your code is unchanged
请记住,如果您想使用on_message
和command
,则需要修改您的on_message
以支持它们。请参阅Why does on_message stop commands from working?
发布于 2020-12-09 07:13:50
我知道我迟到了,但答案是你总是需要在自定义命令中使用ctx。您的代码应该如下所示:@client.command() async def ping(**ctx**): await client.say('Pong')
https://stackoverflow.com/questions/54951295
复制