Discord bot throwing 401 Unauthorized error after updating discord.py version

I’m having trouble with my Discord bot after trying to upgrade discord.py to support slash commands. The bot was working fine before but now I keep getting authentication errors. Here’s my current setup:

import discord
from discord.ext import commands

from config import *

bot = commands.Bot(command_prefix=['?', '#', '+', '-', '%'])

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

@bot.event
async def greet(ctx):
    await ctx.send('Hi there!')

@bot.event
async def on_member_join(member):
    with open('welcome_pic.png', 'rb') as file:
        image = discord.File(file)
        welcome_channel = bot.get_channel(123456789012345678)
        await welcome_channel.send('Welcome! Here is something for you!', file=image)

@bot.event
async def farewell(ctx):
    await ctx.send('See you later!')

@bot.event
async def explode(ctx):
    with open('boom.gif', 'rb') as file:
        animation = discord.File(file)
    await ctx.send('BOOM TIME!', file=animation)

bot.run(TOKEN)

The error message shows ‘Improper token has been passed’ but I haven’t changed my token. Is this related to the discord.py update or did I mess up something in my code? Should I reinstall the library completely?

you’ve got events and commands mixed up. those functions with the ctx parameter need @bot.command(), not @bot.event - that’s likely causing your auth problems. also check if your token expired or got regenerated in the discord dev portal.

Had the exact same problem when I moved to discord.py 2.0 for slash commands. Your token’s fine - it’s the intents that are screwing you over. Newer versions need explicit intents declaration. Add intents = discord.Intents.default() and pass it to your Bot like bot = commands.Bot(command_prefix=['?', '#', '+', '-', '%'], intents=intents). Don’t forget to enable the intents in your Discord Developer Portal under Bot settings. Yeah, your decorators are messed up too, but the missing intents will throw that 401 error even with a good token. Wasted hours on this exact issue.

The authentication error you’re encountering is likely not due to your token or the discord.py update. The issue stems from the way you’ve defined your event functions. The greet, farewell, and explode methods currently use the @bot.event decorator, but they are set up like commands that require a ctx parameter. Events are triggered by the Discord API and don’t need to be invoked using commands. Change these decorators to @bot.command() instead. Moreover, ensure that your token is correctly imported from your config file and that it contains the valid bot token. Additionally, verify if you have set the appropriate intents when initializing your bot, as they can affect authentication.