Discord bot suddenly stopped working after updating discord.py - getting login token errors

My Discord bot was working perfectly fine last week but now it’s throwing a bunch of errors after I tried to upgrade discord.py to support slash commands. I’m getting a 401 Unauthorized error that says “Improper token has been passed” but I haven’t changed my token at all.

Here’s my bot code:

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_image.jpg', 'rb') as f:
        image = discord.File(f)
        welcome_channel = bot.get_channel(123456789012345678)
        await welcome_channel.send('Welcome to the server! Here is your starter pack!', 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 f:
        boom_gif = discord.File(f)
    await ctx.send('KABOOM!!!', file=boom_gif)

bot.run(BOT_TOKEN)

The error traceback shows LoginFailure and HTTPException errors. I double checked my token in the config file and it looks correct. Should I reinstall discord.py or is there something else I’m missing? This is really confusing for someone new to Python like me.

The token error might be from discord.py version changes, but there’s another issue that could cause problems. You’re using commands.Bot() without intents, which newer versions require. Add intents = discord.Intents.default() and intents.members = True before creating your bot, then pass intents=intents to the Bot constructor. Also regenerate your token from the Discord Developer Portal - tokens can get corrupted during library updates even if they look fine in your config.

Had the same issue when I updated discord.py. Your token’s probably fine - the real problem is that version 2.0+ requires intents for most events. Add intents = discord.Intents.default() then use it when you initialize your bot: bot = commands.Bot(command_prefix=['?', '#', '~', '+', '%'], intents=intents). Also, switch your greet, farewell, and explode functions to use @bot.command() instead of @bot.event. That should fix the login failure.

i think ur mixing up @bot.event with @bot.command. if u want users to trigger greet, farewell, and explode, they need to be commands with @bot.command() not events. that should help with the token issue.

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