Help with Discord Bot tutorial

import discord
import os

client = discord.Client()

@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('Key'))

Error Msg

Traceback (most recent call last):
  File "main.py", line 18, in <module>
    client.run(os.getenv('Key'))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 723, in run
    return future.result()
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 702, in runner
    await self.start(*args, **kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 665, in start
    await self.login(*args, bot=bot)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 511, in login
    await self.http.static_login(token.strip(), bot=bot)
AttributeError: 'NoneType' object has no attribute 'strip'```

I think this is an issue with how the environment is set up.
Most probably the os.getenv() method isn’t able to fetch the environment variable.
If your .env file is set up correctly, maybe you can consider using python-dotenv package to load the file.
Here’s an example of how to use it-

import os
from dotenv import load_dotenv

load_dotenv()

KEY = os.getenv('Key')

You can install the package using-
pip install python-dotenv

Also, I have a small suggestion(this is nothing big nor does it change anything, however it does help in some form of consistency) - Try to define the environment variables in CAPS(upper case)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.