Move context menus to their own folder

This commit is contained in:
Tarrgon
2025-05-31 11:10:30 -04:00
parent 2d7c14ed7a
commit 5a8ca6bb54
3 changed files with 43 additions and 20 deletions
@@ -2,9 +2,9 @@ import { ApplicationIntegrationType, Client, InteractionContextType, PermissionF
import { channelIsInStaffCategory, handleWhoIsInteraction } from '../utils'; import { channelIsInStaffCategory, handleWhoIsInteraction } from '../utils';
export default { export default {
name: 'whois-context', name: 'Whois',
data: new ContextMenuCommandBuilder() data: new ContextMenuCommandBuilder()
.setName('whois-context') .setName('Whois')
.setIntegrationTypes(ApplicationIntegrationType.GuildInstall) .setIntegrationTypes(ApplicationIntegrationType.GuildInstall)
.setContexts(InteractionContextType.Guild) .setContexts(InteractionContextType.Guild)
.setDefaultMemberPermissions(PermissionFlagsBits.BanMembers) .setDefaultMemberPermissions(PermissionFlagsBits.BanMembers)
+12 -2
View File
@@ -31,11 +31,13 @@ const client = new DiscordClient({
}); });
const commands: Handler[] = []; const commands: Handler[] = [];
const contextMenus: Handler[] = [];
const buttons: Handler[] = []; const buttons: Handler[] = [];
const modals: Handler[] = []; const modals: Handler[] = [];
const menus: Handler[] = []; const menus: Handler[] = [];
loadHandlersFrom('commands', commands); loadHandlersFrom('commands', commands);
loadHandlersFrom('context-menus', contextMenus);
loadHandlersFrom('buttons', buttons); loadHandlersFrom('buttons', buttons);
loadHandlersFrom('modals', modals); loadHandlersFrom('modals', modals);
loadHandlersFrom('menus', menus); loadHandlersFrom('menus', menus);
@@ -59,14 +61,22 @@ client.on('interactionCreate', async (interaction) => {
return; return;
} }
if (interaction.isChatInputCommand() || interaction.isContextMenuCommand()) { if (interaction.isChatInputCommand()) {
// Handle chat and context menu commands. // Handle chat
for (const command of commands) { for (const command of commands) {
if (interaction.commandName == command.name) { if (interaction.commandName == command.name) {
command.handler(client, interaction); command.handler(client, interaction);
return; return;
} }
} }
} else if (interaction.isContextMenuCommand()) {
// Handle context menu commands.
for (const command of contextMenus) {
if (interaction.commandName == command.name) {
command.handler(client, interaction);
return;
}
}
} else if (interaction.isAutocomplete()) { } else if (interaction.isAutocomplete()) {
// Handle autocomplete requests. // Handle autocomplete requests.
for (const command of commands) { for (const command of commands) {
+16 -3
View File
@@ -13,11 +13,22 @@ export async function refreshCommands(client: Client) {
try { try {
const commands: RESTPostAPIApplicationCommandsJSONBody[] = []; const commands: RESTPostAPIApplicationCommandsJSONBody[] = [];
const guildCommands: { [id: string]: RESTPostAPIApplicationCommandsJSONBody[] } = {}; const guildCommands: { [id: string]: RESTPostAPIApplicationCommandsJSONBody[] } = {};
const commandFiles = fs.readdirSync(`${ROOT_DIR}/commands`).filter(file => file.endsWith('.js') || file.endsWith('.ts')); const commandFiles = {
commands: fs.readdirSync(`${ROOT_DIR}/commands`).filter(file => file.endsWith('.js') || file.endsWith('.ts')),
'context-menus': fs.readdirSync(`${ROOT_DIR}/context-menus`).filter(file => file.endsWith('.js') || file.endsWith('.ts')),
};
for (const file of commandFiles) { for (const [folderName, files] of Object.entries(commandFiles)) {
for (const file of files) {
const p = `${ROOT_DIR}/${folderName}/${file}`;
// eslint-disable-next-line @typescript-eslint/no-require-imports // eslint-disable-next-line @typescript-eslint/no-require-imports
const command: Command = require(`${ROOT_DIR}/commands/${file}`).default; const command: Command = require(p).default;
if (!command) {
console.warn(`File at ${p} has no export. Skipping registering.`);
continue;
}
let data: RESTPostAPIApplicationCommandsJSONBody; let data: RESTPostAPIApplicationCommandsJSONBody;
if (typeof (command.data) == 'function') { if (typeof (command.data) == 'function') {
data = (await command.data(client)).toJSON(); data = (await command.data(client)).toJSON();
@@ -34,6 +45,8 @@ export async function refreshCommands(client: Client) {
} }
} }
} }
}
console.log('Started refreshing application (/) commands.'); console.log('Started refreshing application (/) commands.');
console.log('Global commands: ' + commands.length); console.log('Global commands: ' + commands.length);