pythondiscord.py

Why doesn't message.content.startswith work?


I've been trying to get this code to work but it just wont work. I'm stumped on how to get this to work.

The entirety of my code:

import discord



intents = discord.Intents.default()
client = discord.Client(intents = intents)
intents.members = True
intents.messages = True

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')


@client.event
async def on_message(message):
    if message.author == client.user:
            return
    if message.content.startswith('$hello'):
        print("testing")
        await message.channel.send('Hello!')

Trying to make it detected when "$hello" is said and respond with "Hello"


Solution

  • 
    @client.event
    async def on_message(message):
        if message.author == client.users:
            return
        if message.content.startswith("$hello"):
            print(message.content)
            await message.channel.send("Hello!")
    
    

    the section of if message.author == client.users: is likely causing the issue, because client.users is referencing a list of all users the client can see, but not the user who sent the message. so you could need need to remove the s at the end of users

    if message.author == client.user:
    

    is checking if the author of the bot is the bot itself.

    EDIT: After messing around with your code, i changed the discord.Intents.default() to .all() instead as well.