Reputation: 3
The problem that I am facing is that my discord bot does not respond or read the messages that I am writing in the chat. The out put of the code down bellow is the users name and nothing else.
import discord
import random
TOKEN ='example'
client = discord.Client(intents=discord.Intents.default())
@client.event
async def on_ready():
print('We have logged in as{0.user}'.format(client))
@client.event
async def on_message(message):
username = str(message.author).split('#')[0]
user_message = (message.content)
channel = str(message.channel.name)
print(f'{username}: {user_message} ({channel})')
if message.author == client.user:
return
if message.channel.name == 'example':
if user_message.lower() == 'Hello':
await message.channel.send(f'Hello {username}')
elif user_message.lower() == 'bye':
await message.channel.send(f'Hello {username}')
elif user_message.lower() == '!random':
response = f'This is your number: {random.randrange(1000000)}'
await message.channel.send(response)
client.run(TOKEN)
Upvotes: 0
Views: 826
Reputation: 18
Your bot can't read the content of the messages sent in a guild because it is missing the message_content intent.
You gave your bot only the default intents, which doesn't include the message_content one.
client = discord.Client(intents=discord.Intents.default())
Here is how you fix it:
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
In case your bot isn't still able to read the content of the guild messages, you need also to turn on the message content intent in the Discord Developer Portal:
Select your app Go to the "Bot" tab Turn the "message content intent" on Now your bot should be able to read the content of the messages sent in guilds.
Upvotes: 0
Reputation: 91
The .lower() method only searches for lower case letters in a string, hence the name, so typing "Hello" into the chat will not trigger the command, as "Hello" has a capital "H". To fix your code you can either:
Change your code to
if user_message.lower() == 'hello':
await message.channel.send(f'Hello {username}')
Notice you can still keep the capital H for hello in
await message.channel.send(f'Hello {username}')
Or, you could compare 2 values like this:
string = 'Hello'
if user_message == string:
#the rest of your code goes here
Your full code should be:
import discord
import random
TOKEN ='exemple'
client = discord.Client(intents=discord.Intents.default())
@client.event
async def on_ready():
print('We have logged in as{0.user}'.format(client))
@client.event
async def on_message(message):
username = str(message.author).split('#')[0]
user_message = (message.content)
channel = str(message.channel.name)
print(f'{username}: {user_message} ({channel})')
if message.author == client.user:
return
if message.channel.name == 'example':
string = 'Hello'
if user_message.casefold() == string:
await message.channel.send(f'Hello {username}')
elif user_message.lower() == 'bye':
await message.channel.send(f'Hello {username}')
return
elif user_message.lower() == '!random':
response = f'This is your number: {random.randrange(1000000)}'
await message.channel.send(response)
return
client.run(TOKEN)
Upvotes: 1
Reputation: 5650
Intents.default()
doesn't include the Message Content
intent, which is now required. Without the intent, message.content
will be empty.
More information in the docs: https://discordpy.readthedocs.io/en/stable/intents.html#privileged-intents
Upvotes: 0