Skip to content

Instantly share code, notes, and snippets.

@cchudant
Last active April 4, 2018 19:07
Show Gist options
  • Save cchudant/ad58ba938ce0d4fd4c0cae781630f173 to your computer and use it in GitHub Desktop.
Save cchudant/ad58ba938ce0d4fd4c0cae781630f173 to your computer and use it in GitHub Desktop.
Archiver les messages discord d'un channel
const { Client } = require('discord.js')
const fs = require('fs')
const client = new Client()
client.on('ready', async () => {
console.log('bot ready')
const guild = client.guilds.get('297780867286433792')
//const channels = ['297868535076749323'].map(channel => guild.channels.get(channel))
const channels = guild.channels
const start = new Date().getTime()
await channels.filter(channel => channel.fetchMessages)
.reduce((acc, cur) => acc.then(() => fetchChannel(cur)).catch(console.error), Promise.resolve())
//might throw permission errors (ignored)
console.log('Took ' + (new Date().getTime() - start) + 'ms' )
console.log('ended')
await client.destroy()
process.exit(-1)
})
client.login(process.env.DISCORD_TOKEN)
async function fetchChannel(channel) {
console.log('STARTED FETCH FOR CHANNEL "' + channel.name + '" (' + channel.id + ')')
let messages = [];
let next = 1
let before = null
const start = new Date().getTime()
while (next) {
const res = await channel.fetchMessages({ limit: 100, before })
const fetched = Array.from(res.values())
if (!fetched.length) {
next = false
continue
}
messages = messages.concat(fetched.map(msg => ({
id: msg.id,
author: {
id: msg.author.id,
username: msg.author.username,
descriminator: msg.author.discriminator,
displayAvatarURL: msg.author.displayAvatarURL
},
content: msg.content,
cleanContent: msg.cleanContent,
createdAt: msg.createdAt,
pinned: msg.pinned,
type: msg.type,
attachments: Array.from(msg.attachments.values()).map(obj => delete obj.client && delete obj.message),
embeds: msg.embeds.map(obj => {
delete obj.client
delete obj.message
obj.author && delete obj.author.embed
obj.fields.forEach(field => delete field.embed)
obj.footer && delete obj.footer.embed
obj.image && delete obj.image.embed
obj.provider && delete obj.provider.embed
obj.thumbnail && delete obj.thumbnail.embed
obj.video && delete obj.video.embed
return obj
}),
mentions: {
channels: Array.from(msg.mentions.channels.values()).map(({ id, name }) => ({ id, name })),
everyone: msg.mentions.everyone,
users: Array.from(msg.mentions.users.values()).map(({ id, username, descriminator, displayAvatarURL }) => ({ id, username, descriminator, displayAvatarURL })),
roles: Array.from(msg.mentions.roles.values()).map(({ color, createdAt, id, name, mentionable, permissions, position }) => ({ color, createdAt, id, name, mentionable, permissions, position }))
},
webhookID: msg.webhookID,
tts: msg.tts,
editedAt: msg.editedAt
})))
console.log(messages.length + ' fetched')
next = fetched.length >= 100
before = fetched[fetched.length - 1].id
console.log((messages.length / ((new Date().getTime() - start) / 1000)).toFixed(3) + '/s')
}
console.log('FINISHED FETCH FOR CHANNEL "' + channel.name + '" (' + channel.id + ')')
const file = channel.name + '['+channel.id+'].json'
console.log('WRITING JSON IN ' + file)
fs.writeFileSync('out/' + file, JSON.stringify(messages, null, 2))
console.log('FINISHED WRITING JSON IN ' + file)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment