everyone! What I Want: Recently decided to learn discord.py, but faced a problem from the start. Namely, I wanted to code the simplest bot that can send back "hello" after it sees "hello" from a user. In order to make it, I coded the following:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
channel = bot.get_channel(1069982959458848801)
@bot.event
async def on_ready():
print("BOT IS ONLINE")
@bot.event
async def on_message(msg):
if msg.content == "hello":
await msg.channel.send("hello")
bot.run('TOKEN')
What I Did: Initially, I thought the problem with intents(developer portal is fine also, all three intents are turned on), but then I realised that console does not contain any error messages. Then I double checked my code and everything is fine I suppose.
So, would really appreciate your help with this question. Thank you in advance!
User: hello Bot: hello
CodePudding user response:
Seems like you need the Message.content
intent. As explained in the docs.
Since msg.content will otherwise be an empty string according to this doc source. You could check by trying to print msg.content, before and after the if statement. Then you will see if the content is right and if the if statement produces the desired result.
CodePudding user response:
In discord.Intents.default()
message_content
intent set to false , and msg.content
is always an empty string (message content docs). You need to add
intents.message_content = True
or use
intents = discord.Intents.all()
That's a simplest way to fix that
CodePudding user response:
Avoid using intents = discord.Intents.default()
.
Instead, use intents = discord.Intents.all()
(as you already have all the three intents enabled on your Discord Developer Portal, this would be better and easier). Or, as @Jabi has suggested, which is the most conventional way, but which sometimes with Discord's updates becomes very problematic, just add intents.messages = True
.
You have another problem. Your Bot's command_prefix
is set to !
, meaning that async def on_message(<message>)
will accept only messages that start with !
. But you're trying to check if the message is "hello" using if msg.content == "hello"
, but this will always be False
as it's not possible. Either remove the command_prefix
and check for the prefix using if-elif-else
s or use custom commands to process it.