I'm trying to set up a basic Discord bot that can reply to simple chat commands. I've looked online but couldn't find any easy-to-follow guides. Here's what I've got so far:
```python
import discord
bot = discord.Client()
@bot.event
async def on_ready():
print("BOT ACTIVATED!")
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 fine, but when I type ‘Hello’ in Discord, nothing happens. I’m also getting this error:
await msg.channel.send("Hi there!")
AttributeError: 'Message' object has no attribute 'channel'
Can someone help me figure out what I’m doing wrong? Thanks!
I suspect the issue comes from using an outdated version of discord.py. I encountered a similar problem and found that the library has undergone significant changes. Updating to the latest version should help a lot. In the new version, you would typically use discord.ext.commands instead of relying solely on discord.Client. This means you can define commands with decorators, which simplifies the process and avoids the AttributeError you’re encountering. For example, try using:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!', intents=discord.Intents.default())
@bot.event
async def on_ready():
print('BOT ACTIVATED!')
await bot.change_presence(activity=discord.Game('chatting'))
@bot.command()
async def hello(ctx):
await ctx.send('Hi there!')
bot.run('YOUR_TOKEN_HERE')
This approach should resolve your error and make your bot respond correctly. Be sure to invoke the command with the appropriate prefix, as in ‘!hello’.
yo dude, ur code looks aight but u need to update ur discord.py library. run ‘pip install -U discord.py’ in ur command prompt. also, add these lines at the top:
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
That should fix ur issues bro. lmk if u need more help!
Your code looks close, but there are a few adjustments needed. First, make sure you’re using the latest discord.py version. The ‘channel’ attribute issue suggests an outdated library. Update it with ‘pip install -U discord.py’.
Next, enable intents. Add this before creating your client:
discord.Intents.default().all()
client = discord.Client(intents=discord.Intents.default())
For the message event, try:
if message.content.lower() == ‘hello’:
await message.reply(‘Hi there!’)
This should fix the response issue. Remember to use your bot’s prefix if you’ve set one. Also, double-check your bot token and ensure it has proper permissions in your server. Let me know if you need more help!