-
Notifications
You must be signed in to change notification settings - Fork 2
/
interactions.ts
478 lines (432 loc) · 21 KB
/
interactions.ts
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
import { ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, ChannelType, EmbedBuilder, GuildMember, GuildMemberRoleManager, Interaction, Message, ModalBuilder, PermissionsBitField, TextChannel, TextInputBuilder, TextInputStyle, User, UserSelectMenuBuilder, VoiceChannel } from "npm:discord.js"
import { saveTranscript, findUser, lastLogin, getServerURLs, getLastDaily, addCoins, setLastDaily, getCoins, removeCoins, addPartner, removePartner, getPartners, getMemberFromBBNId } from "./db.ts";
export async function handleInteraction(interaction: Interaction) {
if (interaction.isButton()) {
switch (interaction.customId) {
case "lock": {
lockVoice(interaction, true)
break;
}
case "unlock": {
lockVoice(interaction, false)
break;
}
case "create_ticket": {
const ticket_modal = new ModalBuilder()
.setTitle("Kindly enter this information.")
.setCustomId("ticket_modal");
const user_reason = new TextInputBuilder()
.setCustomId("ticket_reason")
.setLabel(`Why do you want to open a ticket?`)
.setRequired(true)
.setStyle(TextInputStyle.Paragraph);
const row_user_reason = new ActionRowBuilder<TextInputBuilder>().addComponents(user_reason);
ticket_modal.addComponents(row_user_reason);
await interaction.showModal(ticket_modal);
break;
}
case "close_ticket": {
const channel = interaction.channel as TextChannel;
interaction.reply({
content: `> We're closing your ticket. Please be patient. Ticket closed by ${interaction.user.tag}`,
});
const messages: Message[] = [];
let message = await channel.messages
.fetch({ limit: 1 })
.then(messagePage => (messagePage.size === 1 ? messagePage.at(0) : null));
while (message) {
await channel.messages
.fetch({ limit: 100, before: message.id })
.then(messagePage => {
messagePage.forEach(msg => messages.push(msg));
message = 0 < messagePage.size ? messagePage.at(messagePage.size - 1) : null;
});
}
let member;
try {
member = await interaction.guild?.members.fetch(channel.name.split("-")[ 1 ]);
// deno-lint-ignore no-empty
} catch (_) { }
// deno-lint-ignore no-explicit-any
const transcript: any = {
messages: [],
closed: `Ticket closed by ${interaction.user.tag}`,
with: `${member ? member.user.tag : "Unknown User"} (${channel.name.split("-")[ 1 ]})`
};
for (const message of messages.values()) {
// deno-lint-ignore no-explicit-any
const obj: any = {
author: message.author.tag,
authorid: message.author.id,
content: message.content,
timestamp: message.createdTimestamp,
avatar: message.author.displayAvatarURL(),
};
if (message.attachments.size > 0) {
obj.attachments = message.attachments.map(a => a.url);
}
if (message.embeds.length > 0) {
obj.embed = message.embeds[ 0 ].toJSON();
}
transcript.messages.push(obj);
}
await saveTranscript(transcript)
await channel.delete();
break;
}
}
}
if (interaction.isUserSelectMenu() && interaction.guild && interaction.customId === 'verify_modal') {
const member = interaction.guild.members.cache.get(interaction.values[ 0 ])
const role = interaction.guild.roles.cache.get("757983851032215673")
if (member && role) {
if (member.roles.cache.has(role.id)) {
member.roles.remove(role, `Unverified by ${interaction.user.tag}`)
interaction.reply(`Successfully unverified <@${interaction.values[ 0 ]}>!`)
} else {
member.roles.add(role, `Verified by ${interaction.user.tag}`)
interaction.reply(`Successfully verified <@${interaction.values[ 0 ]}>!`)
}
} else {
interaction.reply(`An error occured while assigning the role to <@${interaction.values[ 0 ]}>`)
}
}
if (interaction.isModalSubmit()) {
try {
const ticket_user_reason = interaction.fields.getTextInputValue("ticket_reason");
const dbuser = await findUser(interaction.user.id);
const ticketname = `ticket-${interaction.user.id}`;
const fields = [
{
name: `Reason:`,
value: `> ${ticket_user_reason}`,
}
];
const embed = new EmbedBuilder()
.setColor("#5539cc")
.setTitle(`Ticket of ${interaction.user.username}`)
.addFields(fields);
if (dbuser) {
const login = await lastLogin(interaction.user.id) || [];
embed.addFields({
name: `User ID:`,
value: `> ${dbuser.toHexString()}`,
}, {
name: `Server URLs:`,
value: `> ${await getServerURLs(interaction.user.id)}`,
}, {
name: `Last Login:`,
value: `\`\`\`${JSON.stringify(login[ 0 ] ?? "none")}\`\`\``,
});
embed.setFooter({
text: login[ 1 ] ?? "No Login",
iconURL: interaction.user.displayAvatarURL(),
})
embed.setTimestamp(new Date(new Date().toLocaleString('en-US', { timeZone: login[ 2 ] ?? "UTC" })))
}
const btnrow = new ActionRowBuilder<ButtonBuilder>().addComponents([
new ButtonBuilder()
.setCustomId(`close_ticket`)
.setStyle(ButtonStyle.Danger)
.setLabel(`Close Ticket`),
]);
const possibleChannel = interaction.guild?.channels.cache.find(ch => ch.name === ticketname) as TextChannel;
if (possibleChannel) {
await possibleChannel.permissionOverwrites.create(interaction.user.id, {
"ViewChannel": true
});
await possibleChannel.send({
content: `${interaction.member} || <@&1120392307087261787>`,
embeds: [ embed ],
components: [ btnrow ],
});
await interaction.reply({
content: `> You already have a ticket here: ${possibleChannel}`,
ephemeral: true,
});
return;
}
const ch = await interaction.guild!.channels.create({
name: ticketname,
type: ChannelType.GuildText,
topic: `ticket of ${interaction.user.tag}`,
parent: "1081347349462405221",
});
setTimeout(() => {
ch.permissionOverwrites.create(interaction.user.id, {
"ViewChannel": true
});
}, 5000);
await ch.send({
content: `${interaction.member} || <@&1120392307087261787>`,
embeds: [ embed ],
components: [ btnrow ],
});
await interaction.reply({
content: `> Successfully created your ticket here: ${ch}`,
ephemeral: true,
});
} catch (e) {
await interaction.reply({
content: `> Error while creating your ticket. Please try again later.`,
ephemeral: true,
});
console.error(e);
}
}
if (!interaction.isChatInputCommand()) return
if (interaction.commandName === 'setup') {
// const channel = interaction.guild?.channels.cache.get("757992735171936347") as TextChannel
// const embed = new EmbedBuilder()
// .setTitle("Voicelocker")
// .setDescription("🔒 - Lock Voice Channel\n\n🔓 - Unlock Voice Channel")
// .setFooter({ text: "Provided by BBN", iconURL: "https://bbn.one/images/avatar.png" })
// .setColor('#f55a00')
// const builder = new ActionRowBuilder<ButtonBuilder>().addComponents([
// new ButtonBuilder()
// .setCustomId(`lock`)
// .setStyle(ButtonStyle.Success)
// .setEmoji("🔒")
// .setLabel(`Lock`),
// new ButtonBuilder()
// .setCustomId(`unlock`)
// .setStyle(ButtonStyle.Danger)
// .setEmoji("🔓")
// .setLabel(`Unlock`),
// ])
// channel.send({
// embeds: [ embed ],
// components: [ builder ]
// })
// interaction.reply("message sent!")
// code
const ticketChannel = interaction.guild!.channels.cache.get("1081337337704886392") as TextChannel;
if (!ticketChannel) return;
const embed = new EmbedBuilder()
.setColor("#f55a00")
.setTitle(`BBN - Ticket Support`)
.setDescription(`If you have a problem or question regarding BBN, create a ticket and we will get back to you as soon as possible.\ To create a ticket click the button below.`)
.setFooter({ text: "Provided by BBN", iconURL: "https://bbn.one/images/avatar.png" })
const btnrow = new ActionRowBuilder<ButtonBuilder>().addComponents([
new ButtonBuilder()
.setCustomId("create_ticket")
.setStyle(ButtonStyle.Success)
.setLabel("Create Ticket")
]);
await ticketChannel.send({
embeds: [ embed ],
components: [ btnrow ],
});
interaction.reply({
content: `Ticket System Setup in ${ticketChannel}`,
});
}
if (interaction.commandName === 'escalate') {
if (!Array.from((interaction.member?.roles as GuildMemberRoleManager).cache.keys()).some(role => supportRoles.includes(role))) {
interaction.reply("You do not have permission to escalate this ticket.");
return;
}
// check if ticket channel
if (!(interaction.channel?.type === ChannelType.GuildText && interaction.channel?.parent?.id === "1081347349462405221")) {
interaction.reply("This command can only be used in a ticket channel.");
return;
}
// move to escalation category
interaction.channel?.setParent("1120395441138315345", {
lockPermissions: false,
reason: "Ticket escalated",
});
interaction.reply({
allowedMentions: { roles: [ '757969277063266407' ] },
content: "Ticket escalated. || <@&757969277063266407>"
});
}
if (interaction.commandName === 'deescalate') {
if (!Array.from((interaction.member?.roles as GuildMemberRoleManager).cache.keys()).some(role => supportRoles.includes(role))) {
interaction.reply("You do not have permission to deescalate this ticket.");
return;
}
// check if ticket channel
if (!(interaction.channel?.type === ChannelType.GuildText && interaction.channel?.parent?.id === "1120395441138315345")) {
interaction.reply("This command can only be used in a ticket channel.");
return;
}
// move to escalation category
interaction.channel?.setParent("1081347349462405221", {
lockPermissions: false,
reason: "Ticket deescalated",
});
interaction.reply({
allowedMentions: { roles: [ '1120392307087261787' ] },
content: "Ticket deescalated. || <@&1120392307087261787>"
});
}
if (interaction.commandName === 'verify') {
const verify_modal = new UserSelectMenuBuilder()
.setCustomId("verify_modal")
const row_username = new ActionRowBuilder<UserSelectMenuBuilder>().addComponents(verify_modal)
await interaction.reply({ content: 'Which user do you want to verify?', components: [ row_username ], ephemeral: true })
}
if (interaction.commandName == "daily") {
getLastDaily(interaction.user.id).then(async result => {
if (result !== null) {
const timeDiff = (Date.now() - result) / 3600000;
if (timeDiff < 24) {
return interaction.reply(`You have already claimed your daily reward. Please wait ${Math.ceil(24 - timeDiff)} hours before claiming again.`);
}
}
let reward = 10 + (Math.floor(Math.random() * 10));
if ((await interaction.guild!.members.fetch(interaction.user.id)).premiumSince || (await interaction.guild!.members.fetch(interaction.user.id)).roles.cache.has("1120392307087261787"))
reward *= 10;
const res = await addCoins(interaction.user.id, reward);
if (res === null) {
interaction.reply("We couldn't find your account. Please [log in via Discord here](<https://bbn.one/api/@bbn/auth/redirect/discord?goal=/hosting>)");
return;
}
await setLastDaily(interaction.user.id, Date.now());
interaction.reply(`You have received ${reward} coins as your daily reward!`);
});
}
if (interaction.commandName == "balance") {
const possibleUser = interaction.options.getMentionable("user", false) as User;
let { id } = interaction.user;
if (possibleUser) {
if (!interaction.memberPermissions?.has(PermissionsBitField.Flags.Administrator)) {
interaction.reply("You do not have permission to view other users' balances.");
return;
}
id = possibleUser.id;
}
await getCoins(id).then(result => {
if (result === null) {
interaction.reply("We couldn't find your account. Please [log in via Discord here](<https://bbn.one/api/@bbn/auth/redirect/discord?goal=/hosting>)");
} else {
interaction.reply(`You currently have ${result} coins.`);
}
});
}
if (interaction.commandName == "addcoins") {
if (!interaction.memberPermissions?.has(PermissionsBitField.Flags.Administrator)) {
interaction.reply("You do not have permission to add coins.");
return;
}
const user = interaction.options.getMentionable("user", true) as User;
const coins = interaction.options.getInteger("coins", true);
const res = await addCoins(user.id, coins);
if (res === null) {
interaction.reply("We couldn't find the account in our database");
return;
}
interaction.reply(`Added ${coins} coins to ${user.username}'s balance.`);
}
if (interaction.commandName == "removecoins") {
if (!interaction.memberPermissions?.has(PermissionsBitField.Flags.Administrator)) {
interaction.reply("You do not have permission to remove coins.");
return;
}
const user = interaction.options.getMentionable("user", true) as User;
const coins = interaction.options.getInteger("coins", true);
const res = await removeCoins(user.id, coins);
if (res === null) {
interaction.reply("We couldn't find the account in our database");
return;
}
interaction.reply(`Removed ${coins} coins from ${user.username}'s balance.`);
}
if (interaction.commandName == "addpartner") {
if (!interaction.memberPermissions?.has(PermissionsBitField.Flags.Administrator)) {
interaction.reply("You do not have permission to add partners.");
return;
}
const user = interaction.options.getMentionable("user", true) as User;
const dbmember = await findUser(user.id);
if (!dbmember) {
interaction.reply("We couldn't find an bbn account in our database");
return;
}
const cpu = interaction.options.getInteger("cpu", true);
const ram = interaction.options.getInteger("ram", true);
const storage = interaction.options.getInteger("storage", true);
const slots = interaction.options.getInteger("slots", true);
const invite = await interaction.guild?.invites.create(Deno.env.get("GETSTARTED_CHANNEL")!, {
maxAge: 0,
unique: true,
reason: "Partner invite",
});
if (!invite) {
interaction.reply("We couldn't create an invite for the partner");
return;
}
addPartner(dbmember, cpu, ram, storage, slots, invite.code);
interaction.reply(`Added ${user.username} as a partner.\n\nFollowing resources got added: \nCPU: ${cpu} \nMemory: ${ram} \nStorage: ${storage} \nSlots: ${slots} \nInvite code: https://discord.gg/${invite.code}`);
}
if (interaction.commandName == "removepartner") {
if (!interaction.memberPermissions?.has(PermissionsBitField.Flags.Administrator)) {
interaction.reply("You do not have permission to remove partners.");
return;
}
const user = interaction.options.getMentionable("user", true) as User;
const dbmember = await findUser(user.id);
if (!dbmember) {
interaction.reply("We couldn't find an bbn account in our database");
return;
}
removePartner(dbmember);
interaction.reply(`Removed ${user.username} as a partner.`);
}
if (interaction.commandName == "partners") {
if (!interaction.memberPermissions?.has(PermissionsBitField.Flags.Administrator)) {
interaction.reply("You do not have permission to list partners.");
return;
}
let out = "Owner - CPU, RAM, Storage, Slots, Invitecode, last invite, uses\n";
const partners = await getPartners();
await interaction.deferReply();
out += (await Promise.all(partners.map(async partner =>
`<@${await getMemberFromBBNId(partner.owner)}> \`${partner.owner}\` - \`${partner.cpu}\` \`${partner.memory}\` \`${partner.disk}\` \`${partner.slots}\` \`${partner.invite}\` <t:${Math.round(partner.lastinvite / 1000)}:R> \`${(await interaction.guild?.invites.fetch(partner.invite)!).uses}\``))).join("\n");
const embed = new EmbedBuilder()
.setTitle(`Partners`)
.setDescription(out)
interaction.editReply({ embeds: [ embed ] });
}
if (interaction.commandName == "servers") {
if (interaction.member) {
if (interaction.member.roles instanceof GuildMemberRoleManager) {
if (!Array.from(interaction.member.roles.cache.keys()).some(role => supportRoles.includes(role))) {
interaction.reply("You do not have permission to list servers.");
return;
}
} else {
if (!interaction.member.roles.some(role => supportRoles.includes(role))) {
interaction.reply("You do not have permission to list servers.");
return;
}
}
}
const possiblemember = interaction.options.getMentionable("user", true);
if (!possiblemember) {
interaction.reply("Please mention a user.");
return;
}
const member = possiblemember as GuildMember;
const servers = await getServerURLs(member.id);
if (servers === null) {
interaction.reply("No account found for this user.");
return;
}
if (servers.length === 0) {
interaction.reply("This user has no servers.");
return;
}
interaction.reply(`Servers of ${member.user.username}:\n${servers.map(server => `<${server}>`).join("\n")}`);
}
}
const supportRoles = [ "757969277063266407", "815298088184446987", "1120392307087261787" ] // Owner, Dev, Support
function lockVoice(interaction: ButtonInteraction, lock: boolean) {
const channel = (interaction.member as GuildMember).voice.channel as VoiceChannel
if (channel) {
channel!.setUserLimit(lock ? channel.members.size : 0)
interaction.reply({ content: lock ? "Successfully locked your voice channel!" : "Successfully unlocked your voice channel!", ephemeral: true })
} else
interaction.reply({ content: "You have to be in a voice channel!", ephemeral: true })
}