python doesn't connect to discord token - python

So I am very much a newbie, I am trying to connect a discord bot to python. I've followed multiple tutorials but all of them result in the same problem:
TOKEN = (<the token I am using>)
NameError: name '<the token I am using>' is not defined
I am not sure what is wrong? I am on Mac and using PyCharm CE.
import discord
import os
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
client = discord.Client()
TOKEN = (my token)
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)

The question is quite unclear, however, python NameError is usually caused by:
Misspelled Variable or Function Name
So you probably didn't put quotes around the token itself to make it a string or something as simple as that.

The token value needs to be in the form of a string. That means, it is of the form
TOKEN = "198249184AB8"
Notice the quotes around the value.

Related

What is and How to fix this error in discord.py?

I was using replit (maybe because replit doesnt work?) tryoing to make some code for a Discord bot when I saw this:
Type error: __init__ missing 1 required keyword-only argument: "intents"
Im not really sure what is means
Heres my code (BTW i used pip install discord in shell)
`
import discord
token = "mytoken (not revealing it i guess)"# but even with right token it doesnt work
client = discord.Client()
name = "MIIB.BOT_v1.0"
#client.event
async def on_ready():
print("Bot logged in as ", name, "!")
client.run(token)
on_ready()
#i did *****pip install discord****** in shell btw
`
I tried some variations but not much. I expected:
Bot logged in as botname#6969
Replace
client = discord.Client()
with
client = discord.Client(intents=discord.Intents.default())
As per recently(-ish), you need to specify the intents your bot uses when login to the API, so you should populate the Client argument with an intent object.
Calling the default intents, if you don't plan on doing anything with any other that you might need to enable, would look like this
bot_intents = discord.Intents.default()
client = discord.Client(intents=bot_intents)

Unable to set up discord bot to get connected will not set up authentication token

I'm following a tutorial to set up a discord bot with python and am using Python IDLE shell 3.9.6
So, far I'm just trying to get the bot to connect but am running into issues with it accepting my key and after changing where it is calling to access the bot key. Which I have saved as the .env file type
import os
import discord
from dotenv import load_dotenv
load_dotenv("---.env")
TOKEN = os.getenv()
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)
Error Message:
line 8, in <module>
TOKEN = os.getenv()
TypeError: getenv() missing 1 required positional argument: 'key'
os.getenv() method in Python returns the value of the environment variable key.
You need to specify which key you want to grab from the env file, for example:
TOKEN = os.getenv(foo)
TOKEN = os.getenv(my_key)
From what I read in the comments your key is DISCORD_TOKEN so
TOKEN = os.getenv(DISCORD_TOKEN)

Python discord bot -- AttributeError: 'NoneType' object has no attribute 'strip'

I am developing a Discord Bot using Python. And getting the following error (AttributeError: 'NoneType' object has no attribute 'strip'). Here is my code.
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('O.......')
GUILD = os.getenv('CodeUP')
client = discord.Client()
#client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
client.run(TOKEN)
The thing you're supposed to get as the "token" is the variable name of your bot token stored in the .env file, like this!
Contents of .env file:
BOT_TOKEN=ODMyMTUxNjQ4OTAxMjY3NTA2.YHfnnQ.r_rQ2mmo8HFvaBAl9rry28VM4Nk
Token variable in the python file:
TOKEN = os.getenv('BOT_TOKEN')
os.getenv gets a enviroment variable with the given name. You are using your token as your name. Replace the code with this:
TOKEN = os.getenv('DISCORD_TOKEN', 'ODMyMTUxNjQ4OTAxMjY3NTA2.YHfnnQ.r_rQ2mmo8HFvaBAl9rry28VM4Nk')
This find an environment variable named DISCORD_TOKEN, and if none exists, use 'ODM...'
I hope this is not your real token you are posting on the internet though, if so make sure to cancel it immediately.

Discord Bots in Python

I was following a tutorial on making discord bots when I came across a problem.
I'm trying to connect the client to discord using my token, using the code below:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)
`
I also have a separate .env file which looks like this:
DISCORD_TOKEN="***" ## yeah I'm not giving anyone my token
I ran this in the command prompt and I got this error:
File "discordBot.py", line 15, in <module>
client.run(***)
NameError: name '***' is not defined ## Once again not actually my token
The only thing I've noticed is that the name that isn't defined isn't actually the full token and it stops after a ".", I tried putting the entire thing in quotes on the .env file but that didn't fix it.
This is a client side problem. It would be useful if you could post in your question what service you are using to host the bot. It is a problem with either your server or something with your API key. Make sure the key is right. I don't see anything wrong with your program, if you are saving the responses in a file on your computer make sure its in RB or WB format. Other formats will cause your program to glitch. Best of luck with your bot! This also might be because you are asking the user for an API key and he does not have one. You should switch your async data.

Why is my bot.command function in discord.py not working?

I have tried looking around the internet for answers but I'm not finding anything that fixes my problem:
import os
import asyncio
import discord
from discord.ext import commands
token = "here is my token"
bot = discord.Client()
bot = commands.Bot(command_prefix='!')
#bot.command()
async def length(ctx):
await ctx.send('your message is {} characters long.'.format(len(ctx.message.content)))
print("test print")
bot.run(token)
The length function does not work at all, and its not printing test print in the console.
Does anyone know what the problem is? there are a few other bot.xxx functions in there that works.
It does not work because you have double bot.
Your should only have bot = commands.Bot(command_prefix='!')

Categories