import { Message, Role } from "discord.js"; import { logUnexpectedDiscordAPIError } from "../utilities"; export const handleCrpCommand = async (message: Message) => { if (message.author.bot) return; if (message.content.toLowerCase().startsWith("uma!crp")) { const application = await message.client.application?.fetch(); const ownerId = application?.owner?.id; if (message.author.id !== ownerId) { await message.reply("❌ Only the server owner can use this command."); return; } const parameters = message.content.split(" ").slice(1); if (parameters.length < 2) { await message.reply( "❌ Usage: `uma!crp [target_role_mention2] ...`\nExample: `uma!crp @everyone @test1 @test2 @test3`", ); return; } const sourceRoleMention = parameters[0]; const targetRoleMentions = parameters.slice(1); let sourceRole: Role | undefined; if (sourceRoleMention === "@everyone") { sourceRole = message.guild?.roles.everyone; } else { const sourceRoleMatch = sourceRoleMention.match(/<@&(\d+)>/); if (!sourceRoleMatch) { await message.reply( "❌ Please mention a valid source role. Example: `@everyone` or `@RoleName`", ); return; } const sourceRoleId = sourceRoleMatch[1]; sourceRole = message.guild?.roles.cache.get(sourceRoleId); } if (!sourceRole) { await message.reply("❌ Source role not found."); return; } const targetRoles: Role[] = []; for (const mention of targetRoleMentions) { const targetRoleMatch = mention.match(/<@&(\d+)>/); if (!targetRoleMatch) { await message.reply( `❌ Invalid role mention: ${mention}. Please mention valid roles.`, ); return; } const targetRoleId = targetRoleMatch[1]; const targetRole = message.guild?.roles.cache.get(targetRoleId); if (!targetRole) { await message.reply(`❌ Target role not found: ${mention}`); return; } targetRoles.push(targetRole); } try { const sourcePermissions = sourceRole.permissions.toArray(); for (const targetRole of targetRoles) await targetRole.setPermissions(sourcePermissions); const targetRoleNames = targetRoles.map((role) => role.name).join(", "); await message.reply( `✅ Successfully copied permissions from **${sourceRole.name}** to: **${targetRoleNames}**`, ); } catch (error) { logUnexpectedDiscordAPIError(error); await message.reply( "❌ Failed to copy role permissions. Check bot permissions and try again.", ); } } };