Context menus work!

pull/17/head
PineaFan 3 years ago
parent a00db1b8ba
commit 65801cf0c6
No known key found for this signature in database
GPG Key ID: 0AEF25BAA0FB1C74

@ -1,5 +1,4 @@
import { typeSlashCommandSubcommandBuilder } from '@discordjs/builders'; import Discord, { Interaction, SlashCommandBuilder, ApplicationCommandType } from 'discord.js';
import { Interaction, SlashCommandBuilder, ApplicationCommandType } from 'discord.js';
// @ts-expect-error // @ts-expect-error
import config from "../../config/main.json" assert { type: "json" }; import config from "../../config/main.json" assert { type: "json" };
import client from "../client.js"; import client from "../client.js";
@ -16,7 +15,6 @@ const colours = {
} }
async function registerCommands() { async function registerCommands() {
const developmentMode = config.enableDevelopment;
const commands = []; const commands = [];
const files = fs.readdirSync(config.commandsFolder, { withFileTypes: true }).filter( const files = fs.readdirSync(config.commandsFolder, { withFileTypes: true }).filter(
@ -49,17 +47,8 @@ async function registerCommands() {
} }
} }
console.log(`Processed ${commands.length} commands, registering...`) console.log(`Processed ${processed.length} commands`)
return processed;
const updateCommands = process.argv.includes("--update-commands");
if (developmentMode) {
const guild = await client.guilds.fetch(config.developmentGuildID);
if (updateCommands) guild.commands.set(processed);
console.log(`${colours.purple}Commands registered in ${guild.name}${colours.none}`)
} else {
if (updateCommands) client.application!.commands.set(processed);
console.log(`${colours.blue}Commands registered globally${colours.none}`)
}
}; };
@ -100,31 +89,35 @@ async function registerContextMenus() {
console.log(`Registering ${messageFiles.length} message context menus and ${userFiles.length} user context menus`) console.log(`Registering ${messageFiles.length} message context menus and ${userFiles.length} user context menus`)
let i = 0; let i = 0;
let errors = 0; let errors = 0;
const commands: (Discord.ContextMenuCommandBuilder)[] = []
const totalFiles = messageFiles.length + userFiles.length;
for (const file of messageFiles) { for (const file of messageFiles) {
const last = i === messageFiles.length - 1 ? "└" : "├"; const last = i === totalFiles - 1 ? "└" : "├";
i++; i++;
try { try {
console.log(`${last}${colours.yellow}Loading message context menu ${file.name}${colours.none}`) console.log(`${last}${colours.yellow}Loading message context menu ${file.name}${colours.none}`)
const context = (await import(`../../../${config.messageContextFolder}/${file.name}`)); const context = (await import(`../../../${config.messageContextFolder}/${file.name}`));
context.command.setType(ApplicationCommandType.Message); context.command.setType(ApplicationCommandType.Message);
commands.push(context.command);
client.commands["contextCommands/message/" + file.name.replace(".js", "")] = context; client.commands["contextCommands/message/" + context.command.name] = context;
console.log(`${last.replace("└", " ").replace("├", "│")} └─ ${colours.green}Loaded ${file.name} [${i} / ${messageFiles.length}]${colours.none}`) console.log(`${last.replace("└", " ").replace("├", "│")} └─ ${colours.green}Loaded ${file.name} [${i} / ${totalFiles}]${colours.none}`)
} catch (e) { } catch (e) {
errors++; errors++;
console.log(`${last.replace("└", " ").replace("├", "│")} └─ ${colours.red}Failed to load ${file.name} [${i} / ${messageFiles.length}]${colours.none}`) console.log(`${last.replace("└", " ").replace("├", "│")} └─ ${colours.red}Failed to load ${file.name} [${i} / ${totalFiles}] | ${e}${colours.none}`)
} }
} }
for (const file of userFiles) { for (const file of userFiles) {
const last = i === userFiles.length - 1 ? "└" : "├"; const last = i === totalFiles - 1 ? "└" : "├";
i++; i++;
try { try {
console.log(`${last}${colours.yellow}Loading user context menu ${file.name}${colours.none}`) console.log(`${last}${colours.yellow}Loading user context menu ${file.name}${colours.none}`)
const context = (await import(`../../../${config.userContextFolder}/${file.name}`)); const context = (await import(`../../../${config.userContextFolder}/${file.name}`));
context.command.setType(ApplicationCommandType.User); context.command.setType(ApplicationCommandType.User);
commands.push(context.command);
client.commands["contextCommands/user/" + file.name.replace(".js", "")] = context; client.commands["contextCommands/user/" + context.command.name] = context;
console.log(`${last.replace("└", " ").replace("├", "│")} └─ ${colours.green}Loaded ${file.name} [${i} / ${userFiles.length}]${colours.none}`) console.log(`${last.replace("└", " ").replace("├", "│")} └─ ${colours.green}Loaded ${file.name} [${i} / ${userFiles.length}]${colours.none}`)
} catch (e) { } catch (e) {
@ -134,17 +127,18 @@ async function registerContextMenus() {
} }
console.log(`Loaded ${messageFiles.length + userFiles.length - errors} context menus (${errors} failed)`) console.log(`Loaded ${messageFiles.length + userFiles.length - errors} context menus (${errors} failed)`)
return commands;
}; };
async function registerCommandHandler() { async function registerCommandHandler() {
client.on("interactionCreate", async (interaction: Interaction) => { client.on("interactionCreate", async (interaction: Interaction) => {
if (interaction.isUserContextMenuCommand()) {; if (interaction.isUserContextMenuCommand()) {;
const commandName = interaction.commandName; const commandName = "contextCommands/user/" + interaction.commandName;
console.log(commandName); execute(client.commands[commandName]?.check, client.commands[commandName]?.callback, interaction)
return; return;
} else if (interaction.isMessageContextMenuCommand()) { } else if (interaction.isMessageContextMenuCommand()) {
const commandName = interaction.commandName; const commandName = "contextCommands/message/" + interaction.commandName;
console.log(commandName); execute(client.commands[commandName]?.check, client.commands[commandName]?.callback, interaction)
return; return;
} }
if (!interaction.isChatInputCommand()) return; if (!interaction.isChatInputCommand()) return;
@ -158,25 +152,39 @@ async function registerCommandHandler() {
const command = client.commands[fullCommandName]; const command = client.commands[fullCommandName];
const callback = command?.callback; const callback = command?.callback;
const check = command?.check; const check = command?.check;
execute(check, callback, interaction);
});
}
if (!callback) return; async function execute(check: Function | undefined, callback: Function | undefined, data: Interaction) {
if (check) { if (!callback) return;
let result; if (check) {
try { let result;
result = await check(interaction); try {
} catch (e) { result = await check(data);
console.log(e); } catch (e) {
result = false; console.log(e);
} result = false;
if (!result) return;
} }
callback(interaction); if (!result) return;
}); }
callback(data);
} }
export default async function register() { export default async function register() {
let commandList: (SlashCommandBuilder | SlashCommandSubcommandBuilder)[] = [] let commandList: ( Discord.SlashCommandBuilder | Discord.ContextMenuCommandBuilder )[] = [];
commandList.concat(await registerCommands()); commandList = commandList.concat(await registerCommands());
commandList = commandList.concat(await registerContextMenus());
if (process.argv.includes("--update-commands")) {
if (config.enableDevelopment) {
const guild = await client.guilds.fetch(config.developmentGuildID);
console.log(`${colours.purple}Registering commands in ${guild!.name}${colours.none}`)
await guild.commands.set(commandList);
} else {
console.log(`${colours.blue}Registering commands in production mode${colours.none}`)
await client.application?.commands.set(commandList);
}
}
await registerCommandHandler(); await registerCommandHandler();
await registerEvents(); await registerEvents();
console.log(`${colours.green}Registered commands and events${colours.none}`) console.log(`${colours.green}Registered commands and events${colours.none}`)

Loading…
Cancel
Save