Skip to content

Instantly share code, notes, and snippets.

@cjfswd
Last active May 13, 2026 22:24
Show Gist options
  • Select an option

  • Save cjfswd/39978b736aef59d791686fc062d8243f to your computer and use it in GitHub Desktop.

Select an option

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')
@devlopersabbir

Copy link
Copy Markdown

simple example,, thanks brother ๐Ÿ˜

@LEMMIIX

LEMMIIX commented Oct 28, 2025

Copy link
Copy Markdown

I've seen "guilds" being thrown around here and there. To be able to read messages via API, MUST the server be a guild?
Don't normal text channels on normal friends-servers work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment