Skip to content

Instantly share code, notes, and snippets.

@cjfswd
Last active June 7, 2024 10:33
Show Gist options
  • Save cjfswd/39978b736aef59d791686fc062d8243f to your computer and use it in GitHub Desktop.
Save cjfswd/39978b736aef59d791686fc062d8243f to your computer and use it in GitHub Desktop.
discord rest api get all messages from channel
import fs from 'fs'
import fetch from 'cross-fetch';
import dotenv from 'dotenv';
dotenv.config();
const TOKEN = process.env.TOKEN || ''
const GUILD_URL = process.env.GUILD_URL || ''
class IGuild {
id: string
name: string
constructor(guild: IGuild) {
this.id = guild.id
this.name = guild.name
}
}
let guildChannels = await fetch(
`${GUILD_URL}/channels`,
{
headers: {
'authorization': TOKEN
}
})
.then(res => res.json())
.then((res: IGuild[]) => {
return res.map(item => { return new IGuild(item) })
.filter(res => !['Text Channels', 'Voice Channels', 'General'].includes(res.name))
})
let channelsMessages = await Promise.all(guildChannels.map(async item => {
const messageReq = await fetch(`https://discord.com/api/channels/${item.id}/messages`, {
headers: {
'authorization': TOKEN
}
}).then(res => res.json()).catch(err => console.log(JSON.stringify(err)))
return messageReq
}
))
let mergeChannelsAndMessages = guildChannels.map((item, index) => {
return {
//@ts-ignore
...item, images: [...[].concat.apply([], channelsMessages[index].map(item => item.attachments)).map(item => item.url)]
}
})
let finalObject = [...mergeChannelsAndMessages]
fs.writeFileSync('public/cache.json', JSON.stringify(finalObject), 'utf8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment