From edb9082bc238bbc741c6b64ca8f5cb9d0483ceb8 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Wed, 25 Aug 2021 10:06:31 -0600 Subject: [PATCH] paginate ars --- .vscode/discordjs.code-snippets | 35 ++++++++++++++++++ commands/misc/ar.js | 65 ++++++++++++++++++++++++++------- 2 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 .vscode/discordjs.code-snippets diff --git a/.vscode/discordjs.code-snippets b/.vscode/discordjs.code-snippets new file mode 100644 index 0000000..7e7a0fe --- /dev/null +++ b/.vscode/discordjs.code-snippets @@ -0,0 +1,35 @@ +{ + // Place your Natsuki workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and + // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope + // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is + // used to trigger the snippet and the body will be expanded and inserted. Possible variables are: + // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. + // Placeholders with the same ids are connected. + // Example: + "discord.js bot command": { + "scope": "javascript,typescript", + "prefix": "djscmd", + "body": [ + "const Discord = require('discord.js');${1:\nconst = require(\"\");\n}", + "module.exports = {", + " name: \"$2\",", + " aliases: [$3],", + " meta: {", + " category: '$4',", + " description: \"$5\",", + " syntax: '`$6`',", + " extra: ${7:null}${8:,\n guildOnly: true}", + " },", + " help: new Discord.MessageEmbed()", + " .setTitle(\"Help -> $9\")", + " .setDescription(\"$10\")${11:\n .addField(\"\", \"\")}", + " .addField(\"Syntax\", \"`$6`\"),", + " async execute(message, msg, args, cmd, prefix, mention, client) {", + " if (!args.length) {return message.channel.send(`Syntax: \\`\\${prefix}$6\\``);}", + " $0", + " }", + "};", + ], + "description": "Creates a new command" + } +} \ No newline at end of file diff --git a/commands/misc/ar.js b/commands/misc/ar.js index 9284b75..e85e6bd 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('c375f0') + .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('c375f0') - .setFooter("Natsuki", 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('c375f0') + .setFooter("Natsuki", 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({embeds: [embed]}); + return resolve(r); + } + }); } if (['a', 'add'].includes(args[0].toLowerCase())) { @@ -64,7 +103,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({embeds: [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({filter: 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!");} @@ -89,7 +128,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({embeds: [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({filter: 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!");} @@ -113,7 +152,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({embeds: [viewARs(sortARs(tar))]}); + return viewARs(sortARs(tar)); } if (['s', 'settings'].includes(args[0].toLowerCase())) {