Python discord bot errors

Question:

I am getting the

TypeError: expected token to be a str, received NoneType instead

error in python when trying to run my bot. Here is the full error:

Traceback (most recent call last):
  File "d:PythonProjectsdisco botppap", line 20, in <module>
    client.run(os.getenv('TOKEN'))
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libsite-packagesdiscordclient.py", line 828, in run
    asyncio.run(runner())
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libasynciorunners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libasynciobase_events.py", line 647, in run_until_complete
    return future.result()
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libsite-packagesdiscordclient.py", line 817, in runner
    await self.start(token, reconnect=reconnect)
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libsite-packagesdiscordclient.py", line 745, in start
    await self.login(token)
  File "C:UsersAdminAppDataLocalProgramsPythonPython39libsite-packagesdiscordclient.py", line 577, in login
    raise TypeError(f'expected token to be a str, received {token.__class__.__name__} instead')
TypeError: expected token to be a str, received NoneType instead

This is my code:

import discord
import os
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

client.run(os.getenv('TOKEN'))

I dont have any extra files with the code. This is the only file in the project.
How would i fix the error? Thanks!

I have looked on other peoples posts but nothing has helped so far.

Asked By: turtlesXD

||

Answers:

Token Value Must Be A String

So , If You Are Using .env File For Token
Use Something Like :

import os
from dotenv import load_dotenv # pip install dotenv
load_dotenv() # Load Every .env file in the application path

.... # Stuff

bot.run(os.getenv("BOTTOKEN"))

Answered By: ArDaVaN81