Commands for discord bot not working (Python) - python

I am not sure why the commands don't work. Whenever I enter !hello into my discord server the bot doesnt respond.
import discord
from discord.ext import commands
import os
my_secret = os.environ['Token']
client = commands.Bot(command_prefix='!')
#client.command(pass_context=True)
async def hello(ctx):
await ctx.send("Hello")
client.run(my_secret)

This should Work
I just tested it and it worked fine
did you install Discord? and did you wrote your Token right?
Also you should put this Code in yours so you see when the Bot is ready
#client.event
async def on_ready():
print(" ")
print("Bot started! | Informations:")
print(f"- Name: {client.user.name}")
print(f"- Bot-ID: {client.user.id}")
print(f"- Server using {client.user.name}:",
len(client.guilds))

Related

My dicord bot doesn't respond to commands

from discord.ext import commands
import discord
bot = commands.Bot(command_prefix="/", intents=discord.Intents.default())
TOKEN = "TOKEN"
#bot.event
async def on_ready():
print(f'Bot connected as {bot.user}')
#bot.command()
async def dosomething(ctx):
await ctx.send("I did something")
bot.run(TOKEN)
Bot is active and everything but when I try out /dosomething command nothing happens.
Change The Prefix To Anything Else But Not "/"
And Try Intents.all()
As i Know , Using /commands Needs Other libraries
Like This : discord_slash

Discord bot is not running for some reason CLOSED

I need help with my bot, I coded it all before but then something happened and I made a new one and it just isn’t working when I run the code it works fine but the bot isn’t online or responding to the commands
import discord
from keep_alive import keep_alive
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('$cmds'):
await message.channel.send("Hello! how are you, my current commands are $cmds and $ping more coming soon!") keep_alive() client.run(os.getenv("Token"))
enter image description here
[enter image description here][2]
[2]: https://i.stack.imgur.com/URmvr.jpg![enter image description here](https://i.stack.imgur.com/ei20c.jpg)
It is recommended that you use the commands framework :-
import discord
from discord.ext import commands
#client.command()
async def cmds(ctx):
await ctx.send("Hello! how are you, my current commands are $cmds and $ping more coming soon!")

Why errors in my cogs won't show in console in pycharm

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

my commands dont work in my discord server

im trying to make a bot for my discord server, but all my commands dont work.
im using windows and pycharm to test and use the bot. i tried many different types but nothing works. im using python 3.7
import discord
from discord.ext import commands
import asyncio
from discord.ext.commands import Bot
Client = discord.Client()
client = commands.Bot(command_prefix='.')
#client.event
async def on_ready():
print("bot is active")
#client.command()
async def ping(ctx):
await ctx.send('pong')
await print("pong")
it doesn't crash or give an error, it just doesn't do anything in the command
Try this:
import discord
from discord.ext import commands
TOKEN = YOUR_TOKEN_GOES_HERE
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print("Powering up the bot")
#client.event
async def on_message(message):
print("A new message!")
await client.process_commands(message)
#client.command()
async def ping(ctx):
await ctx.channel.send("Pong!")

Improper token passed

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")

Categories