I'm following a basic tutorial for a Python Discord bot on YouTube and my code is underneath. It says:
discord.errors.LoginFailure: Improper token has been passed.
Before anyone asks, yes I have put in the bot token, not the id or secret.
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client()
client = commands.Bot(command_prefix = "!")
#client.event
async def on_ready():
print("Bot is ready!")
#client.event
async def on_message(message):
if message.content == "cookie":
await client.send_message(message.channel, ":cookie:")
client.run("token is here")
For me, my bot.py file looks like this:
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)
and since I used env (environment) modules, I created a new file with an empty name and with the .env extension in the same path in a folder. The env file only has this line of code:
DISCORD_TOKEN=DFHKJAhka7fdsKHJASDFk1jhaf5afd.HASDFafd23FHdfa_adfahHJKADF32W
So the problem for me was I was using brackets around the token code and after I removed it, it worked!
My code when it had brackets:
DISCORD_TOKEN={DFHKJAhka7fdsKHJASDFk1jhaf5afd.HASDFafd23FHdfa_adfahHJKADF32W}
Make sure you grab the "Token" from the "Bot" page in the Discord development site, rather than the "Secret" from the "General Information" page.
I was having the same problem. My issue was solved by using the correct token from the Discord app page. I was using the "Secret" from the 'General Information' page (which generated the error in the original post for me) instead of the "Token" from the "Bot" page.
As sheneb said in the comment to this, this answer (probably) won't help the OP (since the question now says "Before anyone asks, yes I have put in the bot token, not the id or secret"). However, I found this question/page when searching for the answer, and my issue was solved with this information.
The same thing happened to me, but it started working when I went to the developer page and refreshed the token. Then I just put the new token in the code and it worked!
Maybe the same will work for you...?
You should be able to get it to work by doing this:
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client()
client = commands.Bot(command_prefix = "!")
#client.event
async def on_ready():
print("Bot is ready!")
#client.event
async def on_message(message):
if message.content == "cookie":
await message.client.send(":cookie:")
client.run("token is here", bot=True)
I do this sometimes, but also, check to make sure you have fully created your bot by taking a look at the "bot" tab on your developer page.
I also made a fix for your message send line. It was out of date ;).
Try this:
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client()
client = commands.Bot(command_prefix = "!")
#client.event
async def on_ready():
print("Bot is ready!")
#client.event
async def on_message(message):
if message.content == "cookie":
await message.client.send(":cookie:")
client.run("TOKEN HERE. PUT YOUR TOKEN HERE ;)")
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client()
client = commands.Bot(command_prefix = "!")
#client.event
async def on_ready():
print("Bot is ready!")
#client.event
async def on_message(message):
if message.content == "cookie":
await message.client.send(":cookie:")
client.run("PUT YOUR TOKEN HERE")
Related
I come with a problem that I can't solve. However, I'm trying to create a slash command but it doesn't work too well. If anyone knows the answer thank you very much. (i want it in cogs)
I tried a few things like discord_slash module or similar:
import discord
import discord.ext
from discord_slash import SlashCommand
from discord_slash import SlashContext
client = discord.Client()
client = commands.Bot(command_prefix = "!")
slash = SlashCommand(client, sync_commands=True)
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.online, activity=discord.Game(name='Discord'))
print("Bot online")
#slash.slash(name="ping", description="Ping Pong")
async def _help(ctx: SlashContext):
await ctx.send(content="pong!")
client.run(os.getenv("token"))
i have a problem with my discord bot: when i type on discord $ping he doesn't response me pong and i don't know why, i just check the bot have the admin role so he can write, i'm using VsCode and he didn't give me nothing error.
Here is the code
import discord
from discord.ext import commands
import requests
import json
import random
client = discord.Client()
bot = commands.Bot(command_prefix='$')
#bot.command()
async def ping(ctx):
await ctx.channel.send("pong")
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
client.run("XXXXXXXXXXXXXXXXXXXXXXX")
The problem is, that you are defining the command with bot.command, but you only do client.run. To fix this, either choose a client, or a bot, but not both, like this if you choose bot for example:
import discord
from discord.ext import commands
import json
import random
bot = commands.Bot(command_prefix='$')
#bot.command()
async def ping(ctx):
await ctx.channel.send("pong")
#bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
bot.run(Token)
Also don't use requests, since it's blocking.
Hello guys im trying to write a code that gives me the discord server owner but its giving Me 'None'
import discord
client = discord.Client()
TOKEN = 'token'
#client.event
async def on_message(message):
if message.content.find("getowner") != -1:
await message.channel.send(str(message.guild.owner))
client.run(TOKEN)
Can someone please help me with this bot thanks!!
I want to get the discord servers owner by typing getowner in a text channel.
I recommend using it as a command rather than doing on_message, you can do this:
from discord.ext import commands
token = "Token"
client = commands.Bot(command_prefix="!") #Use any prefix
#client.command(pass_context=True)
async def getOwner(ctx):
#await ctx.channel.send(str(ctx.guild.owner.display_name))
await ctx.channel.send(str(ctx.guild.owner))
client.run(TOKEN)
But if you don't want to use a command, you could use regular expression and just keep it as:
import discord
import re
client = discord.Client()
TOKEN = 'token'
#client.event
async def on_message(ctx):
if re.match("getowner", ctx.content):
await ctx.channel.send(str(ctx.guild.owner))
client.run(TOKEN)
EDIT: Also try using intents with your bot code.
import discord
import re
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
TOKEN = "token"
#client.event
async def on_message(ctx):
if re.match("getowner", ctx.content):
await ctx.channel.send(str(ctx.guild.owner))
client.run(TOKEN)
Same changes can be done with the command version.
It's very simple;
from discord.ext import commands
#client.command(name="getowner")
async def getowner(ctx):
await ctx.send(str(ctx.guild.owner))
Hello everyone i was making a bot using discord.py rewrite on pycharm but once there was a error on one of my cogs but the console didn't show any error and it didn't send any message so I had to search a lot about the error. I want to know why its like that.
My main code of the bot is:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="your prefix")
client.load_extension("extension name")
#client.event
async def on_ready():
print("bot is ready")
client.run('your token
I'm not sure why it doesn't but google will sure help!
You forgot to end the string at client.run so here's the correct code.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="your prefix")
client.load_extension("extension name")
#client.event
async def on_ready():
print("bot is ready")
client.run('your token')
it may hard to me, but i believe on stackoverflow power,
i need to put a number as bot status on my bot.
Here : http://gamers-control-2.000webhostapp.com/count.txt
Also Pic of it : Picture
#time to show status = text that founded in blank website = "41"
#http://gamers-control-2.000webhostapp.com/count.txt
#some how, you say i can type the number, i say the number in web it
change everytime, so i need to get that number to show it as bot status.
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from discord import Game
Client = discord.client
client = commands.Bot(command_prefix = '!')
Clientdiscord = discord.Client()
#client.event
async def on_ready():
await client.change_presence(game=discord.Game(name=http://gamers-control-2.000webhostapp.com/count.txt, type=3))
print('test ready')
client.run("....")
im new with discord.py
You need to first import requests at the beginning
import requests
Then before the ready event
count = requests.get('http://gamers-control-2.000webhostapp.com/count.txt')
And then you set it to
await client.change_presence(game=discord.Game(name=count.text, type=3))
I use await bot.change_presence() like this:
NOTICE:
I am leaving out part of my code
#bot.event
async def on_ready():
replit.clear()
print(f'Logged in as {bot.user.name} - {bot.user.id}')
bot.remove_command('help')
await bot.change_presence(status=discord.Status.online, activity=discord.Game("mod help"))
This is what i use
async def on_ready():
print(f'We have logged in as {bot.user}')
print('Ready!')
return await bot.change_presence(activity=discord.Activity(type=3, name='Add status here'))```