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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
import { Message } from "discord.js";
import { replyWithCleanup } from "../utilities";
export const handleRoleCommand = async (message: Message) => {
if (message.author.bot) return;
if (message.content.toLowerCase().startsWith("uma!role")) {
const application = await message.client.application?.fetch();
const botOwnerId = application?.owner?.id;
const guildOwnerId = message.guild?.ownerId;
if (message.author.id !== botOwnerId && message.author.id !== guildOwnerId)
return;
const parameters = message.content.split(" ").slice(1);
if (parameters.length < 3) {
await replyWithCleanup(
message,
"❌ Usage: `uma!role <server_id> <user_id_or_mention> <role_id_or_mention> [role_id_or_mention...]`\nExamples:\n- `uma!role 1234567890123456789 @user @role`\n- `uma!role 1234567890123456789 @user @role1 @role2 @role3`\n- `uma!role 1234567890123456789 9876543210987654321 1111111111111111111`",
);
return;
}
const serverInput = parameters[0];
const userInput = parameters[1];
const roleInputs = parameters.slice(2);
try {
let serverId: string;
if (/^\d{17,19}$/.test(serverInput)) {
serverId = serverInput;
} else {
await replyWithCleanup(
message,
"❌ Invalid server ID format. Use a valid Discord server ID.",
);
return;
}
let targetUserId: string;
if (userInput.startsWith("<@") && userInput.endsWith(">")) {
targetUserId = userInput.slice(2, -1).replace("!", "");
} else if (/^\d{17,19}$/.test(userInput)) {
targetUserId = userInput;
} else {
await replyWithCleanup(
message,
"❌ Invalid user format. Use a user mention (@user) or user ID.",
);
return;
}
const targetRoleIds: string[] = [];
for (const roleInput of roleInputs)
if (roleInput.startsWith("<@&") && roleInput.endsWith(">")) {
targetRoleIds.push(roleInput.slice(3, -1));
} else if (/^\d{17,19}$/.test(roleInput)) {
targetRoleIds.push(roleInput);
} else {
await replyWithCleanup(
message,
`❌ Invalid role format: ${roleInput}. Use a role mention (@role) or role ID.`,
);
return;
}
const targetUser = await message.client.users.fetch(targetUserId);
if (!targetUser) {
await replyWithCleanup(message, "❌ User not found.");
return;
}
const guild = message.client.guilds.cache.get(serverId);
if (!guild) {
await replyWithCleanup(
message,
`❌ Server with ID ${serverId} not found or bot is not in that server.`,
);
return;
}
const targetMember = await guild.members.fetch(targetUserId);
if (!targetMember) {
await replyWithCleanup(
message,
`❌ User is not a member of ${guild.name}.`,
);
return;
}
const results: string[] = [];
for (const targetRoleId of targetRoleIds) {
const role = guild.roles.cache.get(targetRoleId);
if (!role) {
await replyWithCleanup(
message,
`❌ Role with ID ${targetRoleId} not found in ${guild.name}.`,
);
return;
}
const hasRole = targetMember.roles.cache.has(targetRoleId);
if (hasRole) {
await targetMember.roles.remove(role);
results.push(`Removed **${role.name}**`);
} else {
await targetMember.roles.add(role);
results.push(`Added **${role.name}**`);
}
}
const actionText = results.join(", ");
await replyWithCleanup(
message,
`✅ ${actionText} for **${targetUser.username}** in **${guild.name}**.`,
);
} catch (error) {
console.error("Error in role command:", error);
await replyWithCleanup(
message,
"❌ Failed to manage role. Check bot permissions and try again.",
);
}
}
};
|