Slash commands not working using discord.py - python

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

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 won't go online

I've just started learn python and my discord bot won't go online. it just said
" Process exit with exit code 0". And there's no error with the code.
here's my code. enter image description here
Add await client.process_commands(ctx) and #client.event don't need parentheses(). Use the code given below:
#client.event
async def on_message(ctx):
if ctx.author == client.user:
return
await client.process_commands(ctx)
Use this entire code if it still not working:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_ready():
print('We are logged in!')
#bot.event
async def on_message(message):
if message.author==bot.user:
return
await bot.process_commands(message)
#bot.command()
async def ping(message) :
await message.channel.send("Pong!!")
bot.run("TOKEN")
You should try this instead
import os
import discord
from discord.ext import commands
discord_token = "Your Token"
client = discord.Client()
bot = commands.Bot(command_prefix="s!")
#bot.event
async def on_ready():
print("running")
bot.run(discord_token)
#bot.command()
async def on_ready():
print("running")
bot.run(discord_token)
and the output should be running
You should try this instead
import os
import discord
from discord.ext import commands
discord_token = "Your Token"
client = discord.Client()
bot = commands.Bot(client_prefix="!")
#client.command()
async def on_ready():
print("running")
bot.run(discord_token)
The code you gave is wrong, this is correct:
import os
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!") # you don't need an extra discord.client Bot is enough
token = os.getenv("TOKEN")
#you should not run the program here you should run it at last
#bot.event #no need for brackets
async def on_ready():
print("running")
#bot.event
async def on_message(ctx):
if ctx.author == client.user:
return
#bot.command(name="span",description="YOUR DESCRIPTION HERE") # it is command and not commands
async def span_(ctx,amount:int,*,message):
for i in range(amount):
await ctx.send(message)
bot.run(token) #you should run the bot now only and this will work perfectly!
You can find the the documentation for discord.py here.

Discord command bot doesn't responde (python)

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.

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