Discord on_message method won't read discord command prefix

Viewed 54

I have been trying to make a Discord bot respond to on_message method and it is not replying for the 'haki.' prefix I added in Discord.

Here is what I have so far:

import discord

token = "mydiscordbottoken"
client = discord.Client(intents=discord.Intents.default())
command_prefix = "haki."

@client.event
async def on_ready():
    print('{0.user} BOT is ready'.format(client))
    
@client.event
async def on_message(message):
    if message.author != client.user and message.content.startswith(command_prefix):
        await message.channel.send(message)

client.run(token)

Following a tutorial. He only uses client = discord.Client(), which when I tried would give me an TypeError: init() missing 1 required keyword-only argument: 'intents'. But when I use client = discord.Client(intents=discord.Intents.default()) then run it. My discord bot is online and ready on the server. I believe that the bot can't read my discord messages based of that, but I'm not entirely sure. Any fixes would be greatly appreciated. (ex: haki.Hello should prompt the bot to say Hello)

2 Answers

The first reason I don't like is that your conditions are not separated by parentheses.

if (message.author != client.user) and message.content.startswith(command_prefix) == True:

Also, another reason is that on the developer's site, i.e. your page with bots, you need to put two sliders on the status active in permissions, then you will give permission to read messages.

HOW ADD GATEWAY INTENTS

Bots need a special intent to read your messages now.

First go to your Developer Portal, select your application and activate the Message Content Intent. Next you need to activate the intent in your code:

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

After fixing this intent problem, your bot will work but won't send the information I assume you want it to send. It will send the message object instead of the message content (if that's what you want), so you should use the following inside your on_message method:

await message.channel.send(message.content)
Related