Error: "Improper token has been passed" discord.py
Beginning to learn to write a simple Discord bot in Python. My bot’s token is in a .env file but I’m getting the error discord.errors.LoginFailure: Improper token has been passed.
Putting the token in client.run(TOKEN_HERE)
doesn’t work for me as it does with some people.
I’m using the token from the Bot page on the Discord Developer Portal not the Client Secret from the General Information page.
I’m using Visual Studio Code.
bot.py:
import os import discord from dotenv import load_dotenv load_dotenv() TOKEN = os.getenv('DISCORD_TOKEN') client = discord.Client() @client.event async def on_ready(): print(f'{client.user} has connected to Discord!') client.run(TOKEN)
.env:
DISCORD_TOKEN=secret_token_here
Full error output:
Eds-MacBook-Pro:~ ed$ python3 /users/ed/documents/supremesauce/supremesauce.py Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/http.py", line 261, in static_login data = await self.request(Route('GET', '/users/@me')) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/http.py", line 225, in request raise HTTPException(r, data) discord.errors.HTTPException: 401 Unauthorized (error code: 0): 401: Unauthorized The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/users/ed/documents/supremesauce/supremesauce.py", line 15, in <module> client.run(TOKEN) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 640, in run return future.result() File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 621, in runner await self.start(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 584, in start await self.login(*args, bot=bot) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 442, in login await self.http.static_login(token.strip(), bot=bot) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/http.py", line 265, in static_login raise LoginFailure('Improper token has been passed.') from exc discord.errors.LoginFailure: Improper token has been passed. Eds-MacBook-Pro:~ ed$
Thanks 🙂