Repository navigation
Expand file tree
/
Copy pathbot.js
More file actions
494 lines (424 loc) · 20.6 KB
/
Copy pathbot.js
File metadata and controls
494 lines (424 loc) · 20.6 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
const { Client, PermissionsBitField, SlashCommandBuilder, ChannelType, GatewayIntentBits, REST, Routes, Events, ActivityType } = require('discord.js');
require('dotenv').config();
const rest = new REST({ version: '10' }).setToken(process.env.TEST_TOKEN);
const { Server, User, ReactionRole } = require('./models/models.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildPresences,
],
partials: [
'MESSAGE',
'CHANNEL',
'REACTION'
]
});
// Load Files
const adminCommands = require('./commands/admin_commands/admin_commands.js');
const communityCommands = require('./commands/community_commands/community_commands.js');
const configCommands = require('./commands/config_commands/configs_commands.js');
const { getReactionRoleConfigurations } = require('./commands/config_commands/configs_commands.js');
const help_menu_selected = require('./events/help_menu_selected.js');
const ownerCommands = require('./commands/owner_commands/owner_commands.js');
const {
// Reaction Roles
saveReactionRole,
loadReactionRoles
} = require('./commands/Utils_Functions/utils-reactions.js');
const { checkAllStreams } = require('./commands/Utils_Functions/utils-uplink.js');
const commands = [
// ! Set Link Commands
new SlashCommandBuilder()
.setName('set-link-channel')
.setDescription('Sets a channel for submitting links.')
.addChannelOption(option =>
option.setName('channel')
.setDescription('The channel where links will be sent')
.setRequired(true)
.addChannelTypes(ChannelType.GuildText)),
new SlashCommandBuilder()
.setName('submit-link')
.setDescription('Submits a link to the designated channel.')
.addStringOption(option =>
option.setName('link')
.setDescription('The link to submit')
.setRequired(true)),
// //
// ! Setup Reaction Commands
new SlashCommandBuilder()
.setName('setup-reaction-role')
.setDescription('Sets up a reaction role message')
.addChannelOption(option =>
option.setName('channel')
.setDescription('The channel where the reaction role message will be sent')
.setRequired(true)
),
// //
// ! Stream Announcement Commands
new SlashCommandBuilder()
.setName('setup-stream')
.setDescription('Sets up a stream to monitor and an announcement channel.')
.addStringOption(option =>
option.setName('platform')
.setDescription('Streaming platform (Twitch or YouTube)')
.setRequired(true)
.addChoices(
{ name: 'Twitch', value: 'twitch' },
{ name: 'YouTube', value: 'youtube' }
))
.addStringOption(option =>
option.setName('channel')
.setDescription('Twitch: Channel Name | YouTube: Channel ID')
.setRequired(true))
.addChannelOption(option =>
option.setName('announcement_channel')
.setDescription('The Discord channel for stream announcements')
.setRequired(true)
),
new SlashCommandBuilder()
.setName('setup-stream-message')
.setDescription('Sets up a stream message to monitor and an announcement channel.')
.addStringOption(option =>
option.setName('platform')
.setDescription('Streaming platform (Twitch or YouTube)')
.setRequired(true)
.addChoices(
{ name: 'Twitch', value: 'twitch' },
{ name: 'YouTube', value: 'youtube' }
))
.addStringOption(option =>
option.setName('channel')
.setDescription('Twitch: Channel Name | YouTube: Channel ID')
.setRequired(true))
.addStringOption(option =>
option.setName('message')
.setDescription('Message you want to send as a stream announcement')
.setRequired(true)
),
new SlashCommandBuilder()
.setName('update-stream')
.setDescription('Update Stream Details Information')
.addStringOption(option =>
option.setName('platform')
.setDescription('Streaming platform (Twitch or YouTube)')
.addChoices(
{ name: 'Twitch', value: 'twitch' },
{ name: 'YouTube', value: 'youtube' }))
.addStringOption(option =>
option.setName('channel_name')
.setDescription('The Twitch or YouTube channel name to monitor'))
.addChannelOption(option =>
option.setName('announcement_channel')
.setDescription('The Discord channel for stream announcements')),
new SlashCommandBuilder()
.setName('remove-stream')
.setDescription('Remove a platform or channel')
.addStringOption(option =>
option.setName('platform')
.setDescription('Streaming platform (Twitch or YouTube)')
.setRequired(true)
.addChoices(
{ name: 'Twitch', value: 'twitch' },
{ name: 'YouTube', value: 'youtube' }
))
.addStringOption(option =>
option.setName('channel_name')
.setDescription('The Twitch or YouTube channel name to monitor')
.setRequired(true)),
// //
// ! Playlist Video
new SlashCommandBuilder()
.setName('playlist-video')
.setDescription('Add or remove a YouTube video from a playlist')
.addStringOption(option =>
option.setName('action')
.setDescription('Choose to add or remove the video')
.setRequired(true)
.addChoices(
{ name: 'Add', value: 'add' },
{ name: 'Remove', value: 'remove' }
))
.addStringOption(option =>
option.setName('playlist_url')
.setDescription('The URL of the YouTube playlist')
.setRequired(true))
.addStringOption(option =>
option.setName('video_url')
.setDescription('The URL of the YouTube video to add or remove')
.setRequired(true)),
// //
// ! View Streams In Database
new SlashCommandBuilder()
.setName('view-streams')
.setDescription('View all streams in the database'),
// //
new SlashCommandBuilder()
.setName('announce')
.setDescription('Create an announcement with a customizable embed.')
.addStringOption(option =>
option.setName('title')
.setDescription('The title of the embed'))
.addStringOption(option =>
option.setName('color')
.setDescription('The color of the embed (hex code, e.g., #ff0000)'))
.addStringOption(option =>
option.setName('footer')
.setDescription('The footer text of the embed'))
.addStringOption(option =>
option.setName('image')
.setDescription('URL of the main image. Put .jpg if still image, .gif if animated, etc.)'))
.addChannelOption(option =>
option.setName('channel')
.setDescription('The channel to send the announcement to'))
.addStringOption(option =>
option.setName('notify_role')
.setDescription('The role to be notified when a new stream is detected')),
// //
// ! Help Command
new SlashCommandBuilder()
.setName('help')
.setDescription('Displays the list of available commands'),
// //
new SlashCommandBuilder()
.setName('leave-server')
.setDescription('Forces the bot to leave a server based on its ID.')
.addStringOption(option =>
option.setName('server_id')
.setDescription('The ID of the server the bot should leave')
.setRequired(true)),
// //
new SlashCommandBuilder()
.setName('auth-youtube')
.setDescription('Authorizes the bot to access YouTube.'),
].map(command => command.toJSON());
client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
client.user.setActivity('Moving to Server', { type: ActivityType.Watching });
client.user.setStatus('dnd');
try {
console.log('Started refreshing application (/) commands');
try {
console.log('Checked All Streams');
setInterval(() => checkAllStreams(client), 5 * 60 * 1000); // 5 * 60 * 1000
} catch (error) { console.error('Error checking streams:', error); }
// Fetch all guilds the bot is in
const guilds = await client.guilds.fetch();
for (const guild of guilds.values()) {
try {
// Check if the server already exists in the database
const existingServer = await Server.findOne({ where: { serverId: guild.id } });
// If the server already exists, skip creation
if (existingServer) {
console.log(`Server ${guild.name} already exists in the database.`);
} else {
// Create default entries for the server
await Server.create({
serverId: guild.id,
serverName: guild.name,
textChannelId: null,
loggingChannelId: null,
welcomeChannelId: null,
rankUpChannelId: null,
logLevel: 'low',
mute_role_level_1_id: null,
mute_role_level_2_id: null
});
}
} catch (error) {
console.error(`Error adding guild to database: ${guild.name} (${guild.id})`, error);
}
}
// Load reaction roles before registering slash commands
await loadReactionRoles();
const reactionRoleConfigurations = getReactionRoleConfigurations();
console.log('Reaction role configurations loaded:', reactionRoleConfigurations);
// List all reaction roles to the console
for (const [guildId, configs] of reactionRoleConfigurations.entries()) {
for (const config of configs) {
try {
const guild = await client.guilds.fetch(guildId);
console.log(`Fetching channel ID: ${config.channelId} for guild ${guild.name}`);
const channel = await guild.channels.fetch(config.channelId); // Explicitly fetch the channel
if (!channel) {
console.log(`Channel not found: ${config.channelId} in guild ${guild.name}`);
continue;
}
const message = await channel.messages.fetch(config.messageId);
// Iterate through the reactions on the message using a proper async loop
for (const reaction of message.reactions.cache.values()) {
const users = await reaction.users.fetch();
for (const user of users.values()) {
if (!user.bot) {
console.log(`Reaction found from user ${user.tag} on message ${message.id}`);
}
}
}
} catch (error) {
console.error(`Error processing guild ${guildId} and config ${config}:`, error);
}
}
}
// Register slash commands for each guild dynamically
for (const guild of guilds.values()) {
try {
await rest.put(
Routes.applicationGuildCommands(client.user.id, guild.id),
{ body: commands }
);
console.log(`Successfully registered commands for guild: ${guild.id}`);
} catch (error) {
console.error(`Error registering commands for guild: ${guild.id}`, error);
}
}
console.log('Successfully reloaded application (/) commands');
} catch (error) {
console.error('An error occurred during initialization:', error);
}
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand() && interaction.componentType !== 3) return;
const { commandName, options } = interaction;
if (commandName === 'set-link-channel') { console.log(`set link channel ran`); await configCommands.setupLinkChannel.execute(interaction, options); }
if (commandName ==='submit-link') { console.log(`submit link ran`); await communityCommands.submitLink.execute(interaction, options); }
// //
if (commandName === 'setup-stream') { console.log(`setup stream command ran`); await configCommands.setupStream.execute(interaction, options); }
if (commandName === 'setup-stream-message') { console.log(`setup stream message command ran`); await configCommands.setupStreamMessage.execute(interaction, options); }
if (commandName === 'update-stream') { console.log(`update stream command ran`); await configCommands.updateStream.execute(interaction, options); }
if (commandName === 'remove-stream') { console.log(`remove stream command ran`); await configCommands.removeStream.execute(interaction, options); }
if (commandName === 'view-streams') { console.log(`view streams command ran`); await adminCommands.viewStreams.execute(interaction, options); }
// //
if (commandName === 'announce') { console.log(`announce command ran`); await adminCommands.announce.execute(interaction, options); }
// //
if (commandName === 'auth-youtube') { console.log(`auth youtube command ran`); await configCommands.authYouTube.execute(interaction, options); }
// //
if (commandName === 'playlist-video') { console.log(`playlist video command ran`); await configCommands.playlistYouTube.execute(interaction, options) }
// //
if (commandName === 'setup-reaction-role') { console.log(`setup reaction command ran`); await configCommands.setupReactionRole.execute(interaction, options); }
// //
if (commandName === 'help') { console.log(`help command ran`); await communityCommands.help.execute(interaction); }
// //
if (commandName === 'leave-server') { console.log(`leave server command ran`); await ownerCommands.leaveServer.execute(interaction, options); }
});
client.on(Events.MessageReactionAdd, async (reaction, user) => {
// // Check if the reaction is in a guild and not from a bot
if (user.bot) return;
try {
// Fetch partial reactions and messages
if (reaction.partial) await reaction.fetch();
if (reaction.message.partial) await reaction.message.fetch();
} catch (err) {
console.error('Error fetching partial reaction or message:', err);
return; // Skip further processing if fetch fails
}
const guildId = reaction.message.guild.id;
try {
// Fetch all reaction roles for this message
const existingRoles = await ReactionRole.findAll({
where: {
guildId: guildId,
messageId: reaction.message.id,
},
});
// If no reaction roles exist for this message, return
if (!existingRoles || existingRoles.length === 0) {
console.log(`No reaction role configuration found for message ID: ${reaction.message.id} in guild: ${guildId}`);
return;
}
// Get the emoji identifier (custom vs standard)
const emojiIdentifier = reaction.emoji.id ? `<:${reaction.emoji.name}:${reaction.emoji.id}>` : reaction.emoji.name;
// Find the role associated with the emoji
const roleEntry = existingRoles.find(entry => entry.emoji === emojiIdentifier);
if (!roleEntry) {
console.log(`No role associated with emoji ${emojiIdentifier} for message ID: ${reaction.message.id}`);
return;
}
// Get the role from the guild
const role = reaction.message.guild.roles.cache.get(roleEntry.roleId);
if (!role) {
console.log(`Role not found in guild ${guildId} for role ID: ${roleEntry.roleId}`);
return;
}
// Fetch the member who reacted
const member = await reaction.message.guild.members.fetch(user.id);
// Fetch the bot's highest role to compare with the role to be added
const botMember = await reaction.message.guild.members.fetch(client.user.id);
const botHighestRole = botMember.roles.highest;
// Check if the bot has permission to manage roles and if its role is higher than the target role
if (!reaction.message.guild.members.me.permissions.has(PermissionsBitField.Flags.ManageRoles)) {
console.log(`Bot lacks 'Manage Roles' permission in guild ${guildId}`);
return;
}
if (botHighestRole.comparePositionTo(role) <= 0) {
console.log(`Bot's role (${botHighestRole.name}) is not higher than the role to be added (${role.name})`);
return; // Prevent adding the role to avoid the permission error
}
// Add the role to the member
await member.roles.add(role);
console.log(`Added role ${role.name} to user ${user.username}`);
} catch (error) {
console.error('Error adding role based on reaction:', error);
return;
}
});
client.on(Events.MessageReactionRemove, async (reaction, user) => {
// Check if the reaction is in a guild and not from a bot
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
const guildId = reaction.message.guild.id;
try {
// Fetch all reaction roles for this message
const existingRoles = await ReactionRole.findAll({
where: {
guildId: guildId,
messageId: reaction.message.id,
},
});
// If no reaction roles exist for this message, return
if (!existingRoles || existingRoles.length === 0) {
console.log(`No reaction role configuration found for message ID: ${reaction.message.id} in guild: ${guildId}`);
return;
}
// Get the emoji identifier (custom vs standard)
const emojiIdentifier = reaction.emoji.id ? `<:${reaction.emoji.name}:${reaction.emoji.id}>` : reaction.emoji.name;
// Find the role associated with the emoji
const roleEntry = existingRoles.find(entry => entry.emoji === emojiIdentifier);
if (!roleEntry) {
console.log(`No role associated with emoji ${emojiIdentifier} for message ID: ${reaction.message.id}`);
return;
}
// Get the role from the guild
const role = reaction.message.guild.roles.cache.get(roleEntry.roleId);
if (!role) {
console.log(`Role not found in guild ${guildId} for role ID: ${roleEntry.roleId}`);
return;
}
// Fetch the member who reacted
const member = await reaction.message.guild.members.fetch(user.id);
// Fetch the bot's highest role to compare with the role to be added
const botMember = await reaction.message.guild.members.fetch(client.user.id);
const botHighestRole = botMember.roles.highest;
// Check if the bot has permission to manage roles and if its role is higher than the target role
if (!reaction.message.guild.members.me.permissions.has(PermissionsBitField.Flags.ManageRoles)) {
console.log(`Bot lacks 'Manage Roles' permission in guild ${guildId}`);
return;
}
if (botHighestRole.comparePositionTo(role) <= 0) {
console.log(`Bot's role (${botHighestRole.name}) is not higher than the role to be added (${role.name})`);
return; // Prevent adding the role to avoid the permission error
}
// Add the role to the member
await member.roles.remove(role);
console.log(`Removed role ${role.name} to user ${user.username}`);
} catch (error) {
console.error('Error adding role based on reaction:', error);
return;
}
});
client.login(process.env.TEST_TOKEN);