'create a command using discord.py

i'm starting using discord.py and i don't understand why my bot does not respond to my test command .the bot is starting correcly and says his line in the channel while starting but do not react to my command

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='$')



class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged on as {0}!'.format(self.user))
        channel = client.get_channel(123456789)
        await channel.send("online")
     
        
@bot.command()
async def test(ctx):
    await ctx.send('test')

thank you !



Solution 1:[1]

You have defined your bot as bot. However, in your MyClient class you have put client.get_channel which should be bot.get_channel. You're saying it starts fine and sends the message to the channel which is weird. You might have had trouble saving the code or something

Solution 2:[2]

The first thing is that you don't necessarily need a class attribute; you can use @bot.event just fine like this:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='$')

@bot.event
async def on_ready(self):
        print('Logged on as {0}!'.format(self.user))
        channel = client.get_channel(123456789)
        await channel.send("online")
     
@bot.command()
async def test(ctx):
    await ctx.send('test')

Or, the error in your original code is in two places. discord.Client is inbuilt; instead you should be declaring bot as well as indentation of bot.command:

from discord.ext import commands

bot = commands.Bot(command_prefix='$')

class MyClient(bot):
   
   @bot.event
    async def on_ready(self):
        print('Logged on as {0}!'.format(self.user))
        channel = client.get_channel(123456789)
        await channel.send("online")
     
        
   @bot.command()
    async def test(ctx):
        await ctx.send('test')

I think that should work. The first one I am sure will work 100%; the second one, not so much since I haven't run these codes right now since I am busy. If you get any other error, feel free to ask. Plus, I don't think there is any benefit in declaring a class.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tears
Solution 2 Jeremy Caney