commandhandler refactor

This commit is contained in:
yeongaori
2025-08-16 02:43:23 +09:00
parent f6b81ad909
commit b7311f0907
5 changed files with 164 additions and 143 deletions

View File

@@ -1,105 +1,55 @@
const { EmbedBuilder } = require('discord.js');
const { Collection } = require('discord.js');
const fs = require('fs');
const path = require('path');
const logger = require('../../modules/colorfulLogger');
const {
startDateCommands,
endDateCommands,
randomCommands,
diceCommands,
deleteCommands,
adminUserIds,
commandPrefix
} = require('../../config/constants');
const { setEnlistmentDate } = require('../commands/military/setEnlistmentDate');
const { getDischargeInfo } = require('../commands/military/getDischargeInfo');
const { handleGiftCode } = require('../commands/admin/giftCode');
const { handleEval } = require('../commands/admin/eval');
const { handleTest } = require('../commands/admin/test');
const { handleDelete } = require('../commands/admin/delete');
const { handleRandom } = require('../commands/general/random');
const { commandPrefix } = require('../../config/constants');
const { getNameById } = require('../utils/discordUtils');
async function handleCommand(message) {
const { channel, content, author, guild } = message;
const userId = author.id;
const userName = getNameById(message.client, userId, guild);
function loadLegacyCommands(client) {
client.legacyCommands = new Collection();
const commandFolders = fs.readdirSync(path.join(__dirname, '..', 'commands'));
const contentWithoutPrefix = content.substring(commandPrefix.length);
const args = contentWithoutPrefix.trim().split(/\s+/);
const commandName = args[0].toLowerCase();
if (startDateCommands.includes(commandName)) {
logger.info(`Processing !${commandName} from ${userName} (${userId}) in ${guild ? `guild ${guild.name} (${guild.id})` : 'DM'}`);
const argStartDate = args[1];
const argStartType = args[2];
const customUserIdForAdmin = args[3] && adminUserIds.includes(userId) ? args[3] : undefined;
const resultEmbed = await setEnlistmentDate(message.client, userId, argStartDate, argStartType, `!${commandName}`, customUserIdForAdmin);
await channel.send({ embeds: [resultEmbed] });
return;
}
if (endDateCommands.includes(commandName)) {
logger.info(`Processing !${commandName} from ${userName} (${userId}) in ${guild ? `guild ${guild.name} (${guild.id})` : 'DM'}`);
let targetUserIdForCmd = userId;
let decimalArgForCmd = 4;
let decimalArgIndex = 1;
if (args[1]) {
const mentionMatch = args[1].match(/^<@!?(\d+)>$/);
if (mentionMatch) {
targetUserIdForCmd = mentionMatch[1];
decimalArgIndex = 2;
} else if (/^\d{17,19}$/.test(args[1])) {
targetUserIdForCmd = args[1];
decimalArgIndex = 2;
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 command = require(path.join(__dirname, '..', 'commands', folder, file));
if (command.legacy) {
const legacyCommand = command.legacy;
client.legacyCommands.set(legacyCommand.name, legacyCommand);
if (legacyCommand.aliases && legacyCommand.aliases.length > 0) {
legacyCommand.aliases.forEach(alias => client.legacyCommands.set(alias, legacyCommand));
}
}
} catch (error) {
logger.error(`Error loading legacy command from ${file}:`, error);
}
}
if (args[decimalArgIndex] && !isNaN(parseInt(args[decimalArgIndex]))) {
decimalArgForCmd = parseInt(args[decimalArgIndex]);
} else if (decimalArgIndex === 1 && args[1] && !isNaN(parseInt(args[1]))) {
decimalArgForCmd = parseInt(args[1]);
}
const targetUserNameForCmd = getNameById(message.client, targetUserIdForCmd, guild);
const resultEmbed = getDischargeInfo(message.client, targetUserIdForCmd, targetUserNameForCmd, decimalArgForCmd, false);
await channel.send({ embeds: [resultEmbed] });
return;
}
if (randomCommands.includes(commandName)) {
await handleRandom(message, args.slice(1));
return;
}
if (diceCommands.includes(commandName)) {
await handleRandom(message, ['1', '6']);
return;
}
if (commandName === 'gicode') {
await handleGiftCode(message, 'genshin');
return;
}
if (commandName === 'hsrcode') {
await handleGiftCode(message, 'hsr');
return;
}
if (commandName === 'eval') {
const evalCode = message.content.substring(commandPrefix.length + commandName.length).trim();
await handleEval(message, evalCode);
return;
}
if (commandName === 'test') {
await handleTest(message);
}
if (deleteCommands.includes(commandName)) {
await handleDelete(message.client, message);
return;
}
}
module.exports = { handleCommand };
async function handleCommand(message) {
if (!message.content.startsWith(commandPrefix) || message.author.bot) return;
const args = message.content.slice(commandPrefix.length).trim().split(/\s+/);
const commandName = args.shift().toLowerCase();
const command = message.client.legacyCommands.get(commandName);
if (!command) return;
const { author, guild } = message;
const userName = getNameById(message.client, author.id, guild);
logger.info(`Processing !${commandName} from ${userName} (${author.id}) in ${guild ? `guild ${guild.name} (${guild.id})` : 'DM'}`);
try {
await command.execute(message, args);
} catch (error) {
logger.error(`Error executing legacy command ${commandName}:`, error);
await message.reply('명령어 실행 중 오류가 발생했습니다.');
}
}
module.exports = { loadLegacyCommands, handleCommand };