Encountering an issue when creating a Discord bot using PyCharm

Hey everyone! I'm new to making Discord bots and I'm running into a problem. I'm using PyCharm and Python, but when I try to run my script, I get this weird error:

```python
AttributeError: 'NoneType' object has no attribute 'strip'

It looks like it’s happening when the bot tries to log in. I think it might be something to do with the token, but I’m not sure. Has anyone else run into this before? Any ideas on how to fix it?

Here’s a bit of my code that might be relevant:

import discord
import os
from dotenv import load_dotenv

load_dotenv()

bot = discord.Client()

@bot.event
async def on_ready():
    print(f'{bot.user} is online!')

bot.run(os.getenv('BOT_TOKEN'))

I’d really appreciate any help or tips! Thanks in advance!

I’ve been through this exact problem before, and it’s usually related to how the environment variables are handled. Make sure your .env file is in the correct location and that PyCharm is set up to read it properly. One thing that helped me was adding a print statement right after load_dotenv() to verify the token was actually being loaded:

load_dotenv()
print(f"Token loaded: {os.getenv('BOT_TOKEN')}")

If it prints None or an empty string, you know the issue is with loading the environment variable. Also, double-check that you’re not accidentally overwriting the BOT_TOKEN variable somewhere else in your code. If all else fails, try using a different method to load the token, like reading from a config file instead of using dotenv. Hope this helps!

yo man, sounds like ur token might be empty or not loading right. check ur .env file & make sure BOT_TOKEN is set correctly. also double-check that load_dotenv() is actually working. if ur still stuck, try printing the token to see whats up. good luck!

I encountered a similar issue when setting up my Discord bot. The error typically happens when the token is not properly loaded or is missing. I recommend checking that your .env file is in the same directory as your script and that the BOT_TOKEN is correctly entered without any extra spaces. You could also try inserting the token directly into the script temporarily to verify that the token itself is valid. If the direct token works, then the problem lies in how the environment variables are loaded, which might involve your configuration settings in PyCharm.