I'm trying to make a discord bot, and when I try to load a .env with load_dotenv() it doesn't work because it says
Traceback (most recent call last):
File "/home/fanjin/Documents/Python Projects/Discord Bot/bot.py", line 15, in <module>
client.run(TOKEN)
File "/home/fanjin/.local/lib/python3.8/site-packages/discord/client.py", line 708, in run
return future.result()
File "/home/fanjin/.local/lib/python3.8/site-packages/discord/client.py", line 687, in runner
await self.start(*args, **kwargs)
File "/home/fanjin/.local/lib/python3.8/site-packages/discord/client.py", line 650, in start
await self.login(*args, bot=bot)
File "/home/fanjin/.local/lib/python3.8/site-packages/discord/client.py", line 499, in login
await self.http.static_login(token.strip(), bot=bot)
AttributeError: 'NoneType' object has no attribute 'strip
Here's my code for the bot:
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)
And the save.env file: (It's a fake token)
# .env
DISCORD_TOKEN={Bzc0NjfUH8fEWFjg2NDMyMjY2.X6coqw.JyiOR89JIH7fFFoyOMufK_1A}
Both files are in the same directory, and I even tried to explicitly specify the .env's path with
env_path = Path('path/to/file') / '.env'
load_dotenv(dotenv_path=env_path)
but that also didn't work