Prefix command that converts the last messages sent in a channel into a file, then sends that file back to the channel.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
if (context.params.event.content.startsWith('!archive')) {
let messageCount = parseInt(context.params.event.content.split(' ')[1]);
if (!messageCount || messageCount < 1 || messageCount > 100) {
return await lib.discord.channels['@0.2.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `You must provide a number between 1 and 100 for the number of messages to archive.`,
});
}
let messages = await lib.discord.channels['@0.2.0'].messages.list({
channel_id: `${context.params.event.channel_id}`,
limit: messageCount,
});
let archiveContent = messages
.reverse()
.map((message) => {
let displayText = [
`User: ${message.author.username}#${message.author.discriminator}`,
`Timestamp: ${message.timestamp}`,
`Content: ${message.content}`,
];
if (message.embeds) {
displayText.push(`Embeds: ${JSON.stringify(message.embeds, null, 2)}`);
}
if (message.components) {
displayText.push(
`Components: ${JSON.stringify(message.components, null, 2)}`
);
}
return displayText.join('\n');
})
.join('\n' + '-'.repeat(64) + '\n');
await lib.discord.channels['@0.2.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `Here is an archive of the last ${messageCount} messages in <#${context.params.event.channel_id}>:`,
filename: `messages.txt`,
file: Buffer.from(archiveContent),
});
}