-2

I was programing a python discord bot. I got this error:

error image

Code:

    # jokeybotmain.py
import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('ODg0NjkxODkzNTk2ODU2MzQw.YTcLiA.pKu1RIyV2HOTFhLOWQX0ryliyco')

client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

client.run(TOKEN)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 4
    You seem to have used your **actual token** as the environment variable name, which probably doesn't exist. Also that means you've publicly shared your credentials so now need to rotate them. The point of using env vars is to _not_ have secrets in the source code. – jonrsharpe Sep 07 '21 at 07:24

2 Answers2

0

Error


Your problem is that you put the token (copied from discord developer portal) as a parameter for os.getenv.
You can instead remove the part about dotenv and make your bot work, like this:

import discord
TOKEN = "Your token here" # Remember to delete it before sending your code
# Use Bot instead of Client
# https://stackoverflow.com/questions/51234778/what-are-the-differences-between-bot-and-client
bot = discord.Bot(prefixes=['!'], intents=discord.Intents.all())
Bot.run(TOKEN)
FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
0
import discord
from discord.ext import commands

TOKEN = "YOUR_TOKEN"
client = commands.Bot(command_prefix="!")

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

client.run(TOKEN)
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77