Home computickets How to add slash commands to a discord bot

How to add slash commands to a discord bot

Author

Date

Category

After looking at several sites, including the official discord documentation, I had several options.

1 – json.
I’ve tried adding a slash command via the requests library. But getting no error or result, I threw json

2- discord-py-slash-command . similar result with json.

After the final failure, I went to stackoverflow , and now I’m asking a question.

Codes:

Json –

import requests
url = 'https: //discord.com/api/v8/applications/#id_application/guilds/#guild_id/commands'
json = {
  "name": "test",
  "description": "Super test",
}
headers = {
  "Authorization": "Bot # token"
}
r = requests.post (url, headers = headers, json = json)

discord-py-slash-command (IN COGS) –

import discord
from discord.ext import commands
from discord_slash import cog_ext, SlashContext
class Slash1 (commands.Cog):
  def __init __ (self, bot: commands.Bot):
    self.bot = bot
  @ cog_ext.cog_slash (name = "ping")
  async def ping (self, ctx: SlashContext):
    await ctx.send (content = "Pong!")
def setup (bot: commands.Bot):
  bot.add_cog (Slash1 (bot))

Answer 1

After updating the slash commands code, you need to wait some time, since the rate limit is triggered and you need to re-invite the bot to the server with slash commands already enabled, you can enable them on the website https://discord.com/developers/

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions