I’m facing an authentication error while trying to run my Discord bot that facilitates a Connect Four game. Interestingly, the same code works flawlessly on another machine with identical Python libraries.
# Required imports
import discord
import asyncio
from discord.ext import commands
intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
# Connect Four variables
player_one = ""
player_two = ""
current_turn = ""
game_active = True
board_state = []
winning_combinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
]
@bot.event
async def on_ready():
server_count = 0
for server in bot.guilds:
print(f'Server ID: {server.id} Name: {server.name}')
server_count += 1
print(f'Bot is online in {server_count} servers')
@bot.event
async def on_message(message):
if '!connect4' in message.content:
global player_one, player_two, current_turn, game_active
global board_state
await message.channel.send('Mention someone to start playing!')
try:
response = await bot.wait_for('message',
check=lambda m: m.author == message.author and m.channel == message.channel,
timeout=20)
except asyncio.TimeoutError:
await message.channel.send('You took too long!')
else:
if game_active:
board_state = [":blue_square:" for _ in range(9)]
current_turn = ""
game_active = False
player_one = message.author
player_two = response.mentions[0]
# Initial display of the game board
board_display = ""
for i in range(len(board_state)):
if i in [2, 5, 8]:
board_display += board_state[i]
await message.channel.send(board_display)
board_display = ""
else:
board_display += board_state[i]
# Randomly decide who starts
import random
starter = random.randint(1, 2)
if starter == 1:
current_turn = player_one
await message.channel.send(f"<@{player_one.id}> goes first!")
else:
current_turn = player_two
await message.channel.send(f"<@{player_two.id}> goes first!")
else:
await message.channel.send("Another game is still ongoing!")
bot.run('YOUR_TOKEN_HERE')
The error I encounter is:
Traceback (most recent call last):
File "discord/http.py", line 300, in static_login
data = await self.request(Route('GET', '/users/@me'))
File "discord/http.py", line 254, in request
raise HTTPException(r, data)
discord.errors.HTTPException: 401 Unauthorized (error code: 0): 401: Unauthorized
This links back to:
Traceback (most recent call last):
File "mybot.py", line 96, in <module>
bot.run('TOKEN_STRING_HERE')
File "discord/client.py", line 723, in run
return future.result()
File "discord/client.py", line 702, in runner
await self.start(*args, **kwargs)
File "discord/client.py", line 665, in start
await self.login(*args, bot=bot)
File "discord/client.py", line 511, in login
await self.http.static_login(token.strip(), bot=bot)
File "discord/http.py", line 304, in static_login
raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.
Any idea why the same bot token works on one system but fails on the other?