I tried to follow on how to make a discord bot tutorial but all i get is an error

Hi @KronosZ !

Welcome to the forum!

It is always best to write code directly in the forum instead of posting screenshots.

But the error message is telling you that my_secret is not defined.

And after taking a quick glance at your code I don’t see where you defined it.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thank you for responding but i am new to this and i do not know how to define “(my_secret)”

Please read through my post on how to setup your bot token in replit.

1 Like

I followed your posts steps but there is still an error, this is the code i copied so i dont get it messed up

import os
import discord

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!')


my_secret = os.environ['TOKEN']
client.run(my_secret)

and this is the error i get

Traceback (most recent call last):
File “main.py”, line 19, in
my_secret = os.environ[‘TOKEN’]
File “/usr/lib/python3.8/os.py”, line 675, in getitem
raise KeyError(key) from None
KeyError: ‘TOKEN’

have you added the bot token in the Secrets? have you used the name TOKEN or a different one?

yes i have tried that ive tried everything and it doesnt work out

the error says you don’t have an environment variable named TOKEN, so I suggest you try again

Nevermind my code was sucessful thanks for the help

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