From 817644e66cf577c7dd8eaee29aa8733e5d0850de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Sat, 16 Nov 2024 17:05:28 +0100 Subject: [PATCH 1/2] registering commands with client.application.commands... --- bot/package.json | 5 ++-- bot/src/events/client/ready.ts | 28 ++++++++++++++----- bot/src/structure/WelcomerClient.ts | 42 +---------------------------- bot/src/types/index.ts | 4 +-- bot/src/utils/functions.ts | 25 ++++++++++++++++- 5 files changed, 51 insertions(+), 53 deletions(-) diff --git a/bot/package.json b/bot/package.json index b95514e..690cc6f 100644 --- a/bot/package.json +++ b/bot/package.json @@ -5,8 +5,9 @@ "license": "MIT", "scripts": { "dev": "export NODE_ENV=development && nodemon", - "build": "rm -rf dist && tsc", - "start": "node dist/index.js" + "build": "tsc", + "start": "node dist/index.js", + "prod": "export NODE_ENV=production && yarn build && yarn start" }, "devDependencies": { "@types/node": "^20.14.9", diff --git a/bot/src/events/client/ready.ts b/bot/src/events/client/ready.ts index d199f23..86db6be 100644 --- a/bot/src/events/client/ready.ts +++ b/bot/src/events/client/ready.ts @@ -1,6 +1,7 @@ -import { ActivityType } from "discord.js"; +import { ActivityType, ApplicationCommandType } from "discord.js"; import WelcomerClient from "../../structure/WelcomerClient"; import { EventType } from "../../types"; +import { waitForManager } from "../../utils/functions"; export default class ReadyEvent implements EventType { @@ -8,8 +9,10 @@ export default class ReadyEvent implements EventType { once = true; async execute(client: WelcomerClient) { console.log(`${client.user?.username} is ready (Cluster: ${client.cluster.id})!`) - // trigger the clusterReady event - client.cluster.emit("clusterReady", client.cluster); + await setStatus(); + setInterval(async () => { + await setStatus(); + }, 120000); async function setStatus() { let messages = [ @@ -22,10 +25,21 @@ export default class ReadyEvent implements EventType { client.user?.setActivity(message, { type: ActivityType.Watching }); } - await setStatus(); + if (client.cluster.id === 0) { + await waitForManager(client); + for (const command of client.commands.values()) { + if (command.type !== ApplicationCommandType.ChatInput) continue; + command.contexts = [0]; + command.dmPermission = false; + + + } + await client.application?.commands.set([...client.commands.values()]).then(async (commandsData) => { + console.log("Commands registered!"); + console.log(commandsData); + }) + } + - setInterval(async () => { - await setStatus(); - }, 120000); }; } \ No newline at end of file diff --git a/bot/src/structure/WelcomerClient.ts b/bot/src/structure/WelcomerClient.ts index e256b56..e4f82fa 100644 --- a/bot/src/structure/WelcomerClient.ts +++ b/bot/src/structure/WelcomerClient.ts @@ -8,9 +8,7 @@ import { GatewayIntentBits, Options, Partials, - REST, RESTPostAPIChatInputApplicationCommandsJSONBody, - Routes, } from "discord.js"; import { ButtonType, @@ -103,11 +101,10 @@ export default class WelcomerClient extends Client { }); } - public async loadCommands(reloadRest: boolean = true): Promise { + public async loadCommands(): Promise { this.commands.clear(); this.commandsData.clear(); - let rest = new REST({ version: "10" }).setToken(process.env.TOKEN!); let commands_array: RESTPostAPIChatInputApplicationCommandsJSONBody[] = []; let command_admin: RESTPostAPIChatInputApplicationCommandsJSONBody[] = []; let files = await loadFiles(`dist/commands`); @@ -123,43 +120,6 @@ export default class WelcomerClient extends Client { } this.commands.set(command.data.name.toLowerCase(), command); } - this.application?.commands.set(commands_array); - - if (reloadRest) { - console.log( - `Started loading ${ - commands_array.length + command_admin.length - } commands` - ); - try { - let data = (await rest.put( - Routes.applicationCommands(process.env.CLIENT_ID!), - { body: commands_array } - )) as APIApplicationCommand[]; - - let data_admin = (await rest.put( - Routes.applicationGuildCommands( - process.env.CLIENT_ID!, - process.env.ADMIN_GUILD_ID! - ), - { body: command_admin } - )) as APIApplicationCommand[]; - - if (!data || !data_admin) - return console.error("An error occured on loadCommands!"); - data.forEach((command) => { - this.commandsData.set(command.name, command); - }); - data_admin.forEach((command) => { - this.commandsData.set(command.name, command); - }); - console.log( - `Loaded ${commands_array.length + command_admin.length} commands` - ); - } catch (error) { - console.log(error); - } - } } catch (e) { console.error("An error occured on loadCommands!", e); } diff --git a/bot/src/types/index.ts b/bot/src/types/index.ts index af4c361..394d35a 100644 --- a/bot/src/types/index.ts +++ b/bot/src/types/index.ts @@ -1,4 +1,4 @@ -import { AnySelectMenuInteraction, APIAttachment, APIEmbed, ButtonInteraction, ChatInputCommandInteraction, ColorResolvable, Guild, InteractionResponse, Message, ModalMessageModalSubmitInteraction, SlashCommandBuilder, SlashCommandOptionsOnlyBuilder } from "discord.js"; +import { AnySelectMenuInteraction, APIAttachment, APIEmbed, ButtonInteraction, ChatInputApplicationCommandData, ChatInputCommandInteraction, ColorResolvable, Guild, InteractionResponse, Message, ModalMessageModalSubmitInteraction, SlashCommandBuilder, SlashCommandOptionsOnlyBuilder } from "discord.js"; import WelcomerClient from "../structure/WelcomerClient"; import { WelcomerEmbed } from "../database/schema/APISchemas/Embed"; @@ -7,7 +7,7 @@ export interface modalType { execute(interaction: ModalMessageModalSubmitInteraction, client: WelcomerClient, ...options: any): Promise } -export interface CommandType { +export interface CommandType extends ChatInputApplicationCommandData { name: string; description: string; admin?: boolean; diff --git a/bot/src/utils/functions.ts b/bot/src/utils/functions.ts index 726206c..89f4023 100644 --- a/bot/src/utils/functions.ts +++ b/bot/src/utils/functions.ts @@ -1,4 +1,5 @@ import { TextChannel, PermissionResolvable } from "discord.js"; +import WelcomerClient from "../structure/WelcomerClient"; export const formatNumber = (num: number) => { return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); @@ -9,4 +10,26 @@ export const checkPermsForChannel = async (channel: TextChannel, perms: Permissi return false; } return true; -} \ No newline at end of file +}; + +export async function waitForManager(client: WelcomerClient, cb?: () => unknown, intervalTime: number = 1000): Promise { + if (client.managerReady) { + if (cb) cb(); + return; + } + + await new Promise((resolve, reject) => { + const interval = setInterval(() => { + try { + if (client.managerReady) { + clearInterval(interval); + if (cb) cb(); + resolve(); + } + } catch (error) { + clearInterval(interval); + reject(error); + } + }, intervalTime); + }) +}; \ No newline at end of file From 22ccc87293dfa65cb6d36c8801e6c76eb4240cee Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 16 Nov 2024 16:09:53 +0000 Subject: [PATCH 2/2] Bump cross-spawn in /bot in the npm_and_yarn group across 1 directory Bumps the npm_and_yarn group with 1 update in the /bot directory: [cross-spawn](https://github.com/moxystudio/node-cross-spawn). Updates `cross-spawn` from 7.0.3 to 7.0.5 - [Changelog](https://github.com/moxystudio/node-cross-spawn/blob/master/CHANGELOG.md) - [Commits](https://github.com/moxystudio/node-cross-spawn/compare/v7.0.3...v7.0.5) --- updated-dependencies: - dependency-name: cross-spawn dependency-type: indirect dependency-group: npm_and_yarn ... Signed-off-by: dependabot[bot] --- bot/yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot/yarn.lock b/bot/yarn.lock index 17f6d5d..0aa620c 100644 --- a/bot/yarn.lock +++ b/bot/yarn.lock @@ -562,9 +562,9 @@ create-require@^1.1.0: integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ== cross-spawn@^7.0.0: - version "7.0.3" - resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6" - integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w== + version "7.0.5" + resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.5.tgz#910aac880ff5243da96b728bc6521a5f6c2f2f82" + integrity sha512-ZVJrKKYunU38/76t0RMOulHOnUcbU9GbpWKAOZ0mhjr7CX6FVrH+4FrAapSOekrgFQ3f/8gwMEuIft0aKq6Hug== dependencies: path-key "^3.1.0" shebang-command "^2.0.0"