From 98a19567448cf917f9a283ae65b36f5436e2f062 Mon Sep 17 00:00:00 2001 From: ShuriZma Date: Wed, 17 Apr 2024 19:13:03 +0200 Subject: [PATCH] add embed pagination for the library --- package.json | 1 + src/bot.ts | 122 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 97 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index a9a4351..a202591 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "author": "", "license": "ISC", "dependencies": { + "@devraelfreeze/discordjs-pagination": "^2.7.6", "discord.js": "^14.14.1", "sqlite3": "^5.1.7", "ts-node": "^10.9.2" diff --git a/src/bot.ts b/src/bot.ts index 9d15507..7ad8f3e 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -5,9 +5,12 @@ import { EmbedBuilder, Events, GatewayIntentBits, - Message, PermissionsBitField, REST, + Message, + PermissionsBitField, + REST, Routes, - User + User, + APIUser } from 'discord.js'; import * as sqlite3 from 'sqlite3'; import config from "./config"; @@ -17,6 +20,7 @@ import Reset from "./commands/reset"; import SetModRole from "./commands/setModRole"; import SetIgnorePrefix from "./commands/setIgnorePrefix"; import SetAutoEmbedMessages from "./commands/setAutoEmbedMessages"; +import { pagination, ButtonsTypes, ButtonsStyles } from "@devraelfreeze/discordjs-pagination"; export class Bot { private db: sqlite3.Database; @@ -142,40 +146,106 @@ export class Bot { public sendEmbed(msg: Message|ChatInputCommandInteraction) { this.db.all( - 'SELECT * FROM library ORDER BY amount DESC LIMIT 9', + 'SELECT * FROM library ORDER BY amount DESC', async (err, rows) => { if (rows) { - const icons = [ - ':first_place: • ', - ':second_place: • ', - ':third_place: • ', - ':four: • ', - ':five: • ', - ':six: • ', - ':seven: • ', - ':eight: • ', - ':nine: • ', + const emotes = [ + ':zero:', + ':one:', + ':two:', + ':three:', + ':four:', + ':five:', + ':six:', + ':seven:', + ':eight:', + ':nine:', ]; - const fields = []; + + const words = []; for (let index in rows) { - fields.push({ - name: icons[index] + rows[index]['word'], + let icon = ''; + let intIndex = Number.parseInt(index) + 1; + switch (intIndex) { + case 1: + icon = ':first_place:'; + break; + case 2: + icon = ':second_place:'; + break; + case 3: + icon = ':third_place:'; + break; + default: + const numbers = intIndex.toString().split(''); + for (let i = 0; i < numbers.length; i++) { + icon += emotes[numbers[i]]; + } + } + + icon += ' • '; + + words.push({ + name: icon + rows[index]['word'], value: rows[index]['amount'] + ' uses', inline: true, }); } - fields.push({ - name: '⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤', - value: '\u200B', - }); + let fields = []; + let embeds = []; + for (const word of words) { + fields.push(word); - const embed = new EmbedBuilder() - .setTitle('Top 9 most used words.') - .setDescription('This is the Library with the most used words and their amount.\u200B⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤') - .setColor(0xffe600) - .setFields(fields); - await msg.reply({embeds: [embed]}); + if (fields.length < 9) { + continue; + } + + fields.push({ + name: '⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤', + value: '\u200B', + }); + + const embed = new EmbedBuilder() + .setTitle(embeds.length > 0 ? 'Almighty Library' : 'Top 9 most used words.') + .setDescription('This is the Library with the most used words and their amount.\u200B⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤') + .setColor(0xffe600) + .setFields(fields); + + embeds.push(embed); + fields = []; + } + + if (fields.length > 0) { + fields.push({ + name: '⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤', + value: '\u200B', + }); + + const embed = new EmbedBuilder() + .setTitle('Almighty Library') + .setDescription('This is the Library with the most used words and their amount.\u200B⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤⏤') + .setColor(0xffe600) + .setFields(fields); + + embeds.push(embed); + } + + const user = msg.member.user; + if (!(user instanceof User)) { + console.log('devrael fucked up', user); + return; + } + + await pagination({ + embeds: embeds, + author: user, + interaction: msg instanceof Message ? null : msg, + message: msg instanceof Message ? msg : null, + fastSkip: true, + ephemeral: false, + pageTravel: true, + }); this.counter = 0; console.log('sendEmbed: sent library embed and set counter to 0');