diff --git a/src/commands/military/getDischargeInfo.js b/src/commands/military/getDischargeInfo.js index 4036307..ab925c9 100644 --- a/src/commands/military/getDischargeInfo.js +++ b/src/commands/military/getDischargeInfo.js @@ -13,9 +13,9 @@ const { createProgressBar, getNameById } = require('../../utils/discordUtils'); const { binarySearch, addSpace } = require('../../utils/helpers'); // Core logic for getting discharge info -function getDischargeInfo(client, targetUserId, targetUserName, decimal, usedFullInfo) { +async function getDischargeInfo(client, targetUserId, targetUserName, decimal, usedFullInfo) { try { - const ipdaeDatas = getIpdaeData(); + const ipdaeDatas = await getIpdaeData(); const userNameForEmbed = (targetUserName && targetUserName.trim() !== `Unknown User (${targetUserId})` && targetUserName.trim() !== '') ? `${targetUserName}님` : `사용자 (${targetUserId})님`; const index = binarySearch(ipdaeDatas, targetUserId); if (index === -1) { @@ -115,7 +115,7 @@ async function execute(interaction) { const targetUserName = getNameById(interaction.client, targetUserId, interaction.guild); logger.info(`Processing /전역일 for ${targetUserName} (${targetUserId}), requested by ${interaction.user.username} (${interaction.user.id})`); await interaction.deferReply(); - const resultEmbed = getDischargeInfo(interaction.client, targetUserId, targetUserName, decimalArg, fullInfoArg); + const resultEmbed = await getDischargeInfo(interaction.client, targetUserId, targetUserName, decimalArg, fullInfoArg); await interaction.editReply({ embeds: [resultEmbed] }); } @@ -145,7 +145,7 @@ const legacy = { } const targetUserName = getNameById(client, targetUserId, guild); - const resultEmbed = getDischargeInfo(client, targetUserId, targetUserName, decimal, false); + const resultEmbed = await getDischargeInfo(client, targetUserId, targetUserName, decimal, false); await channel.send({ embeds: [resultEmbed] }); } }; diff --git a/src/commands/military/setEnlistmentDate.js b/src/commands/military/setEnlistmentDate.js index b307892..1d2a3fe 100644 --- a/src/commands/military/setEnlistmentDate.js +++ b/src/commands/military/setEnlistmentDate.js @@ -64,7 +64,7 @@ const commandData = new SlashCommandBuilder() .setDescription('입대일을 설정합니다') .setContexts(InteractionContextType.Guild, InteractionContextType.BotDM, InteractionContextType.PrivateChannel) .setIntegrationTypes(ApplicationIntegrationType.GuildInstall, ApplicationIntegrationType.UserInstall) - .addStringOption(option => option.setName('날짜').setDescription('형식: YYYY-MM-DD').setRequired(true)) + .addStringOption(option => option.setName('입대일').setDescription('형식: YYYY-MM-DD').setRequired(true)) .addStringOption(option => option.setName('복무형태').setDescription('복무 형태').setRequired(true) .addChoices( { name: '육군', value: '육군' }, @@ -75,7 +75,7 @@ const commandData = new SlashCommandBuilder() )); async function execute(interaction) { - const slashStartDateArg = interaction.options.getString('날짜'); + const slashStartDateArg = interaction.options.getString('입대일'); const slashStartTypeArg = interaction.options.getString('복무형태'); logger.info(`Processing /입대일 for ${interaction.user.username} (${interaction.user.id}) with date: ${slashStartDateArg}, type: ${slashStartTypeArg}`); await interaction.deferReply(); @@ -103,4 +103,4 @@ module.exports = { data: commandData, execute, legacy, -}; \ No newline at end of file +}; diff --git a/src/data/dataManager.js b/src/data/dataManager.js index d7236dd..50ed735 100644 --- a/src/data/dataManager.js +++ b/src/data/dataManager.js @@ -88,8 +88,8 @@ async function loadCelebrationConfig() { } async function migrateData() { - let ipdaeData = getIpdaeData(); - let notificationHistory = getNotificationHistory(); + let ipdaeData = await getIpdaeData(); + let notificationHistory = await getNotificationHistory(); let ipdaeDataNeedsSave = false; let notificationHistoryNeedsSave = false; @@ -124,19 +124,19 @@ async function saveData(type) { try { switch (type) { case 'ipdae': - const ipdaeData = getIpdaeData(); + const ipdaeData = await getIpdaeData(); await fs.writeFile(ipdaeDataDirectory, JSON.stringify(ipdaeData, null, 4), 'utf8'); cacheManager.set('ipdaeData', ipdaeData, CACHE_TTL); logger.info('ipdaeData.json saved and cache updated.'); break; case 'config': - const celebrationConfig = getCelebrationConfig(); + const celebrationConfig = await getCelebrationConfig(); await fs.writeFile(celebrationConfigPath, JSON.stringify(celebrationConfig, null, 4), 'utf8'); cacheManager.set('celebrationConfig', celebrationConfig, CACHE_TTL); logger.info('celebrationConfig.json saved and cache updated.'); break; case 'history': - const notificationHistory = getNotificationHistory(); + const notificationHistory = await getNotificationHistory(); await fs.writeFile(notificationHistoryPath, JSON.stringify(notificationHistory, null, 4), 'utf8'); cacheManager.set('notificationHistory', notificationHistory, CACHE_TTL); logger.info('notificationHistory.json saved and cache updated.'); @@ -149,39 +149,39 @@ async function saveData(type) { } } -function getIpdaeData() { +async function getIpdaeData() { let data = cacheManager.get('ipdaeData'); if (!data) { logger.info("ipdaeData not in cache or expired, reloading from file."); - loadIpdaeData(); + await loadIpdaeData(); data = cacheManager.get('ipdaeData'); } return data || []; } -function getCelebrationConfig() { +async function getCelebrationConfig() { let data = cacheManager.get('celebrationConfig'); if (!data) { logger.info("celebrationConfig not in cache or expired, reloading from file."); - loadCelebrationConfig(); + await loadCelebrationConfig(); data = cacheManager.get('celebrationConfig'); } return data || {}; } -function getNotificationHistory() { +async function getNotificationHistory() { let data = cacheManager.get('notificationHistory'); if (!data) { logger.info("notificationHistory not in cache or expired, reloading from file."); - loadNotificationHistory(); + await loadNotificationHistory(); data = cacheManager.get('notificationHistory'); } return data || {}; } async function updateEnlistmentData(userId, date, type) { - let ipdaeDatas = getIpdaeData(); - let notificationHistory = getNotificationHistory(); + let ipdaeDatas = await getIpdaeData(); + let notificationHistory = await getNotificationHistory(); let notificationHistoryModified = false; const index = binarySearch(ipdaeDatas, userId); diff --git a/src/events/ready.js b/src/events/ready.js index bfa57fc..404e5b2 100644 --- a/src/events/ready.js +++ b/src/events/ready.js @@ -1,10 +1,48 @@ +require('dotenv').config(); +const fs = require('fs'); +const path = require('path'); +const { REST } = require('@discordjs/rest'); +const { Routes } = require('discord-api-types/v10'); +const { ActivityType } = require('discord.js'); const logger = require('../../modules/colorfulLogger'); const { handleCommandError } = require('../../utils/errorHandler'); const { checkAndSendCelebrationMessages, scheduleDailyCelebrationCheck } = require('../commands/military/celebration'); -const { ActivityType } = require('discord.js'); const { initializeRPC } = require('../handlers/rpcHandler'); const { initializeKoreanbotsUpdate } = require('../handlers/koreanbotsHandler'); +async function registerSlashCommands(client) { + const commands = []; + const commandFolders = fs.readdirSync(path.join(__dirname, '..', 'commands')); + + for (const folder of commandFolders) { + const commandFiles = fs.readdirSync(path.join(__dirname, '..', 'commands', folder)).filter(file => file.endsWith('.js')); + for (const file of commandFiles) { + try { + const filePath = path.join(__dirname, '..', 'commands', folder, file); + const command = require(filePath); + if (command.data) { + commands.push(command.data.toJSON()); + } + } catch (error) { + logger.error(`Error loading command from ${file}:`, error); + } + } + } + + const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN); + + try { + logger.info('Started refreshing application (/) commands.'); + await rest.put( + Routes.applicationCommands(client.user.id), + { body: commands }, + ); + logger.info('Successfully reloaded application (/) commands.'); + } catch (error) { + logger.error('Failed to reload application (/) commands:', error); + } +} + module.exports = { name: 'ready', once: true, @@ -12,6 +50,8 @@ module.exports = { try { logger.info(`Logged in as ${client.user.tag}`); + await registerSlashCommands(client); + initializeRPC(client); initializeKoreanbotsUpdate(client);