Need help: Discord bot not responding to chat commands

I’m trying to create a Discord bot that responds to simple chat commands. I’ve done some research but haven’t found any clear explanations. Here’s my current code:

import discord

client = discord.Client()

@client.event
def on_ready():
    print('Bot is online!')
    await client.change_presence(activity=discord.Game('chatting'))

@client.event
def on_message(message):
    if message.author == client.user:
        return
    
    if message.content == 'Hello':
        await message.channel.send('Hi there!')

client.run('MY_BOT_TOKEN')

The bot’s status shows as ‘chatting’ which is what I wanted. However, when I type ‘Hello’ in Discord the bot doesn’t respond. I’m also getting this error when I run the script:

await message.channel.send('Hi there!')
AttributeError: 'Client' object has no attribute 'send'

Can someone help me figure out why the bot isn’t responding and how to fix this error? Thanks!

yo, i had the same prob. try using commands.Bot instead of Client. it’s easier to work with for chat commands. also, make sure ur bot has the right perms in the server. if that don’t fix it, check ur token again. good luck!

I ran into a similar issue when I first started working with Discord bots. The problem is likely related to the Discord.py version you’re using. In newer versions (2.0+), you need to enable message intents explicitly.

Try modifying your code like this:

import discord

intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)

# Rest of your code remains the same

This should resolve the AttributeError and allow your bot to respond to messages. Also, make sure you’ve invited the bot to your server with the proper permissions.

If you’re still having issues, double-check your bot token and ensure you’ve enabled the necessary privileged intents in the Discord Developer Portal. Let me know if you need any more help!

I’ve encountered this issue before. The problem lies in using the older Client class instead of the more versatile commands.Bot. Here’s a quick fix:

from discord.ext import commands

bot = commands.Bot(command_prefix='!', intents=discord.Intents.default())

@bot.command()
async def hello(ctx):
    await ctx.send('Hi there!')

bot.run('MY_BOT_TOKEN')

This approach simplifies command handling and resolves the AttributeError. Remember to update your Discord.py library if you haven’t already. Also, ensure you’ve properly configured bot permissions in your server settings. If issues persist, double-check your bot token in the Discord Developer Portal.