Trouble starting Discord bot - encountering TypeError about missing intents parameter

I’m developing a Discord bot using Python to enhance my programming skills, but I’m facing an error when I attempt to run it. This is the setup I’ve been using:

# main.py
import bot_module

if __name__ == '__main__':
    bot_module.launch_bot()
# responses_module.py
import random

def process_message(msg) -> str:
    cleaned_message = msg.lower()
    
    if cleaned_message == "hi":
        return "Hello!"
    
    if cleaned_message == "roll":
        return str(random.randint(1, 6))
    
    if cleaned_message == "!help":
        return "This command provides help."
# bot_module.py
import discord
import responses_module

async def send_reply(msg, user_input, private):
    try:
        reply = responses_module.process_message(user_input)
        await msg.author.send(reply) if private else await msg.channel.send(reply)
    except Exception as e:
        print(e)

def launch_bot():
    TOKEN = '################################################################'
    client = discord.Client()
    
    @client.event
    async def on_ready():
        print(f'{client.user} is now online!')
    
    @client.event
    async def on_message(msg):
        if msg.author == client.user:
            return
        
        user = str(msg.author)
        user_input = str(msg.content)
        channel = str(msg.channel)
        
        if user_input[0] == "?":
            user_input = user_input[1:]
            await send_reply(msg, user_input, private=True)
        else:
            await send_reply(msg, user_input, private=False)
    
    client.run(TOKEN)

When I execute this code, I receive:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    bot_module.launch_bot()
  File "bot_module.py", line 12, in launch_bot
    client = discord.Client()
TypeError: Client.__init__() missing 1 required keyword-only argument: 'intents'

I’ve rearranged the code but it hasn’t helped. What does this intents error signify, and how can I resolve it?

This error showed up when Discord.py 2.0 made intents mandatory for security. I hit the same problem updating an old bot. Quick fix: add intents=discord.Intents.all() to your Client setup, but that’s pretty broad permissions-wise. Better approach: use intents = discord.Intents.default() then turn on what you actually need like intents.message_content = True. Make sure you enable the matching privileges in your bot settings on Discord’s developer portal - without that, your code won’t matter. Heads up: bots in 100+ servers need verification for some intents.

yeah, discord changed their api requirements. you need to add intents when creating the client now. use client = discord.Client(intents=discord.Intents.default()) for everything, or pick specific intents your bot actually needs.

The issue you’re encountering is due to Discord’s requirement for specifying intents starting with version 2.0. To resolve this, you need to create an instance of discord.Intents and specify which intents your bot needs. For your use case, you can modify your code as follows:

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

Make sure to enable the ‘Message Content Intent’ in your bot’s settings on the Discord Developer Portal as well, or your bot won’t receive any messages.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.