Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
clementvt committed Nov 16, 2024
2 parents 741cccc + bd5f8ed commit bd48ae1
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 56 deletions.
5 changes: 3 additions & 2 deletions bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"exports": "./dist/index.mjs",
"scripts": {
"dev": "export NODE_ENV=development && nodemon",
"build": "pkgroll --clean-dist",
"start": "node dist/index.mjs"
"build": "tsc",
"start": "node dist/index.js",
"prod": "export NODE_ENV=production && yarn build && yarn start"
},
"devDependencies": {
"@types/node": "^20.14.9",
Expand Down
28 changes: 21 additions & 7 deletions bot/src/events/client/ready.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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 {
name = "ready";
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 = [
Expand All @@ -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);
};
}
42 changes: 1 addition & 41 deletions bot/src/structure/WelcomerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import {
GatewayIntentBits,
Options,
Partials,
REST,
RESTPostAPIChatInputApplicationCommandsJSONBody,
Routes,
} from "discord.js";
import {
ButtonType,
Expand Down Expand Up @@ -103,11 +101,10 @@ export default class WelcomerClient extends Client {
});
}

public async loadCommands(reloadRest: boolean = true): Promise<void> {
public async loadCommands(): Promise<void> {
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(`src/commands`);
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions bot/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -7,7 +7,7 @@ export interface modalType {
execute(interaction: ModalMessageModalSubmitInteraction, client: WelcomerClient, ...options: any): Promise<void>
}

export interface CommandType {
export interface CommandType extends ChatInputApplicationCommandData {
name: string;
description: string;
admin?: boolean;
Expand Down
25 changes: 24 additions & 1 deletion bot/src/utils/functions.ts
Original file line number Diff line number Diff line change
@@ -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,");
Expand All @@ -9,4 +10,26 @@ export const checkPermsForChannel = async (channel: TextChannel, perms: Permissi
return false;
}
return true;
}
};

export async function waitForManager(client: WelcomerClient, cb?: () => unknown, intervalTime: number = 1000): Promise<void> {
if (client.managerReady) {
if (cb) cb();
return;
}

await new Promise<void>((resolve, reject) => {
const interval = setInterval(() => {
try {
if (client.managerReady) {
clearInterval(interval);
if (cb) cb();
resolve();
}
} catch (error) {
clearInterval(interval);
reject(error);
}
}, intervalTime);
})
};
6 changes: 3 additions & 3 deletions bot/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -853,9 +853,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"
Expand Down

0 comments on commit bd48ae1

Please sign in to comment.