mirror of
https://github.com/ahmadk953/poixpixel-discord-bot.git
synced 2025-06-12 01:49:31 +00:00
Added Warn and Ban Commands, Added Logging, and Much More
This commit is contained in:
parent
d89de72e08
commit
86adac3f08
33 changed files with 2200 additions and 204 deletions
|
@ -1,9 +1,16 @@
|
|||
import { REST, Routes } from 'discord.js';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { loadConfig } from './configLoader.js';
|
||||
|
||||
const config = loadConfig();
|
||||
const { token, clientId, guildId } = config;
|
||||
|
||||
const __dirname = path.resolve();
|
||||
const commandsPath = path.join(__dirname, 'target', 'commands');
|
||||
|
||||
const rest = new REST({ version: '10' }).setToken(token);
|
||||
|
||||
const getFilesRecursively = (directory: string): string[] => {
|
||||
const files: string[] = [];
|
||||
const filesInDirectory = fs.readdirSync(directory);
|
||||
|
@ -13,8 +20,7 @@ const getFilesRecursively = (directory: string): string[] => {
|
|||
|
||||
if (fs.statSync(filePath).isDirectory()) {
|
||||
files.push(...getFilesRecursively(filePath));
|
||||
}
|
||||
else if (file.endsWith('.js')) {
|
||||
} else if (file.endsWith('.js')) {
|
||||
files.push(filePath);
|
||||
}
|
||||
}
|
||||
|
@ -27,9 +33,13 @@ const commandFiles = getFilesRecursively(commandsPath);
|
|||
export const deployCommands = async () => {
|
||||
try {
|
||||
console.log(
|
||||
`Started refreshing ${commandFiles.length} application (/) commands.`,
|
||||
`Started refreshing ${commandFiles.length} application (/) commands...`,
|
||||
);
|
||||
|
||||
const existingCommands = (await rest.get(
|
||||
Routes.applicationGuildCommands(clientId, guildId),
|
||||
)) as any[];
|
||||
|
||||
const commands = commandFiles.map(async (file) => {
|
||||
const commandModule = await import(`file://${file}`);
|
||||
const command = commandModule.default;
|
||||
|
@ -40,8 +50,7 @@ export const deployCommands = async () => {
|
|||
'execute' in command
|
||||
) {
|
||||
return command;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
console.warn(
|
||||
`[WARNING] The command at ${file} is missing a required "data" or "execute" property.`,
|
||||
);
|
||||
|
@ -53,9 +62,31 @@ export const deployCommands = async () => {
|
|||
commands.filter((command) => command !== null),
|
||||
);
|
||||
|
||||
const apiCommands = validCommands.map((command) => command.data.toJSON());
|
||||
|
||||
const commandsToRemove = existingCommands.filter(
|
||||
(existingCmd) =>
|
||||
!apiCommands.some((newCmd) => newCmd.name === existingCmd.name),
|
||||
);
|
||||
|
||||
for (const cmdToRemove of commandsToRemove) {
|
||||
await rest.delete(
|
||||
Routes.applicationGuildCommand(clientId, guildId, cmdToRemove.id),
|
||||
);
|
||||
console.log(`Removed command: ${cmdToRemove.name}`);
|
||||
}
|
||||
|
||||
const data: any = await rest.put(
|
||||
Routes.applicationGuildCommands(clientId, guildId),
|
||||
{ body: apiCommands },
|
||||
);
|
||||
|
||||
console.log(
|
||||
`Successfully registered ${data.length} application (/) commands with the Discord API.`,
|
||||
);
|
||||
|
||||
return validCommands;
|
||||
}
|
||||
catch (error) {
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue