-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
101 lines (81 loc) · 3.17 KB
/
index.js
File metadata and controls
101 lines (81 loc) · 3.17 KB
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
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers
]
});
const TOKEN = process.env.DISCORD_TOKEN;
client.once('ready', () => {
console.log(`[BOT] Logged in as ${client.user.tag}`);
});
client.on('messageCreate', async message => {
if (message.author.bot) return;
if (!message.content.startsWith('!addrole')) return;
console.log(`[COMMAND] Received: ${message.content} from ${message.author.tag}`);
const args = message.content.split(' ');
if (args.length < 3) {
console.log(`[ERROR] Invalid command format.`);
return message.reply('Usage: !addrole <roleId> <comma-separated user IDs>');
}
const roleId = args[1];
const userIdsStr = args.slice(2).join(' ');
const userIds = userIdsStr
.split(',')
.map(id => id.trim())
.filter(id => id.length > 0);
if (userIds.length === 0) {
console.log(`[ERROR] No valid user IDs provided.`);
return message.reply('❌ No valid user IDs provided.');
}
const role = message.guild.roles.cache.get(roleId);
if (!role) {
console.log(`[ERROR] Role with ID ${roleId} not found.`);
return message.reply(`❌ Role with ID ${roleId} not found.`);
}
console.log(`[ROLE] Target role: ${role.name} (${role.id})`);
if (message.member.roles.highest.comparePositionTo(role) <= 0) {
console.log(`[PERMISSION] User ${message.author.tag} does not have permission to assign ${role.name}.`);
return message.reply(`❌ You cannot assign the role ${role.name} because it is higher or equal to your highest role.`);
}
const botMember = await message.guild.members.fetch(client.user.id);
if (botMember.roles.highest.comparePositionTo(role) <= 0) {
console.log(`[PERMISSION] Bot lacks permission to assign ${role.name}.`);
return message.reply(`❌ I cannot assign the role ${role.name} because it is higher or equal to my highest role.`);
}
const processingMessage = await message.reply('Processing... Please wait.');
let success = [];
let failed = [];
for (const userId of userIds) {
try {
const member = await message.guild.members.fetch(userId);
await member.roles.add(role);
console.log(`[SUCCESS] Added ${role.name} to ${member.user.tag}`);
success.push(member.user.tag);
} catch (err) {
console.error(`[ERROR] Failed to add ${role.name} to user ID ${userId}:`, err);
failed.push(userId);
}
}
let replyMessage = '';
if (success.length > 0) {
replyMessage += `✅ ${role.name} added to: ${success.join(', ')}\n`;
}
if (failed.length > 0) {
replyMessage += `❌ Failed to add ${role.name} to: ${failed.join(', ')}`;
}
console.log(`[RESULT] ${replyMessage}`);
const chunkSize = 2000;
let remainingMessage = replyMessage;
while (remainingMessage.length > chunkSize) {
const chunk = remainingMessage.slice(0, chunkSize);
await processingMessage.reply(chunk);
remainingMessage = remainingMessage.slice(chunkSize);
}
if (remainingMessage.length > 0) {
await processingMessage.reply(remainingMessage);
}
});
client.login(TOKEN);