Help needed: Discord bot won't start due to 'intents' error

I’m learning Python by making a Discord bot. But I’m stuck with an error when trying to run it. Here’s what’s happening:

# discord_bot.py
import message_handler
import discord

def start_bot():
    BOT_TOKEN = 'your_token_here'
    bot = discord.Client()

    @bot.event
    async def ready():
        print('Bot is online!')

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

        if content.startswith('!'):
            await message_handler.process(msg, content[1:], private=True)
        else:
            await message_handler.process(msg, content, private=False)

    bot.run(BOT_TOKEN)

# main.py
import discord_bot

if __name__ == '__main__':
    discord_bot.start_bot()

When I run this, I get an error about missing ‘intents’. I’m new to this and don’t know how to fix it. Can someone explain what’s wrong and how to solve it? Thanks!

I faced a similar issue when I was starting out with Discord bots. The error about missing ‘intents’ comes from recent security measures introduced by Discord that require you to explicitly specify which events your bot should receive. In my case, I resolved it by importing the Intents class from discord, creating an intents object using the default settings, and then enabling the message content intent by setting intents.message_content to True. I passed this intents object to my bot’s constructor. My revised code looked like this:

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True  # Enable message content intent

bot = commands.Bot(command_prefix='!', intents=intents)

# Rest of your code...

This approach worked for me, and remember to enable the appropriate intents in the Discord Developer Portal as well.

yo, had this problem too. its annoying but easy to fix. you need to add intents to ur bot. heres how:

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

that should do it. good luck with ur bot!

As someone who’s worked extensively with Discord bots, I can shed some light on your issue. The ‘intents’ error is a common stumbling block for newcomers. It’s related to Discord’s privilege system, which helps maintain user privacy and server security.

To resolve this, you’ll need to modify your code to explicitly declare the intents your bot requires. In your case, you’ll want to enable the message content intent at minimum. Here’s how you can adjust your code:

import discord

intents = discord.Intents.default()
intents.message_content = True

def start_bot():
    BOT_TOKEN = 'your_token_here'
    bot = discord.Client(intents=intents)

    # Rest of your code remains the same

Additionally, ensure you’ve enabled the necessary intents in your Discord Developer Portal for the bot. This should resolve your error and get your bot up and running.