-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extract.js
156 lines (128 loc) · 11 KB
/
Extract.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// This code is responsible for extracting messages from the discord.
(()=>
{
// This code is responsible for extracting messages from the discord.
class DiscordAPI {
constructor(authToken) {
this.DISCORD_EPOCH = 1420070400000n;
this.setAuthorization(authToken);
}
setAuthorization(token)
{
this.authToken = token;
}
//Snowflake is a epoch-based way of calculating timestamps with more information
convertTimestampToSnowflake(timestamp) {
return ((BigInt(new Date(timestamp).getTime()) - this.DISCORD_EPOCH) << 22n).toString();
}
constructSearchParams(options) {
const params = new URLSearchParams();
if (options.author_id) params.append('author_id', options.author_id);
if (options.mentions) params.append('mentions', options.mentions);
if (options.has && options.has.length > 0) params.append('has', options.has.join(','));
if (options.pinned) params.append('pinned', 'true');
if (options.before) params.append('before', options.before);
if (options.after) params.append('after', options.after);
return params.toString();
}
async fetchMessages(apiUrl) {
try {
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Authorization': this.authToken,
'Content-Type': 'application/json'
}
});
return await response.json();
} catch (error) {
console.error('Error fetching messages:', error);
return [];
}
}
indexChannelElements() {
const channelContainer = document.querySelector('ul[aria-label="Channels"]');
if (!channelContainer) {
console.log("Channel container not found");
return {};
}
const channelElements = channelContainer.querySelectorAll('[data-dnd-name]');
const indexedChannels = {};
channelElements.forEach((element, index) => {
const channelName = element.getAttribute('data-dnd-name');
indexedChannels[channelName] = {
element,
index,
url: element.getAttribute('href') || element.querySelector('a')?.getAttribute('href') || element.querySelector('[href]')?.getAttribute('href')
};
});
console.log("Indexed Channels:", indexedChannels);
return indexedChannels;
}
async fetchChannelMessages(channelName, options) {
const channels = this.indexChannelElements();
const channel = channels[channelName];
if (channel) {
console.log(`Channel: ${channelName}, Index: ${channel.index}, URL: ${channel.url || 'N/A'}`);
const channelId = channel.url?.split('/').pop();
const allMessages = [];
let startTime, endTime;
if (channelId) {
options.limit = options.limit || 100;
// Start timing
startTime = performance.now();
while (options.limit > 0) {
const apiUrl = `https://discord.com/api/v9/channels/${channelId}/messages?limit=${Math.min(options.limit, 100)}&${this.constructSearchParams(options)}`;
console.log(`API URL: ${apiUrl}`);
const messages = await this.fetchMessages(apiUrl);
allMessages.push(...messages);
if (messages.length < 100) break;
const lastMessage = messages[messages.length - 1];
options.before = this.convertTimestampToSnowflake(lastMessage.timestamp);
options.limit -= messages.length;
}
// End timing
endTime = performance.now();
console.log(`Time taken: ${(endTime - startTime).toFixed(2)} milliseconds`);
console.log(`Fetched ${allMessages.length} messages`);
return allMessages;
} else {
console.error(`Channel "${channelName}" not found.`);
}
}
}
saveMessagesToFile(messages) {
// Reverse the order of the messages
const reversedMessages = messages.reverse().map(message => `${message.author.username}: ${message.content}`).join('\n');
// Create a Blob from the reversed messages
const blob = new Blob([reversedMessages], { type: 'text/plain' });
// Create a link element
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'discord_messages.txt';
// Append the link to the document and trigger the download
document.body.appendChild(a);
a.click();
// Remove the link from the document
document.body.removeChild(a);
console.log('Messages saved to discord_messages.txt in reversed order.');
}
}
// Example usage
const AUTHORIZATION_KEY="YOUR_DISCORD_TOKEN";
const discordAPI = new DiscordAPI(AUTHORIZATION_KEY);
const searchOptions = {
author_id: null,
mentions: null,
has: [],
pinned: false,
before: null,
after: null,
limit: 1000
};
discordAPI.fetchChannelMessages('announcements', searchOptions).then(messages => {
console.log(messages);
discordAPI.saveMessagesToFile(messages);
});
})();
//Extensions