Skip to content

Instantly share code, notes, and snippets.

@thinhbuzz
Last active January 23, 2024 08:13
Show Gist options
  • Save thinhbuzz/1940ab7ee7ccb0bb106b65aef2999d38 to your computer and use it in GitHub Desktop.
Save thinhbuzz/1940ab7ee7ccb0bb106b65aef2999d38 to your computer and use it in GitHub Desktop.
get discord message by date range
const serverId = '1012618482094968903';
const channelId = '1193845566204035092';
const authorization = 'token string';
const startTime = 1704672000000;
const endTime = 1705190400000;
function pick(obj, keys) {
const result = {};
keys.forEach((key) => {
result[key] = obj[key];
});
return result;
}
function uniqueArrayObject(arr, key = 'id') {
return arr.reduce((acc, current) => {
const x = acc.find((item) => item[key] === current[key]);
if (!x) {
return acc.concat([current]);
} else {
return acc;
}
}, []);
}
async function getMessages(before) {
const myHeaders = new Headers();
myHeaders.append('authorization', authorization);
const requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow',
};
if (before) {
before = `before=${before}`;
} else {
before = '';
}
const messages = await fetch(
`https://discord.com/api/v9/channels/${channelId}/messages?limit=100&${before}`,
requestOptions,
)
.then((response) => response.json())
.catch(() => [])
.then((_messages) => {
return _messages
.filter((message) => {
const messageTimestamp = new Date(message.timestamp).getTime();
return messageTimestamp >= startTime && messageTimestamp < endTime;
})
.map((message) => {
return {
author: pick(message.author, ['global_name', 'id', 'username']),
...pick(message, ['timestamp', 'id', 'reactions']),
};
});
});
if (messages.length) {
messages.push(...(await getMessages(messages[messages.length - 1].id)));
}
return messages;
}
async function getReactionsByReaction(messageId, reaction, after) {
const myHeaders = new Headers();
myHeaders.append('authorization', authorization);
const requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow',
};
if (after) {
after = `after=${after}`;
} else {
after = '';
}
const reactions = await fetch(
`https://discord.com/api/v9/channels/${channelId}/messages/${messageId}/reactions/${reaction}?limit=100&${after}`,
requestOptions,
)
.then((response) => response.json())
.catch(() => [])
.then((_reactions) => {
return _reactions.map((reaction) => pick(reaction, ['global_name', 'id', 'username']));
});
if (reactions.length) {
reactions.push(
...(await getReactionsByReaction(messageId, reaction, reactions[reactions.length - 1].id)),
);
}
return reactions;
}
async function getReactions(message) {
const reactions = (
await Promise.all(
(message.reactions || []).map((reaction) => {
const emoji = reaction.emoji;
return getReactionsByReaction(
message.id,
encodeURIComponent(emoji.id ? emoji.name + ':' + emoji.id : emoji.name),
'',
);
}),
)
).flat();
message.reactions = uniqueArrayObject(reactions);
message.reactionCount = message.reactions.length;
}
(async () => {
const messages = await getMessages();
for (const message of messages) {
message.link = `https://discord.com/channels/${serverId}/${channelId}/${message.id}`;
await getReactions(message);
}
// sort by reactionCount
messages.sort((a, b) => b.reactionCount - a.reactionCount);
console.log(JSON.stringify(messages));
})().catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment