Jamie Bowen
Jamie Bowen

Reputation: 45

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. I'm making a currency bot, the currency being ep, that keeps track of user's wealth and saves everything in a json file. 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!

Upvotes: 0

Views: 3950

Answers (2)

Lakshay184
Lakshay184

Reputation: 11

I know that I am late but the answer is that you always require ctx in custom command. Your code should look like this: @client.command() async def ping(**ctx**): await client.say('Pong')

Upvotes: 1

Patrick Haugh
Patrick Haugh

Reputation: 60944

You need to use discord.ext.commands.Bot instead of 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

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 commands, you need to modify your on_message to support them. See Why does on_message stop commands from working?

Upvotes: 2

Related Questions