-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
341 lines (308 loc) · 14.5 KB
/
script.js
File metadata and controls
341 lines (308 loc) · 14.5 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
const { Client, GatewayIntentBits, PermissionsBitField, ChannelType, REST, Routes, SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType } = require('discord.js');
const mongoose = require('mongoose');
// add envs
mongoose.connect(mongoUri, { useNewUrlParser: true, useUnifiedTopology: true });
const teamSchema = new mongoose.Schema({
guildId: String,
teamName: String,
roleId: String,
textChannelId: String,
members: [String],
devpost: [String],
github: String
});
const Team = mongoose.model('Team', teamSchema);
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers
]
});
const commands = [
new SlashCommandBuilder()
.setName('create_team')
.setDescription('Create a team with role, channels, and permissions')
.addStringOption(option =>
option.setName('team_name')
.setDescription('Name of the team')
.setRequired(true))
.addStringOption(option =>
option.setName('members')
.setDescription('Mentioned Discord members (space separated)')
.setRequired(true))
.addStringOption(option =>
option.setName('devpost')
.setDescription('Devpost usernames (comma separated)')
.setRequired(true))
.addStringOption(option =>
option.setName('github')
.setDescription('GitHub repo URL')
.setRequired(false))
.toJSON(),
new SlashCommandBuilder()
.setName('showteam')
.setDescription('Show details of a team')
.addStringOption(option =>
option.setName('team_name')
.setDescription('Name of the team')
.setRequired(true))
.toJSON(),
new SlashCommandBuilder()
.setName('updateteam')
.setDescription('Update team members, devpost usernames, or github repo')
.addStringOption(option =>
option.setName('team_name')
.setDescription('Name of the team')
.setRequired(true))
.addStringOption(option =>
option.setName('members')
.setDescription('Mentioned Discord members (space separated)')
.setRequired(false))
.addStringOption(option =>
option.setName('devpost')
.setDescription('Devpost usernames (comma separated)')
.setRequired(false))
.addStringOption(option =>
option.setName('github')
.setDescription('GitHub repo URL')
.setRequired(false))
.toJSON(),
new SlashCommandBuilder()
.setName('showallteams')
.setDescription('Show details of all teams')
.toJSON()
];
const rest = new REST({ version: '10' }).setToken(token);
async function registerCommands() {
try {
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands }
);
console.log('Slash commands registered.');
} catch (error) {
console.error(error);
}
}
registerCommands();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
// Permission check: only users above the bot's highest role can use commands
const botMember = await interaction.guild.members.fetchMe();
const authorMember = await interaction.guild.members.fetch(interaction.user.id);
if (authorMember.roles.highest.position <= botMember.roles.highest.position) {
const embed = new EmbedBuilder()
.setTitle("Permission Denied")
.setDescription("You don't have permission to use this command.")
.setColor(0xff0000);
return interaction.reply({ embeds: [embed], ephemeral: true });
}
if (interaction.commandName === 'create_team') {
await interaction.deferReply({ ephemeral: false });
const teamName = interaction.options.getString('team_name');
const membersRaw = interaction.options.getString('members');
const devpostRaw = interaction.options.getString('devpost');
const githubRepo = interaction.options.getString('github');
const memberMentions = membersRaw.match(/<@!?(\d+)>/g) || [];
if (memberMentions.length === 0) {
const embed = new EmbedBuilder()
.setTitle("Missing Members")
.setDescription("Please mention at least one member.")
.setColor(0xff0000);
return interaction.editReply({ embeds: [embed] });
}
const devpostUsernames = devpostRaw.split(',').map(u => u.trim()).filter(Boolean);
if (devpostUsernames.length === 0) {
const embed = new EmbedBuilder()
.setTitle("Missing Devpost Usernames")
.setDescription("Please provide at least one Devpost username.")
.setColor(0xff0000);
return interaction.editReply({ embeds: [embed] });
}
let role = await interaction.guild.roles.create({
name: teamName,
mentionable: true,
reason: `Team role for ${teamName}`
});
for (const mention of memberMentions) {
const memberId = mention.replace(/[<@!>]/g, '');
const member = await interaction.guild.members.fetch(memberId).catch(() => null);
if (member) {
await member.roles.add(role);
}
}
await Team.findOneAndUpdate(
{ guildId: interaction.guild.id, teamName: teamName.toLowerCase() },
{
guildId: interaction.guild.id,
teamName: teamName.toLowerCase(),
roleId: role.id,
textChannelId: null,
members: memberMentions,
devpost: devpostUsernames,
github: githubRepo
},
{ upsert: true, new: true }
);
const embed = new EmbedBuilder()
.setTitle(`Team "${teamName}" Created`)
.addFields(
{ name: 'Role', value: `<@&${role.id}>`, inline: true },
{ name: 'Devpost Usernames', value: devpostUsernames.join(', '), inline: false }
)
.setColor(0x00ff99);
if (githubRepo) embed.addFields({ name: 'GitHub Repo', value: githubRepo, inline: false });
await interaction.editReply({ embeds: [embed] });
}
if (interaction.commandName === 'showteam') {
await interaction.deferReply({ ephemeral: false });
let teamName = interaction.options.getString('team_name');
if (teamName.startsWith('@')) teamName = teamName.slice(1).trim();
const team = await Team.findOne({ guildId: interaction.guild.id, teamName: teamName.toLowerCase() });
if (!team) {
const embed = new EmbedBuilder()
.setTitle("Team Not Found")
.setDescription(`No team found with the name "${teamName}".`)
.setColor(0xff0000);
return interaction.editReply({ embeds: [embed] });
}
const embed = new EmbedBuilder()
.setTitle(`Team "${teamName}" Details`)
.addFields(
{ name: 'Role', value: `<@&${team.roleId}>`, inline: true },
{ name: 'Text Channel', value: team.textChannelId ? `<#${team.textChannelId}>` : 'N/A', inline: true },
{ name: 'Members', value: team.members.join(' '), inline: false },
{ name: 'Devpost Usernames', value: team.devpost.join(', '), inline: false }
)
.setColor(0x3399ff);
if (team.github) embed.addFields({ name: 'GitHub Repo', value: team.github, inline: false });
await interaction.editReply({ embeds: [embed] });
}
if (interaction.commandName === 'updateteam') {
await interaction.deferReply({ ephemeral: true });
let teamName = interaction.options.getString('team_name');
if (teamName.startsWith('@')) teamName = teamName.slice(1).trim();
const membersRaw = interaction.options.getString('members');
const devpostRaw = interaction.options.getString('devpost');
const githubRaw = interaction.options.getString('github');
const team = await Team.findOne({ guildId: interaction.guild.id, teamName: teamName.toLowerCase() });
if (!team || !team.members.includes(`<@${interaction.user.id}>`) && !team.members.includes(`<@!${interaction.user.id}>`)) {
const embed = new EmbedBuilder()
.setTitle("Permission Denied")
.setDescription("You can only update your own team.")
.setColor(0xff0000);
return interaction.editReply({ embeds: [embed] });
}
const confirmButton = new ButtonBuilder()
.setCustomId("confirm_button")
.setLabel("Yes, update details")
.setStyle(ButtonStyle.Danger);
const cancelButton = new ButtonBuilder()
.setCustomId("cancel_button")
.setLabel("Cancel")
.setStyle(ButtonStyle.Secondary);
const row = new ActionRowBuilder()
.addComponents(confirmButton, cancelButton);
const response = await interaction.editReply({
content: 'Are you absolutely sure you want to update team data? This action will delete all previous data!',
components: [row],
ephemeral: true,
});
const collectorFilter = i => i.user.id === interaction.user.id;
try {
const confirmation = await response.awaitMessageComponent({ filter: collectorFilter, time: 60_000 });
if (confirmation.customId === 'confirm_button') {
confirmButton.setDisabled(true);
cancelButton.setDisabled(true);
await confirmation.update({ components: [row] });
if (!team) {
const embed = new EmbedBuilder()
.setTitle("Team Not Found")
.setDescription(`No team found with the name "${teamName}".`)
.setColor(0xff0000);
return interaction.editReply({ embeds: [embed] });
}
let updated = false;
if (membersRaw) {
const memberMentions = membersRaw.match(/<@!?(\d+)>/g) || [];
team.members = memberMentions;
const role = await interaction.guild.roles.fetch(team.roleId);
const allMembers = await interaction.guild.members.fetch();
for (const member of allMembers.values()) {
if (member.roles.cache.has(role.id)) {
await member.roles.remove(role).catch(() => {});
}
}
for (const mention of memberMentions) {
const memberId = mention.replace(/[<@!>]/g, '');
const member = await interaction.guild.members.fetch(memberId).catch(() => null);
if (member) {
await member.roles.add(role).catch(() => {});
}
}
updated = true;
}
if (devpostRaw) {
const devpostUsernames = devpostRaw.split(',').map(u => u.trim()).filter(Boolean);
team.devpost = devpostUsernames;
updated = true;
}
if (githubRaw) {
team.github = githubRaw;
updated = true;
}
if (!updated) {
const embed = new EmbedBuilder()
.setTitle("Nothing to Update")
.setDescription("Please provide members and/or devpost usernames and/or github repo to update.")
.setColor(0xffcc00);
return interaction.editReply({ embeds: [embed] });
}
await team.save();
const embed = new EmbedBuilder()
.setTitle(`Team "${teamName}" Updated`)
.addFields(
{ name: 'Role', value: `<@&${team.roleId}>`, inline: true },
{ name: 'Text Channel', value: team.textChannelId ? `<#${team.textChannelId}>` : 'N/A', inline: true },
{ name: 'Members', value: team.members.join(' '), inline: false },
{ name: 'Devpost Usernames', value: team.devpost.join(', '), inline: false }
)
.setColor(0x00ccff);
if (team.github) embed.addFields({ name: 'GitHub Repo', value: team.github, inline: false });
await interaction.editReply({ embeds: [embed] });
} else if (confirmation.customId === 'cancel_button'){
confirmButton.setDisabled(true);
cancelButton.setDisabled(true);
await confirmation.update({ content: 'Deletion cancelled.', components: [row] });
}
} catch (e) {
confirmButton.setDisabled(true);
cancelButton.setDisabled(true);
await interaction.editReply({ content: 'Confirmation not received in time, action cancelled.', components: [row] });
}
}
if (interaction.commandName === 'showallteams') {
await interaction.deferReply({ ephemeral: false });
const teams = await Team.find({ guildId: interaction.guild.id });
if (!teams.length) {
const embed = new EmbedBuilder()
.setTitle("No Teams Found")
.setDescription("There are no teams in this server.")
.setColor(0xff0000);
return interaction.editReply({ embeds: [embed] });
}
const embed = new EmbedBuilder()
.setTitle("All Teams")
.setColor(0x00bfff);
for (const team of teams) {
let value = `Role: <@&${team.roleId}>\nText: ${team.textChannelId ? `<#${team.textChannelId}>` : 'N/A'}\nMembers: ${team.members.join(' ')}\nDevpost: ${team.devpost.join(', ')}`;
if (team.github) value += `\nGitHub: ${team.github}`;
embed.addFields({ name: team.teamName, value: value.length > 1024 ? value.slice(0, 1021) + '...' : value, inline: false });
}
await interaction.editReply({ embeds: [embed] });
}
});
client.login(token);