/[sudobot]/trunk/scripts/deploy-commands.js
ViewVC logotype

Contents of /trunk/scripts/deploy-commands.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 575 - (show annotations)
Mon Jul 29 17:59:26 2024 UTC (8 months ago) by rakinar2
File MIME type: text/javascript
File size: 3848 byte(s)
chore: add trunk
1 #!/usr/bin/env node
2
3 /**
4 * This file is part of SudoBot.
5 *
6 * Copyright (C) 2021-2022 OSN Inc.
7 *
8 * SudoBot is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * SudoBot is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 require("module-alias/register");
23 require("reflect-metadata");
24
25 const { REST } = require("@discordjs/rest");
26 const { Routes } = require("discord-api-types/v9");
27 const { ApplicationCommandType, ContextMenuCommandBuilder, SlashCommandBuilder } = require("discord.js");
28 const { config } = require("dotenv");
29 const { existsSync } = require("fs");
30 const path = require("path");
31
32 function makeSlashCommandBuilder(command) {
33 const builder = command.slashCommandBuilder ?? new SlashCommandBuilder();
34
35 if (!builder.name) builder.setName(command.name);
36 if (!builder.description && command.description) builder.setDescription(command.description);
37
38 return builder.setDMPermission(false);
39 }
40
41 function makeContextMenuCommandBuilder(command) {
42 return new ContextMenuCommandBuilder().setName(command.name).setType(command.applicationCommandType).setDMPermission(false);
43 }
44
45 (async () => {
46 if (existsSync(path.join(__dirname, ".env")) || existsSync(path.join(__dirname, "../.env"))) {
47 config({
48 path: existsSync(path.join(__dirname, ".env")) ? undefined : path.join(__dirname, "../.env")
49 });
50 } else {
51 process.env.ENV = "prod";
52 }
53
54 const { CLIENT_ID, HOME_GUILD_ID, TOKEN } = process.env;
55
56 const commands = [];
57
58 if (!process.argv.includes("--clear")) {
59 const clientPath = path.resolve(existsSync(path.join(__dirname, "../build")) ? "build" : "src", "core/Client.js");
60
61 console.info("Importing client from: ", clientPath);
62
63 const { default: Client } = require(clientPath);
64
65 const client = new Client({
66 intents: []
67 });
68
69 await client.boot({
70 events: false
71 });
72
73 for (const [name, command] of client.commands) {
74 if (name.includes("__") || client.commands.get(name)?.name !== name) continue;
75 if (!command.supportsInteractions) continue;
76
77 commands.push(
78 ...(command.otherApplicationCommandBuilders ?? []),
79 command.applicationCommandType === ApplicationCommandType.ChatInput
80 ? makeSlashCommandBuilder(command)
81 : makeContextMenuCommandBuilder(command)
82 );
83 }
84 }
85
86 console.table(
87 commands.map(c =>
88 c instanceof SlashCommandBuilder
89 ? {
90 type: "ChatInputCommand",
91 name: c.name,
92 description: c.description
93 }
94 : { type: "ContextMenuCommand", name: c.name, description: "None" }
95 )
96 );
97
98 const rest = new REST({ version: "10" }).setToken(TOKEN);
99
100 rest.put(
101 Routes[process.argv.includes("--guild") ? "applicationGuildCommands" : "applicationCommands"](CLIENT_ID, HOME_GUILD_ID),
102 {
103 body: commands
104 }
105 )
106 .then(() =>
107 console.log("Successfully registered application " + (process.argv.includes("--guild") ? "guild " : "") + "commands.")
108 )
109 .catch(console.error)
110 .finally(() => process.exit(0));
111 })();

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26