-
Notifications
You must be signed in to change notification settings - Fork 187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Properly registering a slash command with the API #9
Comments
If you're curious, here's my code at the moment. I haven't implemented any calls to import discord
import logging
from discord.ext import commands
from discord_slash import SlashCommand
from discord_slash.model import SlashContext
from time import sleep
logging.basicConfig(level=logging.INFO, format="[%(asctime)s] %(levelname)s %(name)s | %(message)s")
bot = commands.Bot(command_prefix="!", intents=discord.Intents.default())
slash = SlashCommand(bot)
allowedIds = [396395639791026196]
@slash.slash(name="test", description="Sends a test message.", guild_ids=allowedIds)
async def _test(ctx: SlashContext):
embed = discord.Embed(title="Here's an embed.",
color=0xeb4034)
await ctx.send(3, content="This is a test. Only you can see this.",
embeds=[embed], hidden=True)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user.name} with ID {bot.user.id}")
print(f"{len(slash.commands)} command(s) are registered.")
sleep(0.5)
bot.run("token") |
Well, |
After I figured out how the registering system worked, I made a short script that contains functions for auto-registering commands. I have this in import discord_slash
import discord_slash.utils.manage_commands
import aiohttp
from typing import Tuple
async def addToAPI(SlashCommand: discord_slash.SlashCommand, botId, botToken: str):
"""Adds all commands that are registered in SlashCommand to Discord."""
commands = SlashCommand.commands
print("Adding commands to the API...")
if len(commands) <= 0:
print("No commands are registered, cancelled.")
return
for cmd, info in commands.items():
if info["guild_ids"] is not None:
for guild in info["guild_ids"]:
print(f"Adding command '{cmd}' to guild {guild}")
await discord_slash.utils.manage_commands.add_slash_command(botId, botToken, guild, cmd,
info["description"], info["api_options"])
else:
print(f"Adding global command '{cmd}'")
await discord_slash.utils.manage_commands.add_slash_command(botId, botToken, None, cmd,
info["description"], info["api_options"])
print("Done adding commands.")
async def _getCommands(botId, botToken: str, guildsWithCommands=None) -> Tuple[list, dict]:
"""Retrives all global and guild specific commands that are registered on Discord."""
if guildsWithCommands is None:
guildsWithCommands = []
url = f"https://discord.com/api/v8/applications/{botId}/commands"
async with aiohttp.ClientSession() as session:
async with session.get(url, headers={"Authorization": f"Bot {botToken}"}) as resp:
globalCmds = await resp.json()
guildCmds = {}
for guildId in guildsWithCommands:
url = f"https://discord.com/api/v8/applications/{botId}/guilds/{guildId}/commands"
async with session.get(url, headers={"Authorization": f"Bot {botToken}"}) as resp:
guildCmds[guildId] = await resp.json()
return globalCmds, guildCmds
async def removeFromAPI(botId, botToken: str, guildsWithCommands: list):
"""Removes all commands that are registered to the bot on Discord."""
globalCmds, guildCmds = await _getCommands(botId, botToken, guildsWithCommands)
print("Removing commands to the API...")
if len(globalCmds) <= 0 and len(guildCmds) <= 0:
print("No commands are registered, cancelled.")
return
for guild, cmds in guildCmds.items():
for cmd in cmds:
print(f"Removing command '{cmd['name']}' from guild {guild}")
await discord_slash.utils.manage_commands.remove_slash_command(botId, botToken, guild, cmd["id"])
for cmd in globalCmds:
print(f"Removing global command '{cmd['name']}'")
await discord_slash.utils.manage_commands.remove_slash_command(botId, botToken, None, cmd["id"])
print("Done removing commands.") |
Hi, I recently started using this extension to try out Discord's new slash commands feature. I noticed my commands weren't showing up in the Discord client when entering
/
into the chat box, so I went through the issues and found #3 and #4.Leastrio said in #4:
I noticed the lib has
discord_slash.utils.manage_commands
which contains methods for adding slash commands with the Discord API, I assume this is what Leastrio was referring to. However, I don't understand how to use this in junction with myslash.slash()
decorated functions or really just the lib in general. This leads to me to have questions like, "How do I link a command to a function? What's the purpose ofslash.slash()
's parameters if they're seemingly overwritten bydiscord_slash.utils.manage_commands.add_slash_command
?"All help is appreciated, thank you!
The text was updated successfully, but these errors were encountered: