My Discord bot is online but not responding to commands in the channel

I’m working on a Discord bot project using Python and Replit. The bot is online but doesn’t react when I type the $inspire command in my server. Here’s my code:

import discord
import os
import requests
import json

bot_intents = discord.Intents.default()
bot = discord.Client(intents=bot_intents)

def fetch_inspiration():
    api_response = requests.get("https://inspireme.api/quote")
    data = json.loads(api_response.text)
    inspirational_quote = f'{data[0]["text"]} - {data[0]["author"]}'
    return inspirational_quote

@bot.event
async def on_ready():
    print(f'Bot is now active as {bot.user}')

@bot.event
async def on_message(msg):
    if msg.author == bot.user:
        return

    if msg.content.startswith('$motivate'):
        quote = fetch_inspiration()
        await msg.channel.send(quote)

bot.run(os.environ['SECRET_TOKEN'])

The bot should send a random inspirational quote when I type $motivate, but it’s not working. I’ve double-checked the permissions. What could be wrong?

I’ve encountered similar issues with Discord bots before and, in my experience, one important detail is the setup of intents.

You’re using default intents, which might not include message content. Try modifying your intents like this:

bot_intents = discord.Intents.default()
bot_intents.message_content = True
bot = discord.Client(intents=bot_intents)

Also, ensure that the ‘Message Content Intent’ is enabled in your Discord Developer Portal for this bot. Adding print statements in your on_message function can help confirm whether the function is triggering. Finally, double-check that your bot’s role in the server hierarchy is set correctly so it can process and send messages as expected.

hey man, i think i know whats up. check ur bot’s intents in the discord developer portal. make sure ‘message content intent’ is turned on. also, try adding some print statements in ur on_message function to see if its even gettin triggered. that’ll help u figure out where the problem is. good luck!

I noticed a discrepancy in your code that might be causing the issue. In your initial question, you mentioned using the $inspire command, but your code is set up to respond to $motivate. Make sure you’re using the correct command in your server.

Also, ensure that your bot has the necessary permissions in the specific channel where you’re trying to use the command. Sometimes, bots can appear online but lack the right permissions to send messages in certain channels.

Lastly, double-check that your SECRET_TOKEN environment variable in Replit is correctly set with your bot’s token. If the token is invalid or missing, the bot might appear online but fail to respond to commands.

If these suggestions don’t resolve the issue, you might want to add some error handling or logging to your code to help identify where the problem occurs.