Able to log in and do whatever I want, but upon trying to send a message a 411 Length Required error is thrown
Just add \n to the end of your token
EDIT Apperently you need 2 newlines, so put \n\n at the end of your token
I expect that the invalid token was caught earlier, preferably before logging in
I could log in to my bot with an invalid key, and only sending a message threw an error
Traceback is here (imgur cuz I don't have the copy-paste)
1.0.1a RW3.7.3Kubuntu 18.10 Cosmic CuttlefishI have tested this with two bots on both the pypi version of this library and the most recent version, but I was unable to reproduce this. It throws an error on login because the authentication failed.
EDIT: For reference, this is the traceback that occurs when you try to log in.
Task exception was never retrieved
future: <Task finished coro=<Client.start() done, defined at C:\Users\zacha\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\client.py:511> exception=ConnectionClosed('WebSocket connection is closed: code = 4004 (private use), reason = Authentication failed.')>
Traceback (most recent call last):
File "C:\Users\zacha\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\gateway.py", line 469, in poll_event
msg = await self.recv()
File "C:\Users\zacha\AppData\Local\Programs\Python\Python37-32\lib\site-packages\websockets\protocol.py", line 350, in recv
yield from self.ensure_open()
File "C:\Users\zacha\AppData\Local\Programs\Python\Python37-32\lib\site-packages\websockets\protocol.py", line 512, in ensure_open
self.close_code, self.close_reason) from self.transfer_data_exc
websockets.exceptions.ConnectionClosed: WebSocket connection is closed: code = 4004 (private use), reason = Authentication failed.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\zacha\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\client.py", line 520, in start
await self.connect(reconnect=reconnect)
File "C:\Users\zacha\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\client.py", line 442, in connect
await self._connect()
File "C:\Users\zacha\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\client.py", line 406, in _connect
await self.ws.poll_event()
File "C:\Users\zacha\AppData\Local\Programs\Python\Python37-32\lib\site-packages\discord\gateway.py", line 477, in poll_event
raise ConnectionClosed(exc, shard_id=self.shard_id) from exc
discord.errors.ConnectionClosed: WebSocket connection is closed: code = 4004 (private use), reason = Authentication failed.
I'm not too sure, but given that you cannot reproduce this, it may be a Linux specific issue
EDIT
However, I highly doubt that that's the case
On linux, I get a similar, yet slightly different error when having a newline at the end of my token.
Traceback:
Traceback (most recent call last):
File "testbot.py", line 10, in <module>
bot.run(TOKEN_REDACTED)
File "/usr/local/lib/python3.6/dist-packages/discord/client.py", line 544, in run
self.loop.run_until_complete(runner())
File "/usr/lib/python3.6/asyncio/base_events.py", line 473, in run_until_complete
return future.result()
File "/usr/local/lib/python3.6/dist-packages/discord/client.py", line 539, in runner
await self.start(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/discord/client.py", line 510, in start
await self.login(*args, bot=bot)
File "/usr/local/lib/python3.6/dist-packages/discord/client.py", line 376, in login
await self.http.static_login(token, bot=bot)
File "/usr/local/lib/python3.6/dist-packages/discord/http.py", line 258, in static_login
data = await self.request(Route('GET', '/users/@me'))
File "/usr/local/lib/python3.6/dist-packages/discord/http.py", line 222, in request
raise HTTPException(r, data)
discord.errors.HTTPException: Bad Request (status code: 400): <html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>cloudflare</center>
</body>
</html>
Are you sure you did bot.run('{token}\n')?
yes
Interesting
Maybe it's because I put the key into a different file called secrets.txt...
with open('secrets.txt', 'r') as KEY: secret = KEY.readlines()
key = secret[0]
bot.run(key)
Can we please take this issue into the discord.py server?
Sure
My discord tag is !] VOX螢LIZ危D PRISM螞TIC#9244
Apperently 2 new lines are needed, according to Ava#4982 and Phantom#0004
By digging into the code, I have found that this could be a error caused by discord ignoring new lines when authenticating and discord.py not removing the new lines. A fix could be removing all the new lines off the token before authenticating : https://github.com/Rapptz/discord.py/blob/master/discord/client.py#L376
The reason why this error comes up is because when the requests are sent the newlines are included messing up the request.
The funny part is, you fail to log in with a single new line, but log in fine with two.... But yeah, that is an issue.
Because I'm not proficient in python, the following code is NOT the best to use, but is certainly good enough
await self.http.static_login(token.replace("\n", ""), bot=bot)
Better use
token.strip()
Just ran into this issue -- had TOKEN\n\n in a file and didn't strip it. Getting back a 411 isn't super-clear -- might not be a bad idea to throw a warning if the argument has newlines?