Skip to content

Instantly share code, notes, and snippets.

@LA
Created December 27, 2020 02:11
Show Gist options
  • Save LA/62de16c3447f5d6b5bceee163bd29e66 to your computer and use it in GitHub Desktop.
Save LA/62de16c3447f5d6b5bceee163bd29e66 to your computer and use it in GitHub Desktop.
Discord Channel Transcript using REST API (NestJS)
async fetchMessages(channelId: string): Promise<string[]> {
let messages = [];
let beforeMessageId = '';
while (beforeMessageId !== null) {
const { data } = await this.httpService
.get(
`${API_URL}/channels/${channelId}/messages?limit=100${
beforeMessageId ? `&before=${beforeMessageId}` : ''
}`,
{
headers: {
authorization: this.discordBotAuthorization,
},
},
)
.toPromise();
data.reverse();
beforeMessageId = data.length && data.length === 100 ? data[0].id : null;
messages = [
...data.map(d => {
return `[${DateTime.fromISO(d.timestamp).toFormat(
'MM-dd-yyyy HH:mm',
)}] ${d.author.username}#${d.author.discriminator} <${
d.author.id
}>: ${d.content}`;
}),
...messages,
];
}
return messages;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment