I’m trying to set up a Discord bot that can reply to simple chat commands. I’ve done some research but haven’t found any clear explanations. Here’s what I’ve got so far:
import discord
bot = discord.Client()
@bot.event
async def on_ready():
print('Bot is online!')
await bot.change_presence(activity=discord.Game('chatting'))
@bot.event
async def on_message(msg):
if msg.author == bot.user:
return
if msg.content == 'Hello':
await msg.channel.send('Hi there!')
bot.run('MY_SECRET_TOKEN')
The bot’s status shows as ‘chatting’ which is what I wanted. But when I type ‘Hello’ in Discord, the bot doesn’t respond. I’m also getting this error:
AttributeError: 'Client' object has no attribute 'send_message'
Can someone help me figure out why the bot isn’t responding and how to fix this error? Thanks!
Hey there! I’ve been through the same struggle with Discord bots. Your code looks pretty good, but there’s a small issue that’s causing the bot to not respond.
The error you’re seeing about ‘send_message’ is actually unrelated to your current code. It seems like you might have tried using an older method somewhere else in your script.
For the bot not responding to ‘Hello’, make sure you’ve enabled the message content intent. Add this line before creating your bot:
intents = discord.Intents.default()
intents.message_content = True
bot = discord.Client(intents=intents)
Also, double-check that you’ve invited the bot to your server with the right permissions. Sometimes that’s the culprit!
Lastly, ensure your bot token is correct. If it’s invalid, the bot won’t connect properly.
Hope this helps! Let me know if you need any more assistance.
I’ve implemented several Discord bots and can offer some insights. Your code structure is correct, but you’re using the older Client class. For better functionality, switch to discord.Bot() from discord.ext.commands. This simplifies command handling.
Also, ensure you’ve properly set up your bot’s permissions in the Discord Developer Portal. Sometimes, missing permissions can prevent the bot from seeing or responding to messages.
Regarding the AttributeError, it’s likely from an outdated tutorial. The current method is await message.channel.send(), which you’re already using correctly.
Lastly, consider implementing error handling in your on_message event. This can help you catch and log any issues that might be preventing your bot from responding as expected.
yo man, i had similar issues. make sure u enabled message content intent in discord developer portal. also, try using discord.commands instead of client. it’s way easier to work with. oh and double check ur bot token. sometimes that’s the problem. good luck with ur bot!