From c661b573b868d9fddf21c1fcec740826f835c577 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Wed, 1 Sep 2021 21:15:45 -0600 Subject: [PATCH] paginate ars --- commands/misc/ar.js | 65 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 13 deletions(-) diff --git a/commands/misc/ar.js b/commands/misc/ar.js index c0beebd..f9630f7 100644 --- a/commands/misc/ar.js +++ b/commands/misc/ar.js @@ -4,6 +4,7 @@ const AR = require('../../models/ar'); const GuildData = require('../../models/guild'); const ask = require('../../util/ask'); +const {Pagination} = require('../../util/pagination'); module.exports = { name: "ar", @@ -27,19 +28,57 @@ module.exports = { function sortARs(tar) { let t = tar.triggers; + let res = {}; + res.paginate = t.length > 10; let ar = tar.ars; - let s = ''; - for (let i=0;i ${ar[i]}\n\n`;} - return s; + if (res.paginate) { + let pages = []; + let cond = false; + let x = 0; + while (true) { + let s = ''; + for (let i = 0; i < 10; i++) { + if (ar[(x * 10) + i] === undefined) {cond = true; break;} + s += `\`${(x*10)+i+1}.\` ${t[(x * 10) + i]}\n-> ${ar[(x * 10) + i]}\n\n`; + if ((x * 10) + i >= ar.length) {cond = true; break;} + } + pages.push(new Discord.MessageEmbed() + .setTitle(`Auto-Responses in this Server`) + .setDescription(s) + .setColor('328ba8') + .setTimestamp() + ); + if (cond) {break;} + x++; + } + res.pagination = new Pagination(message.channel, pages, message, client, true); + } else { + let s = ''; + for (let i=0;i ${ar[i]}\n\n`;} + res.s = s; + } + return res; } - function viewARs(string) { - return new Discord.MessageEmbed() - .setTitle("Auto-Responses in this Server") - .setDescription(string) - .setColor('328ba8') - .setFooter("Luno", client.user.avatarURL()) - .setTimestamp(); + function viewARs(res, mode) { + return new Promise(async resolve => { + if (res.paginate) { + if (mode) {res.pagination.pages.forEach(page => page.addField(mode === 'edit' ? "Editing" : 'Deletion', `Please say the **number** of the AR you wish to ${mode}.`));} + let r = await res.pagination.start({endTime: 60000, user: message.author.id}); + return resolve(r); + } else { + let string = res.s; + let embed = new Discord.MessageEmbed() + .setTitle("Auto-Responses in this Server") + .setDescription(string) + .setColor('328ba8') + .setFooter("Luno", client.user.avatarURL()) + .setTimestamp(); + if (mode) {embed.addField(mode === 'edit' ? "Editing" : 'Deletion', `Please say the **number** of the AR you wish to ${mode}.`);} + let r = await message.channel.send(embed); + return resolve(r); + } + }); } if (['a', 'add'].includes(args[0].toLowerCase())) { @@ -63,7 +102,7 @@ module.exports = { if (!tar || !tar.triggers.length) {return message.channel.send("You can't edit any auto-responses... because there aren't any here...");} let sar = sortARs(tar); - await message.channel.send(viewARs(sar).addField("Editing", "Please say the **number** of the AR you wish to edit.")); + await viewARs(sar, 'edit'); let collected; try {collected = await message.channel.awaitMessages(m => m.author.id === message.author.id, {errors: ['time'], time: 60001, max: 1});} catch {return message.channel.send("This question has timed out. Please try again!");} @@ -88,7 +127,7 @@ module.exports = { if (!tar || !tar.triggers.length) {return message.channel.send("It's not like this server has any ARs for me to delete in the first place!");} let sar = sortARs(tar); - await message.channel.send(viewARs(sar).addField("Deletion", "Please say the **number** of the AR you wish to delete.")); + await viewARs(sar, 'delete'); let collected; try {collected = await message.channel.awaitMessages(m => m.author.id === message.author.id, {errors: ['time'], time: 60000, max: 1});} catch {return message.channel.send("This question has timed out. Please try again!");} @@ -112,7 +151,7 @@ module.exports = { if (['v', 'view', 'l', 'list'].includes(args[0].toLowerCase())) { let tar = await AR.findOne({gid: message.guild.id}); if (!tar || !tar.triggers.length) {return message.channel.send("This server has no ARs!");} - return message.channel.send(viewARs(sortARs(tar))); + return await viewARs(sortARs(tar)); } if (['s', 'settings'].includes(args[0].toLowerCase())) {