modmail/structure/commands/MessageIds.js

55 lines
1.9 KiB
JavaScript
Raw Normal View History

const { MessageAttachment } = require('discord.js');
const Command = require('../Command');
class MessageIds extends Command {
constructor (client) {
super(client, {
name: 'messageids',
aliases: [ 'msgids', 'mmids' ]
});
}
async execute (message, { args }) {
let channel = null;
if (args?.length) channel = await this.client.resolveChannel(args[0]);
else ({ channel } = message);
const result = this.client.getUserFromChannel(channel);
if (result.error) return result;
const [ userId ] = result;
const user = await this.client.users.fetch(userId);
2022-03-23 21:46:42 +01:00
const dmChannel = await user.createDM();
const history = await this.client.cache.loadModmailHistory(userId);
const sorted = history.sort((a, b) => b.timestamp - a.timestamp);
const idContentPairs = [];
2022-03-23 21:48:43 +01:00
const urlBase = `https://discord.com/channels/@me`;
for (const mm of sorted) {
if (!mm.msgid && !mm.isReply) break; // Old modmails from before msg id logging -- could probably supplement with fetching messages but cba rn
2022-03-23 21:48:43 +01:00
idContentPairs.push(`${urlBase}/${dmChannel.id}/${mm.msgid} - ${mm.content}`);
}
if (!idContentPairs.length) {
2022-03-23 21:46:42 +01:00
const msgs = await dmChannel.messages.fetch();
const sortedMsgs = msgs.filter(msg => msg.author.id !== this.client.user.id).sort((a, b) => b.createdTimestamp - a.createdTimestamp);
2022-03-23 21:48:43 +01:00
for (const msg of sortedMsgs.values()) idContentPairs.push(`${urlBase}/${dmChannel.id}/${msg.id} - ${msg.content}`);
}
await message.channel.send({
files: [
new MessageAttachment(
Buffer.from(`ID - Content pairs for\n${user.tag} - ${user.id}\n\n${idContentPairs.join('\n')}`),
'ids.txt'
)
]
});
}
}
module.exports = MessageIds;