Last active
October 28, 2025 11:42
-
-
Save cjfswd/39978b736aef59d791686fc062d8243f to your computer and use it in GitHub Desktop.
discord rest api get all messages from channel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
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?