#๐Ÿ”’ .env issues

76 messages ยท Page 1 of 1 (latest)

spark halo
#

Heya! Issue #2 for today. Many more to come.
I get this error when trying to run my code:
Traceback (most recent call last):
File "c:\Users\REDACTED\Desktop\The Pikachu Crew\The Pikachu Crew Bot\main_code\script.py", line 5, in <module>
from dotenv import load_dotenv
ModuleNotFoundError: No module named 'dotenv'

Here is my code:

import os
import discord


from dotenv import load_dotenv
load_dotenv(".env")
TOKEN = os.getenv('BOT_TOKEN')
GUILD = os.getenv('GUILD')
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
client = discord.Client(intents=discord.Intents.default())

bot = commands.Bot(command_prefix='!', intents=intents)

@client.event
async def on_ready():
    print(f'Logged in as {bot.user}')
    print('------')

client.run(TOKEN)

any help is appreciated!

coral ibexBOT
#

@spark halo

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dense cloak
spark halo
#

ive installed it tho

#

yeah its installed

burnt lintel
#

can you run py -0

spark halo
#
PS C:\Users\redacted\Desktop\The Pikachu Crew\The Pikachu Crew Bot> pip install python-dotenv
Requirement already satisfied: python-dotenv in c:\users\redacted\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages (1.0.1)
WARNING: You are using pip version 22.0.4; however, version 24.1 is available.
You should consider upgrading via the 'C:\Users\redacted\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.exe -m pip install --upgrade pip' command.
#

ok

burnt lintel
#

ugh

spark halo
#
 -V:3.12 *        Python 3.12 (64-bit)
 -V:3.9           Python 3.9 (Store)
burnt lintel
#

i'd uninstall 3.9

spark halo
#

doing that now

#

done

#

still same issue

burnt lintel
#

restart VSC

#

restart your terminals if you have them open too

spark halo
#

done both of those and still same issue

burnt lintel
#

be patient.

#

run py -0 again

spark halo
#
-V:3.12 *        Python 3.12 (64-bit)
burnt lintel
#

cool, now install python-dotenv again

spark halo
#

okie

#

aand pip got uninstalled

burnt lintel
#

then do py -m pip install python-dotenv

spark halo
#

ty

#

new issue

#
PS C:\Users\no\Desktop\The Pikachu Crew\The Pikachu Crew Bot> & C:/Users/no/AppData/Local/Programs/Python/Python312/python.exe "c:/Users/no/Desktop/The Pikachu Crew/The Pikachu Crew Bot/main_code/script.py"
Traceback (most recent call last):
  File "c:\Users\no\Desktop\The Pikachu Crew\The Pikachu Crew Bot\main_code\script.py", line 14, in <module>
    bot = commands.Bot(command_prefix='!', intents=intents)
          ^^^^^^^^
NameError: name 'commands' is not defined
burnt lintel
#

you should go over the discord.py documentation and learn it's basics

spark halo
#

yeah that might be useful

#

ty for the help

#

wait one more thing

#

removed the commands bit since will try and do some other stuff before commands and got these (had this multiple times before and still have no clue how to fix it)

PS C:\Users\a\Desktop\The Pikachu Crew\The Pikachu Crew Bot> & C:/Users/a/AppData/Local/Programs/Python/Python312/python.exe "c:/Users/a/Desktop/The Pikachu Crew/The Pikachu Crew Bot/main_code/script.py"
2024-06-23 17:57:08 INFO     discord.client logging in using static token
Traceback (most recent call last):
  File "c:\Users\a\Desktop\The Pikachu Crew\The Pikachu Crew Bot\main_code\script.py", line 19, in <module>
    client.run(TOKEN)
  File "C:\Users\a\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\client.py", line 869, in run
    asyncio.run(runner())
  File "C:\Users\a\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\a\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\a\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\a\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\client.py", line 858, in runner
    await self.start(token, reconnect=reconnect)
  File "C:\Users\a\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\client.py", line 786, in start
    await self.login(token)
  File "C:\Users\a\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\client.py", line 617, 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

its smth to do with .env but god knows why or how

#

it keeps happening as well. happened at least 3 times today

#

@burnt lintel

spark halo
fathom crow
#

what does your project file structure and the contents of your .env file looks like?
(replace the actual token by something like 'abcxyz', and optionally the guild ID by some random number, but keep the rest as is)

spark halo
#

this is the .env

#

and this is the code and structure

fathom crow
#

move .env up one folder

spark halo
#

so just into the main 'The Pikachu Crew Bot'

fathom crow
#

yes

spark halo
#

and now it works

#

typical

#

thanks

#

now I move onto ping and mod commands

#

probs welcome messages first

fathom crow
#

and you probably should use a bot instead of a client, ```py
from discord.ext import commands

bot = commands.Bot()

use bot instead of client everywhere else

spark halo
#

ok

#

wait

#

nw error

fathom crow
#

name error because bot is not defined?

spark halo
#
2024-06-23 18:03:59 INFO     discord.client logging in using static token
2024-06-23 18:04:00 INFO     discord.gateway Shard ID None has connected to Gateway (Session ID: c401323ecb2fadb789e7d989b04e87e2).
2024-06-23 18:04:02 ERROR    discord.client Ignoring exception in on_ready
Traceback (most recent call last):
  File "C:\Users\zrees\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\client.py", line 449, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\zrees\Desktop\The Pikachu Crew\The Pikachu Crew Bot\main_code\script.py", line 16, in on_ready
    print(f'Logged in as {bot.user}')
                          ^^^
NameError: name 'bot' is not defined

#

yeah

fathom crow
#

a Bot can do everything a Client can, but with some extra features
some tutorials might use client, but overall you should prefer bot if you want to add commands

spark halo
#

so client<bot

fathom crow
#

p much

spark halo
#

alrighty now its fixed

#

wml on welcome and boost messages

#

i need to figure out how to use embeds for em

#

and images

burnt lintel
#

discord bots arent simple, you should learn the basics of python and then look into how async works + discord.py in general

spark halo
#

yeah will do

spark halo
#

@fathom crow

burnt lintel
#

just copying code is pointless if you dont understand it

spark halo
#

I understand it lmao

#

it just doesnt work

#

and thats what im tryna fix

burnt lintel
#

if you understood it yo would know how to fix it .-.

spark halo
#

I found out why!

#

i was being stupid

#

it was supposed to only work in dms

#

and not the server

coral ibexBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.