Discord Bots in Python - 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.

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)

Why is my discord bot code not running anything?

Okay so I wanted to make a discord bot for my first python project. However, despite my seemingly correct code, the bot doesn't run:
I used this basic code to simply get the bot online but nothing happens. I haven't seen anyone else have this problem either.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print("Bot is ready")
client.run("Token") # i removed the token for security
and this is the result of the program
Am I doing something wrong?
I am using this version of the discord botting API: https://github.com/Rapptz/discord.py
As #jwjhdev said, client.run is not supposed to be tabbed under on_ready. If you delete the tab, then everything should work perfectly.

python doesn't connect to discord token

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.

Grab User Info With Discord.py

I'm new to coding and need a discord python command that can return the user's avatar and name that requested it. I've tried looking at the documentation but don't really understand it. Any help is appreciated.
You can use this code snippet to get the user's name and avatar. From here you could then process it and return a message using.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.content.startswith('!user'):
sender = message.author
name = sender.display_name
avatar_url = sender.avatar_url
await message.channel.send(f'Hello, {name}. Your avatar URL is {avatar_url}.')
client.run('your token here')
If you don't have the discord python module installed, you can do so by running pip install discord.py and you should be good.
If you haven't yet set up your app in the discord developer console, you must do that prior to running this code snippet, adding in the bot token on the last line. If you haven't done this already, a good tutorial I can recommend can be found here.

Categories