add embed pagination for the library

This commit is contained in:
ShuriZma 2024-04-17 19:13:03 +02:00
parent 285e6796d0
commit 98a1956744
Signed by: ShuriZma
GPG Key ID: 8D289758EE9B8074
2 changed files with 97 additions and 26 deletions

View File

@ -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"

View File

@ -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<boolean>|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');