Python Discord bot fails due to missing 'Intents' argument

I’m new to making Discord bots with Python. I wrote a simple bot that should respond to a command, but I’m getting an error about missing ‘Intents’. Here’s what I’m seeing:

TypeError: __init__() missing 1 required keyword-only argument: 'intents'

My code looks like this:

import discord
import os

bot = discord.Bot()

@bot.event
async def on_startup():
    print(f'Bot is online as {bot.user}')

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

    if msg.content.startswith('!greet'):
        await msg.channel.send('Welcome!')

bot.start(os.environ['BOT_TOKEN'])

I’ve tried looking for solutions online, but nothing seems to work. Can someone explain what ‘Intents’ are and how to fix this error? Thanks!

The issue you’re facing is related to Discord’s Intents system, which controls what events your bot can access. To resolve this error, you need to specify the intents when creating your bot instance.

Here’s an example of how to modify your code:

import discord
import os

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

bot = discord.Bot(intents=intents)

# Rest of your code remains the same

This change informs Discord of the specific type of data your bot requires, with the ‘message_content’ intent enabling access to message details. Ensure that you also enable the required intents in the Discord Developer Portal, as some intents are considered privileged and require explicit permission. After implementing this, your bot should run without encountering the missing intents error.

yo, ur prob is u need to add intents. its a new thing discord added. try this:

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

that shud fix it. lmk if u need more help!

Hey there! I’ve been through this exact same issue when I was starting out with Discord bots. The ‘Intents’ thing is a bit of a pain, but it’s actually there for a good reason - it helps manage what info your bot can access.

Here’s what worked for me:

import discord
from discord.ext import commands

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

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

This sets up the basic intents and explicitly enables message content access. Don’t forget to update your event handlers to match the new setup. Also, make sure you’ve enabled the necessary intents in the Discord Developer Portal - that caught me out for a while!

If you’re still having trouble, double-check your discord.py version. Some older tutorials don’t mention intents because they weren’t always required. Let me know if you need any more help!