Discord.py: HTTPException: UNAUTHORIZED (status code: 401) on several occasions

Created on 13 Nov 2016  路  5Comments  路  Source: Rapptz/discord.py

So I've asked in a general programming server and also the discord api server for help, but no one could help me, so the thought of the problem being between my monitor and the chair is getting less and less.

I get the Exception at 3 occasions.

The first occurrence is in adding roles to a user:

When my bot starts up, it checks to see if a certain role exists on the server and if otherwise, makes it.
This goes without a problem.
If a user sends a command .newgame that role should get assigned to the user.

role = Game.getGameRole(message.channel.server)
if role not in message.author.roles:
        print('Giving Role: ', role)
        print('Role type: ', type(role))
        print('To: ', message.author)
        await client.add_roles(message.author, role)

with in the Game module;

def getGameRole(server):
    role = discord.utils.find(lambda r: r.name == GameRole, server.roles)
    return role

with GameRole being a global variable 'Adventurer'

This is the output:

Command recognized
<class 'discord.member.Member'>
Giving Role:  Adventurer
Role type:  <class 'discord.role.Role'>
To:  D4rkwulf#7141
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Kaj\Desktop\Python\pip\discord.py\discord\client.py", line 273, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "C:\Users\Kaj\Desktop\Python\pip\discord.py\Nim.py", line 26, in on_message
    await exe(message)
  File "C:\Users\Kaj\Desktop\Python\pip\discord.py\Commands.py", line 101, in newgame
    await client.add_roles(memb, role)
  File "C:\Users\Kaj\Desktop\Python\pip\discord.py\discord\client.py", line 2560, in add_roles
    yield from self._replace_roles(member, new_roles)
  File "C:\Users\Kaj\Desktop\Python\pip\discord.py\discord\client.py", line 2531, in _replace_roles
    yield from self.http.replace_roles(member.id, member.server.id, roles)
  File "C:\Users\Kaj\Desktop\Python\pip\discord.py\discord\http.py", line 137, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: UNAUTHORIZED (status code: 401): 401: Unauthorized

And I only use my bot in server channels, not in private channels, so message.author is a Member and not a User. I did check this with a print(type(message.author))

I've been tracing this back and forth and I just can't find the error.

The second occurrence is in the prune command

cmd = message.content.split()
chn = message.channel
count = int(cmd[1])
try:
        control = cmd[2]
except IndexError:
        control = None
print(chn, count, control)
deleted = await client.purge_from(channel=chn, limit=count, check=control)
print('Deleted {} message(s)'.format(len(deleted)))

Don't mind the ugly code for now.
When entering the command .clean 10, this is the output:
```bot_testing_d4 10 None
Ignoring exception in on_message
Traceback (most recent call last):
File "C:Users\Kaj\Desktop\Python\pip\discord.py\discord\client.py", line 273, in _run_event
yield from getattr(self, event)(args, *kwargs)
File "C:Users\Kaj\Desktop\Python\pip\discord.py\Nim.py", line 26, in on_message
await exe(message)
File "C:Users\Kaj\Desktop\Python\pip\discord.py\Commands.py", line 89, in clean
deleted = await client.purge_from(channel=chn, limit=count, check = control)
File "C:Users\Kaj\Desktop\Python\pip\discord.py\discord\client.py", line 1055, in purge_from
msg = yield from iterator.iterate()
File "C:Users\Kaj\Desktop\Python\pip\discord.py\discord\iterators.py", line 114, in iterate
yield from self.fill_messages()
File "C:Users\Kaj\Desktop\Python\pip\discord.py\discord\iterators.py", line 122, in fill_messages
data = yield from self._retrieve_messages(retrieve)
File "C:Users\Kaj\Desktop\Python\pip\discord.py\discord\iterators.py", line 138, in _retrieve_messages_before_strategy
data = yield from self.client._logs_from(self.channel, retrieve, before=self.before)
File "C:Users\Kaj\Desktop\Python\pip\discord.py\discord\http.py", line 137, in request
raise HTTPException(r, data)
discord.errors.HTTPException: UNAUTHORIZED (status code: 401): 401: Unauthorized

## The third occurrence is in sending a message
This functions sends data depending on the attribute values of its class
```Python
async def send(self, msg):
                print(self.Channel)
                print(msg)
                await client.send_message(self.Channel, msg)

Which is called by

print(Commands.Troll.Author, Commands.Troll.Channel, Commands.Troll.Origin, Commands.Troll.Active)
        await Commands.Troll.send(message.content)

This is the output;
D4rkwulf#7141 general bot_testing_d4 True general hi Ignoring exception in on_message Traceback (most recent call last): File "C:\Users\Kaj\Desktop\Python\pip\discord.py\discord\client.py", line 273, in _run_event yield from getattr(self, event)(*args, **kwargs) File "C:\Users\Kaj\Desktop\Python\pip\discord.py\Nim.py", line 20, in on_message if(not await ValidateCommand(message)): File "C:\Users\Kaj\Desktop\Python\pip\discord.py\Nim.py", line 46, in ValidateCommand await Commands.Troll.send(message.content) File "C:\Users\Kaj\Desktop\Python\pip\discord.py\Commands.py", line 56, in send await client.send_message(self.Channel, msg) File "C:\Users\Kaj\Desktop\Python\pip\discord.py\discord\client.py", line 829, in send_message data = yield from self.http.send_message(channel_id, content, guild_id=guild_id, tts=tts) File "C:\Users\Kaj\Desktop\Python\pip\discord.py\discord\http.py", line 137, in request raise HTTPException(r, data) discord.errors.HTTPException: UNAUTHORIZED (status code: 401): 401: Unauthorized

On the last 2, honestly, i haven't spend too much time, figuring out what's wrong with them, but on the first one, not only I but a bunch of other programmers, including the Discord API community could't figure it out.

Thank you for your time in advance!

invalid question

Most helpful comment

@HYP3RD34TH Thank you!

Now using:

import discord
from discord.ext.commands import Bot

my_bot = Bot(command_prefix="!")

@my_bot.event
async def on_message(message):
    if message.content.startswith('!meme'):
        await my_bot.send_message(message.channel, 'Meme')

my_bot.run('mytoken')

and it works xxx

All 5 comments

I found the problem. I used different instances of client.

Having this same issue. with a very basic bot (should be an easy fix)

import discord
from discord.ext.commands import Bot

client = discord.Client()
my_bot = Bot(command_prefix="!")

@my_bot.event
async def on_message(message):
    if message.content.startswith('!meme'):
        await client.send_message(message.channel, 'Meme')

my_bot.login("myTokenIsHere")
my_bot.run('myTokenIsHere')

Give me this error:

Ignoring exception in on_message
Traceback (most recent call last):
  File "D:\Python36\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "D:/Python27/discordBot/discordBot.py", line 12, in on_message
    await client.send_message(message.channel, 'Meme')
  File "D:\Python36\lib\site-packages\discord\client.py", line 1152, in send_message
    data = yield from self.http.send_message(channel_id, content, guild_id=guild_id, tts=tts, embed=embed)
  File "D:\Python36\lib\site-packages\discord\http.py", line 200, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: UNAUTHORIZED (status code: 401): 401: Unauthorized

What am I doing wrong?

I'm not sure... Still using the Stable version... But It looks like you are trying to run 2 Instances...
You don't need the client... Bot or in this case my_bot should be enough... same for my_bot.login()
bot.run() should be enough to start the bot... not sure if the error has to do something with it... I am not a python pro and still a beginner

@HYP3RD34TH Thank you!

Now using:

import discord
from discord.ext.commands import Bot

my_bot = Bot(command_prefix="!")

@my_bot.event
async def on_message(message):
    if message.content.startswith('!meme'):
        await my_bot.send_message(message.channel, 'Meme')

my_bot.run('mytoken')

and it works xxx

In my case, I was sending the token for the app and not the bot, so the code above. Once I used to token for the botuser, I authenticated just fine.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marcoshuck picture marcoshuck  路  16Comments

wolfclaws picture wolfclaws  路  17Comments

Mercurial picture Mercurial  路  22Comments

imayhaveborkedit picture imayhaveborkedit  路  58Comments

haaddaa1 picture haaddaa1  路  14Comments