paginate ars

master
Kit Kasune 3 years ago
parent e5ee3fd28b
commit c661b573b8
  1. 65
      commands/misc/ar.js

@ -4,6 +4,7 @@ const AR = require('../../models/ar');
const GuildData = require('../../models/guild'); const GuildData = require('../../models/guild');
const ask = require('../../util/ask'); const ask = require('../../util/ask');
const {Pagination} = require('../../util/pagination');
module.exports = { module.exports = {
name: "ar", name: "ar",
@ -27,19 +28,57 @@ module.exports = {
function sortARs(tar) { function sortARs(tar) {
let t = tar.triggers; let t = tar.triggers;
let res = {};
res.paginate = t.length > 10;
let ar = tar.ars; let ar = tar.ars;
let s = ''; if (res.paginate) {
for (let i=0;i<t.length;i++) {s+=`\`${i+1}.\` ${t[i]}\n-> ${ar[i]}\n\n`;} let pages = [];
return s; 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<t.length;i++) {s+=`\`${i+1}.\` ${t[i]}\n-> ${ar[i]}\n\n`;}
res.s = s;
}
return res;
} }
function viewARs(string) { function viewARs(res, mode) {
return new Discord.MessageEmbed() return new Promise(async resolve => {
.setTitle("Auto-Responses in this Server") if (res.paginate) {
.setDescription(string) if (mode) {res.pagination.pages.forEach(page => page.addField(mode === 'edit' ? "Editing" : 'Deletion', `Please say the **number** of the AR you wish to ${mode}.`));}
.setColor('328ba8') let r = await res.pagination.start({endTime: 60000, user: message.author.id});
.setFooter("Luno", client.user.avatarURL()) return resolve(r);
.setTimestamp(); } 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())) { 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...");} 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); 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; let collected;
try {collected = await message.channel.awaitMessages(m => m.author.id === message.author.id, {errors: ['time'], time: 60001, max: 1});} 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!");} 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!");} 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); 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; let collected;
try {collected = await message.channel.awaitMessages(m => m.author.id === message.author.id, {errors: ['time'], time: 60000, max: 1});} 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!");} 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())) { if (['v', 'view', 'l', 'list'].includes(args[0].toLowerCase())) {
let tar = await AR.findOne({gid: message.guild.id}); let tar = await AR.findOne({gid: message.guild.id});
if (!tar || !tar.triggers.length) {return message.channel.send("This server has no ARs!");} 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())) { if (['s', 'settings'].includes(args[0].toLowerCase())) {

Loading…
Cancel
Save