Discord.py: Can't seem to load token from file

Created on 29 Nov 2017  Â·  3Comments  Â·  Source: Rapptz/discord.py

OS: GalliumOS (Xubuntu optimized for Chromebooks)

Bit of a noob here, but this keeps saying the token's wrong.
I checked, and the open("token","r").read() is returning my token properly, and as a string...

Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/discord/gateway.py", line 430, in poll_event
msg = yield from self.recv()
File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 319, in recv
raise ConnectionClosed(self.close_code, self.close_reason)
websockets.exceptions.ConnectionClosed: WebSocket connection is closed: code = 4004, reason = Authentication failed..
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "loachbot.py", line 168, in
bot.run(str(open("./token","r").read()))
File "/usr/local/lib/python3.5/dist-packages/discord/client.py", line 519, in run
self.loop.run_until_complete(self.start(args, *kwargs))
File "/usr/lib/python3.5/asyncio/base_events.py", line 387, in run_until_complete
return future.result()
File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
result = coro.send(None)
File "/usr/local/lib/python3.5/dist-packages/discord/client.py", line 491, in start
yield from self.connect()
File "/usr/local/lib/python3.5/dist-packages/discord/client.py", line 448, in connect
yield from self.ws.poll_event()
File "/usr/local/lib/python3.5/dist-packages/discord/gateway.py", line 437, in poll_event
raise ConnectionClosed(e) from e
discord.errors.ConnectionClosed: WebSocket connection is closed: code = 4004, reason = Authentication failed..

import discord, os
os.system('clear')

bot = discord.Client()
token = open("token","r").read()
# print(token)

@bot.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == bot.user:
        return

        # commands and stuff go here, removed for length

@bot.event
async def on_ready():
    print('Logged in as '+bot.user.name)

bot.run(token)

Thanks in advance, sorry for the length.

question

Most helpful comment

thanks, I figured that out 😆not sure why nano does that, but it wasn't too hard to fix

Sent from my T-Mobile 4G LTE Device
-------- Original message --------From: Clinton Dcruz notifications@github.com Date: 1/21/18 12:48 AM (GMT-05:00) To: "Rapptz/discord.py" discord.py@noreply.github.com Cc: superloach superloach42@gmail.com, Author author@noreply.github.com Subject: Re: [Rapptz/discord.py] Can't seem to load token from file (#937)
Check the contents of 'token.txt' file.

Even a new line/return character will affect the string, since you're using the read() function. It basically reads the entire file, including the new line/return characters.
Use

token = open("token.txt","r").readline() #To ignore the n character which may be present in the file.

or

bot.run(token.strip()) #To strip the string off, of such unnecessary characters.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/Rapptz/discord.py","title":"Rapptz/discord.py","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/Rapptz/discord.py"}},"updates":{"snippets":[{"icon":"PERSON","message":"@InfestusLeo in #937: Check the contents of 'token.txt' file.rnEven a new line/return character will affect the string, since you're using the read() function. _It basically reads the entire file, including the new line/return characters._rnrnUserntoken = open("token.txt","r").readline() #To ignore the \n character which may be present in the file.rnorrnbot.run(token.strip()) #To strip the string off, of such unnecessary characters.rnrn"}],"action":{"name":"View Issue","url":"https://github.com/Rapptz/discord.py/issues/937#issuecomment-359225840"}}}

All 3 comments

You probably have some whitespace characters in your token file, or you're trying to use a user token here, which would require you to have put bot=False in bot.run()

Or the token's wrong, or you're using client secret instead of the token, or the client ID, etc

Check the contents of 'token.txt' file.
Even a new line/return character will affect the string, since you're using the read() function. _It basically reads the entire file, including the new line/return characters._

Use
token = open("token.txt","r").readline() #To ignore the n character which may be present in the file.
or
bot.run(token.strip()) #To strip the string off, of such unnecessary characters.

thanks, I figured that out 😆not sure why nano does that, but it wasn't too hard to fix

Sent from my T-Mobile 4G LTE Device
-------- Original message --------From: Clinton Dcruz notifications@github.com Date: 1/21/18 12:48 AM (GMT-05:00) To: "Rapptz/discord.py" discord.py@noreply.github.com Cc: superloach superloach42@gmail.com, Author author@noreply.github.com Subject: Re: [Rapptz/discord.py] Can't seem to load token from file (#937)
Check the contents of 'token.txt' file.

Even a new line/return character will affect the string, since you're using the read() function. It basically reads the entire file, including the new line/return characters.
Use

token = open("token.txt","r").readline() #To ignore the n character which may be present in the file.

or

bot.run(token.strip()) #To strip the string off, of such unnecessary characters.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.

{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/Rapptz/discord.py","title":"Rapptz/discord.py","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/Rapptz/discord.py"}},"updates":{"snippets":[{"icon":"PERSON","message":"@InfestusLeo in #937: Check the contents of 'token.txt' file.rnEven a new line/return character will affect the string, since you're using the read() function. _It basically reads the entire file, including the new line/return characters._rnrnUserntoken = open("token.txt","r").readline() #To ignore the \n character which may be present in the file.rnorrnbot.run(token.strip()) #To strip the string off, of such unnecessary characters.rnrn"}],"action":{"name":"View Issue","url":"https://github.com/Rapptz/discord.py/issues/937#issuecomment-359225840"}}}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

0x78f1935 picture 0x78f1935  Â·  20Comments

VoxelPrismatic picture VoxelPrismatic  Â·  16Comments

Yuvira picture Yuvira  Â·  18Comments

ams2990 picture ams2990  Â·  14Comments

ThePiGuy24 picture ThePiGuy24  Â·  17Comments