summaryrefslogtreecommitdiff
path: root/packages/gateway/src/commands/crp.ts
blob: 4758574c30948908532215f7ecd9799dbf29277b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { Message, Role } from "discord.js";

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 <source_role_mention> <target_role_mention1> [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) {
      console.error("Error copying role permissions:", error);
      await message.reply(
        "❌ Failed to copy role permissions. Check bot permissions and try again.",
      );
    }
  }
};