Home > Net >  Why doesn't my discord bot respond to any command that i give in the chat
Why doesn't my discord bot respond to any command that i give in the chat

Time:10-12

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 ='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':
        if user_message.lower() == 'Hello'
        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)

CodePudding user response:

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.casefold() == 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)

CodePudding user response:

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

  • Related