129 lines
4.4 KiB
JavaScript
129 lines
4.4 KiB
JavaScript
const path = require("path");
|
|
const Embed = require(path.join(__dirname, "../functions/embed"));
|
|
|
|
/**
|
|
* Validates and processes role assignments from a message
|
|
* @param {Object} client - The client instance
|
|
* @param {Object} message - The message object
|
|
* @param {Object} db - Database instance
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async function Collector(client, message, db) {
|
|
const ROLE_REGEX = /{role:(.*?)}/g;
|
|
const MAX_ROLES = 20;
|
|
|
|
// Get collector for the message author
|
|
const collector = client.messageCollector.get(message.authorId);
|
|
|
|
// Validate message contains role tags
|
|
const roleMatches = message.content.match(ROLE_REGEX);
|
|
if (!roleMatches?.length) {
|
|
await sendErrorResponse(message, client, db, "Events.messageCreate.noRoles", `\`{role:Red}\``);
|
|
return;
|
|
}
|
|
|
|
// Extract role names from message
|
|
const roleNames = roleMatches.map(match => match.match(/{role:(.*?)}/)[1]);
|
|
|
|
// Check role count limit
|
|
if (roleNames.length > MAX_ROLES) {
|
|
await sendErrorResponse(message, client, db, "Events.messageCreate.maxRoles");
|
|
return;
|
|
}
|
|
|
|
collector.regex = roleNames;
|
|
|
|
// Find matching server roles
|
|
const serverRoles = [...message.server.roles].map(([id, role]) => ({ id, role }));
|
|
const matchedRoles = roleNames.map(name =>
|
|
serverRoles.find(({ role }) => role.name.toLowerCase() === name.toLowerCase())
|
|
);
|
|
|
|
// Check for unknown roles
|
|
const unknownRoles = roleNames.filter((name, index) => !matchedRoles[index]);
|
|
if (unknownRoles.length > 0) {
|
|
await sendErrorResponse(
|
|
message,
|
|
client,
|
|
db,
|
|
"Events.messageCreate.unknown",
|
|
unknownRoles.map(role => `\`{role:${role}}\``).join(", ")
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Check for duplicate roles
|
|
const roleIds = matchedRoles.map(({ id }) => id);
|
|
const duplicates = roleIds.filter((id, index) => roleIds.indexOf(id) !== index);
|
|
if (duplicates.length > 0) {
|
|
const duplicateRoles = duplicates.map(id =>
|
|
matchedRoles.find(({ id: roleId }) => roleId === id).role.name
|
|
);
|
|
await sendErrorResponse(
|
|
message,
|
|
client,
|
|
db,
|
|
"Events.messageCreate.duplicate",
|
|
duplicateRoles.map(name => `\`{role:${name}}\``).join(", ")
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Check bot role permissions
|
|
const botRole = message.channel.server.member.orderedRoles.reverse()[0];
|
|
if (!botRole) {
|
|
await sendErrorResponse(message, client, db, "Events.messageCreate.noBotRole");
|
|
return;
|
|
}
|
|
|
|
// Check role positions
|
|
const invalidPositions = matchedRoles.filter(({ role }) => role.rank <= botRole.rank);
|
|
if (invalidPositions.length > 0) {
|
|
await sendErrorResponse(
|
|
message,
|
|
client,
|
|
db,
|
|
"Events.messageCreate.positions",
|
|
invalidPositions.map(({ role }) => `\`{role:${role.name}}\``).join(", ")
|
|
);
|
|
return;
|
|
}
|
|
|
|
// Process valid role assignment
|
|
await message.delete().catch(() => {});
|
|
collector.roles = matchedRoles;
|
|
|
|
const react = [client.config.emojis.check];
|
|
const messageContent = collector.type === "content"
|
|
? { content: message.content, interactions: [react] }
|
|
: {
|
|
embeds: [new Embed().setDescription(message.content).setColor("#A52F05")],
|
|
interactions: [react]
|
|
};
|
|
|
|
const sentMessage = await message.channel.sendMessage(messageContent);
|
|
collector.messageId = sentMessage.id;
|
|
}
|
|
|
|
/**
|
|
* Helper function to send error responses
|
|
* @param {Object} message - The message object
|
|
* @param {Object} client - The client instance
|
|
* @param {Object} db - Database instance
|
|
* @param {string} translationKey - Translation key for the error message
|
|
* @param {string} [additionalInfo] - Additional information to append to the error message
|
|
*/
|
|
async function sendErrorResponse(message, client, db, translationKey, additionalInfo = "") {
|
|
const errorMessage = additionalInfo
|
|
? `${client.translate.get(db.language, translationKey)}\n${additionalInfo}`
|
|
: client.translate.get(db.language, translationKey);
|
|
|
|
await message.reply(
|
|
{ embeds: [new Embed().setColor("#FF0000").setDescription(errorMessage)] },
|
|
false
|
|
).catch(() => {});
|
|
|
|
await message.react(client.config.emojis.cross).catch(() => {});
|
|
}
|
|
|
|
module.exports = Collector; |