first commit
This commit is contained in:
105
src/handlers/commandHandler.js
Normal file
105
src/handlers/commandHandler.js
Normal file
@@ -0,0 +1,105 @@
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
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 { 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);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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 };
|
Reference in New Issue
Block a user