modmail/structure/commands/Logs.js

72 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-06-19 20:05:32 +02:00
const Command = require('../Command');
class Logs extends Command {
constructor(client) {
super(client, {
name: 'logs',
2021-06-19 21:41:06 +02:00
aliases: ['mmlogs', 'mmhistory', 'mmlog'],
2021-06-19 20:05:32 +02:00
showUsage: true,
usage: '<user> [page]'
});
}
2021-06-19 22:41:51 +02:00
async execute(message, { args }) {
2021-06-19 20:05:32 +02:00
const user = await this.client.resolveUser(args[0]);
let pageNr = 1;
if (args[1]) {
const num = parseInt(args[1]);
if (isNaN(num)) return {
error: true,
msg: 'Invalid page number, must be number'
};
pageNr = num;
}
const { member, channel } = message;
const history = await this.client.modmail.loadHistory(user.id);
2021-06-19 22:21:29 +02:00
if (!history.length) return 'Not found in modmail DB';
2021-06-19 21:09:36 +02:00
const page = this.paginate([...history].filter((e) => !e.markread).reverse(), pageNr, 10);
2021-06-19 20:05:32 +02:00
const embed = {
author: {
name: `${user.tag} modmail history`,
// eslint-disable-next-line camelcase
icon_url: user.displayAvatarURL({ dynamic: true })
},
footer: {
text: `${user.id} | Page ${page.page}/${page.maxPage}`
},
fields: [],
color: member.highestRoleColor
};
for (const entry of page.items) {
const user = await this.client.resolveUser(entry.author);
embed.fields.push({
name: `${user.tag}${entry.anon ? ' (ANON)' : ''} @ ${new Date(entry.timestamp).toUTCString()}`,
value: entry.content.substring(0, 1000) + (entry.content.length > 1000 ? '...' : '')
});
}
await channel.send({ embed });
}
paginate(items, page = 1, pageLength = 10) {
const maxPage = Math.ceil(items.length / pageLength);
if (page < 1) page = 1;
if (page > maxPage) page = maxPage;
const startIndex = (page - 1) * pageLength;
return {
items: items.length > pageLength ? items.slice(startIndex, startIndex + pageLength) : items,
page,
maxPage,
pageLength
};
}
}
module.exports = Logs;