Readme

register_discord_commands

Registers slash commands for your Discord bot, globally or in one guild. Note that this overwrites old commands.

  • app_id: Your bot's application ID as a string
  • guild_id: Provide a guild ID as a string to apply the commands only in one guild, or use null to apply globally. Note that applying commands globally takes a while on Discord's end.
  • token: Your bot's token.
  • commands: An array of command objects to register. For a basic command with no arguments, only name and description are needed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { fetch } from "https://esm.town/v/std/fetch";
export let register_discord_commands = async (
app_id: String,
guild_id: ?String,
token: String,
commands: Array<Object>
) => {
let url = `https://discord.com/api/v10/applications/${app_id}/`;
if (guild_id) {
url += `guilds/${guild_id}/commands`;
} else {
url += `commands`;
}
const response = await fetch(url, {
headers: {
"Content-Type": "application/json",
Authorization: `Bot ${token}`,
},
method: "PUT",
body: JSON.stringify(commands),
});
return response.ok || response.text();
};
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.