|
1 | 1 | import { PrismaClient } from '@prisma/client'; |
2 | | -import { ChatInputCommandInteraction, EmbedBuilder } from 'discord.js'; |
| 2 | +import { APIEmbedField, ChatInputCommandInteraction, EmbedBuilder } from 'discord.js'; |
| 3 | +import { paginate } from '../../Utils/functions/paginator'; |
| 4 | +import { colors } from '../../Utils/functions/utils'; |
3 | 5 |
|
4 | 6 | module.exports = { |
5 | 7 | async execute(interaction: ChatInputCommandInteraction, database: PrismaClient) { |
6 | 8 | const serverOpt = interaction.options.getString('type'); |
7 | 9 |
|
8 | | - if (serverOpt == 'server') displayServers(); |
9 | | - else if (serverOpt == 'user') displayUsers(); |
| 10 | + const embeds: EmbedBuilder[] = []; |
| 11 | + let fields: APIEmbedField[] = []; |
10 | 12 |
|
11 | | - async function displayServers() { |
| 13 | + const LIMIT = 5; |
| 14 | + let counter = 0; |
| 15 | + |
| 16 | + |
| 17 | + // loop through all data |
| 18 | + // after counter hits limit (5) assign fields to an embed and push to to embeds array |
| 19 | + // reset counter & clear fields array |
| 20 | + // repeat until you reach the end |
| 21 | + |
| 22 | + if (serverOpt == 'server') { |
12 | 23 | const result = await database.blacklistedServers.findMany(); |
13 | 24 |
|
14 | | - const Embed = new EmbedBuilder() |
15 | | - .setColor('#0099ff') |
16 | | - .setAuthor({ |
17 | | - name: 'Blacklisted Servers:', |
18 | | - iconURL: interaction.client.user?.avatarURL()?.toString(), |
| 25 | + result.forEach((data, index) => { |
| 26 | + console.log(data); |
| 27 | + |
| 28 | + fields.push({ |
| 29 | + name: data.serverName, |
| 30 | + value: `${interaction.client.emoji.icons.id}: ${data.serverId}\nReason: ${data.reason}\n\n`, |
19 | 31 | }); |
20 | | - for (let i = 0; i < result.length; i++) { |
21 | | - Embed.addFields([ |
22 | | - { |
23 | | - name: result[i].serverName, |
24 | | - value: `${interaction.client.emoji.icons.id}: ${result[i].serverId}\nReason: ${result[i].reason}\n\n`, |
25 | | - }, |
26 | | - ]); |
27 | | - } |
28 | | - interaction.reply({ embeds: [Embed] }); |
29 | | - } |
30 | 32 |
|
| 33 | + counter++; |
| 34 | + if (counter >= LIMIT || index === result.length - 1) { |
| 35 | + embeds.push(new EmbedBuilder() |
| 36 | + .setFields(fields) |
| 37 | + .setColor('#0099ff') |
| 38 | + .setAuthor({ |
| 39 | + name: 'Blacklisted Servers:', |
| 40 | + iconURL: interaction.client.user?.avatarURL()?.toString(), |
| 41 | + })); |
31 | 42 |
|
32 | | - async function displayUsers() { |
| 43 | + counter = 0; |
| 44 | + fields = []; |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
| 48 | + else if (serverOpt == 'user') { |
33 | 49 | const result = await database.blacklistedUsers.findMany(); |
34 | 50 |
|
35 | | - const Embed = new EmbedBuilder() |
36 | | - .setColor('#0099ff') |
37 | | - .setAuthor({ |
38 | | - name: 'Blacklisted Users:', |
39 | | - iconURL: interaction.client.user?.avatarURL()?.toString(), |
| 51 | + result.forEach((data, index) => { |
| 52 | + fields.push({ |
| 53 | + name: data.username, |
| 54 | + value: `${interaction.client.emoji.icons.id}: ${data.userId}\nReason: ${data.reason}`, |
40 | 55 | }); |
41 | 56 |
|
42 | | - for (let i = 0; i < result.length; i++) { |
43 | | - Embed.addFields([ |
44 | | - { |
45 | | - name: result[i].username, |
46 | | - value: `${interaction.client.emoji.icons.id}: ${result[i].userId}\nReason: ${result[i].reason}\n\n`, |
47 | | - }, |
48 | | - ]); |
49 | | - } |
50 | | - interaction.reply({ embeds: [Embed] }); |
| 57 | + counter++; |
| 58 | + if (counter >= LIMIT || index === result.length - 1) { |
| 59 | + embeds.push(new EmbedBuilder() |
| 60 | + .setFields(fields) |
| 61 | + .setColor(colors('chatbot')) |
| 62 | + .setAuthor({ |
| 63 | + name: 'Blacklisted Users:', |
| 64 | + iconURL: interaction.client.user?.avatarURL()?.toString(), |
| 65 | + })); |
| 66 | + |
| 67 | + counter = 0; |
| 68 | + fields = []; |
| 69 | + } |
| 70 | + }); |
51 | 71 | } |
| 72 | + |
| 73 | + paginate(interaction, embeds); |
| 74 | + |
52 | 75 | }, |
53 | 76 | }; |
0 commit comments