Add pagination to !logs
parent
a60515bcc7
commit
3cf31f37d9
|
@ -3,47 +3,62 @@ const threads = require("../data/threads");
|
||||||
const moment = require('moment');
|
const moment = require('moment');
|
||||||
const utils = require("../utils");
|
const utils = require("../utils");
|
||||||
|
|
||||||
|
const LOG_LINES_PER_PAGE = 10;
|
||||||
|
|
||||||
module.exports = bot => {
|
module.exports = bot => {
|
||||||
const addInboxServerCommand = (...args) => threadUtils.addInboxServerCommand(bot, ...args);
|
const addInboxServerCommand = (...args) => threadUtils.addInboxServerCommand(bot, ...args);
|
||||||
|
|
||||||
addInboxServerCommand('logs', (msg, args, thread) => {
|
addInboxServerCommand('logs', async (msg, args, thread) => {
|
||||||
async function getLogs(userId) {
|
const firstArgUserId = utils.getUserMention(args[0]);
|
||||||
const userThreads = await threads.getClosedThreadsByUserId(userId);
|
let userId = firstArgUserId
|
||||||
|
? firstArgUserId
|
||||||
|
: thread && thread.user_id;
|
||||||
|
|
||||||
// Descending by date
|
if (! userId) return;
|
||||||
userThreads.sort((a, b) => {
|
|
||||||
if (a.created_at > b.created_at) return -1;
|
|
||||||
if (a.created_at < b.created_at) return 1;
|
|
||||||
return 0;
|
|
||||||
});
|
|
||||||
|
|
||||||
const threadLines = await Promise.all(userThreads.map(async thread => {
|
let userThreads = await threads.getClosedThreadsByUserId(userId);
|
||||||
const logUrl = await thread.getLogUrl();
|
|
||||||
const formattedDate = moment.utc(thread.created_at).format('MMM Do [at] HH:mm [UTC]');
|
|
||||||
return `\`${formattedDate}\`: <${logUrl}>`;
|
|
||||||
}));
|
|
||||||
|
|
||||||
const message = `**Log files for <@${userId}>:**\n${threadLines.join('\n')}`;
|
// Descending by date
|
||||||
|
userThreads.sort((a, b) => {
|
||||||
|
if (a.created_at > b.created_at) return -1;
|
||||||
|
if (a.created_at < b.created_at) return 1;
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
|
||||||
// Send the list of logs in chunks of 15 lines per message
|
// Pagination
|
||||||
const lines = message.split('\n');
|
const totalUserThreads = userThreads.length;
|
||||||
const chunks = utils.chunk(lines, 15);
|
const maxPage = Math.ceil(totalUserThreads / LOG_LINES_PER_PAGE);
|
||||||
|
const inputPage = firstArgUserId ? args[1] : args[0];
|
||||||
|
const page = Math.max(Math.min(inputPage ? parseInt(inputPage, 10) : 1, maxPage), 1); // Clamp page to 1-<max page>
|
||||||
|
const isPaginated = totalUserThreads > LOG_LINES_PER_PAGE;
|
||||||
|
const start = (page - 1) * LOG_LINES_PER_PAGE;
|
||||||
|
const end = page * LOG_LINES_PER_PAGE;
|
||||||
|
userThreads = userThreads.slice((page - 1) * LOG_LINES_PER_PAGE, page * LOG_LINES_PER_PAGE);
|
||||||
|
|
||||||
let root = Promise.resolve();
|
const threadLines = await Promise.all(userThreads.map(async thread => {
|
||||||
chunks.forEach(lines => {
|
const logUrl = await thread.getLogUrl();
|
||||||
root = root.then(() => msg.channel.createMessage(lines.join('\n')));
|
const formattedDate = moment.utc(thread.created_at).format('MMM Do [at] HH:mm [UTC]');
|
||||||
});
|
return `\`${formattedDate}\`: <${logUrl}>`;
|
||||||
|
}));
|
||||||
|
|
||||||
|
let message = isPaginated
|
||||||
|
? `**Log files for <@${userId}>** (page **${page}/${maxPage}**, showing logs **${start + 1}-${end}/${totalUserThreads}**):`
|
||||||
|
: `**Log files for <@${userId}>:**`;
|
||||||
|
|
||||||
|
message += `\n${threadLines.join('\n')}`;
|
||||||
|
|
||||||
|
if (isPaginated) {
|
||||||
|
message += `\nTo view more, add a page number to the end of the command`;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length > 0) {
|
// Send the list of logs in chunks of 15 lines per message
|
||||||
// User mention/id as argument
|
const lines = message.split('\n');
|
||||||
const userId = utils.getUserMention(args.join(' '));
|
const chunks = utils.chunk(lines, 15);
|
||||||
if (! userId) return;
|
|
||||||
getLogs(userId);
|
let root = Promise.resolve();
|
||||||
} else if (thread) {
|
chunks.forEach(lines => {
|
||||||
// Calling !logs without args in a modmail thread returns the logs of the user of that thread
|
root = root.then(() => msg.channel.createMessage(lines.join('\n')));
|
||||||
getLogs(thread.user_id);
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
addInboxServerCommand('loglink', async (msg, args, thread) => {
|
addInboxServerCommand('loglink', async (msg, args, thread) => {
|
||||||
|
|
Loading…
Reference in New Issue