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:\Python\Projects\disco bot\ppap", line 20, in <module>
client.run(os.getenv('TOKEN'))
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 828, in run
asyncio.run(runner())
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 647, in run_until_complete
return future.result()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 817, in runner
await self.start(token, reconnect=reconnect)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 745, in start
await self.login(token)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.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.
CodePudding user response:
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"))