Using Replit to make a discord bot

Hello! I am new to coding and just experimenting with Discord bots as I find it more enjoyable to learn by doing instead of taking in loads of information. I used the code from the freeCodeCamp tutorial (How to Create a Discord Bot for Free with Python – Full Tutorial)

Here is the code:

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('TOKEN'))

(I do have a .env file with the discord bot token that is up to date)

However when I run the code I get this lengthy error, I’m not too sure what it means and how to fix it

Traceback (most recent call last):
  File "main.py", line 18, in <module>
    client.run(os.getenv('TOKEN'))
  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'

Any help on how to fix it? If you need any further information I am happy to supply it :slight_smile:

Thanks in advance!

Hey there,

What’s the content of your .env file look like?

Is it:

TOKEN='faketokenhere'

Yes, the .env file is that. Only difference is I haven’t included ‘’ around it but adding them doesn’t seem to change the output

So the issue is that your getenv('TOKEN') call isn’t returning anything (which is throwing a NoneType error on the run call. Is your .env file in the root directory of your project?

I understand the first half of that, but not so sure on setting my .env file to the root directory. How do I check if it is and if it isn’t how do I change that?

It should be in your project’s folder, but not in a subfolder.

So is this ok?

You shouldn’t name your file token.env. You need to name it only .env - that’s the exact filename the function looks for.

To fix the problem I ran the code via PyCharm on my PC and instead of using a .env just put the token into main.py

I would highly recommend using a .env and not putting the token in directly(big security issue). You could use the python-dotenv package to also load env files without issues.

pip install python-dotenv
import os
import discord
from dotenv import load_dotenv

load_dotenv()

TOKEN = os.getenv('TOKEN')

7 posts were split to a new topic: Need help with discord bot

Hello! I am new to coding and just experimenting with Discord bots as I find it more enjoyable to learn by doing instead of taking in loads of information. I used the code from the freeCodeCamp tutorial (How to Create a Discord Bot for Free with Python – Full Tutorial )
I am getting this error version solver error
image

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