-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeowulf-Code.js
298 lines (245 loc) · 9.3 KB
/
Beowulf-Code.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const { Client, GatewayIntentBits } = require('discord.js');
const { google } = require('googleapis');
const http = require('http');
const fs = require('fs').promises;
const { URLSearchParams } = require('url');
const moment = require('moment');
const credentialsPath = 'make a credential json file and put the path here';
const tokenPath = 'make a token json file and put the path here';
const folderIds = ['id1', 'id2']; //put ids from google drive, by choosing the file and then copying the id in the link
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
const oAuth2Client = new google.auth.OAuth2({ //replace these here with your own data
clientId: 'clientId',
clientSecret: 'clientSecret',
redirectUri: 'http://localhost:3000',
});
const drive = google.drive({ version: 'v3', auth: oAuth2Client });
const SCOPES = ['https://www.googleapis.com/auth/drive'];
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
const server = http.createServer(async (req, res) => {
if (req.url.startsWith('/auth/google/callback')) {
const urlParams = new URLSearchParams(req.url.split('?')[1]);
const code = urlParams.get('code');
try {
const { tokens } = await oAuth2Client.getToken(code);
oAuth2Client.setCredentials(tokens);
console.log('Tokens:', tokens);
await saveToken(tokens); // Await the saveToken function
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Authorization successful!');
} catch (error) {
console.error('Error exchanging code for tokens:', error);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Error exchanging code for tokens');
}
} else {
res.writeHead(302, { 'Location': authUrl });
res.end();
}
});
const PORT = 3000;
server.listen(PORT, async () => {
console.log(`Server running at http://localhost:${PORT}/`);
await loadToken(); // Await the loadToken function
});
async function loadToken() {
try {
const token = await fs.readFile(tokenPath);
oAuth2Client.setCredentials(JSON.parse(token));
console.log('Token loaded successfully.');
} catch (err) {
console.error('Error loading token:', err);
}
}
async function saveToken(tokens) {
try {
await fs.writeFile(tokenPath, JSON.stringify(tokens));
console.log('Token saved successfully.');
} catch (err) {
console.error('Error saving token:', err);
}
}
async function refreshAccessToken() {
try {
const { credentials } = await oAuth2Client.refreshAccessToken();
oAuth2Client.setCredentials(credentials);
console.log('Access token refreshed successfully.');
await saveToken(credentials);
} catch (error) {
console.error('Error refreshing access token:', error);
// No need to throw error here, just log it
}
}
const refreshTokenInterval = setInterval(async () => {
try {
if (oAuth2Client.isTokenExpiring()) {
await refreshAccessToken();
}
} catch (error) {
console.error('Error during token refresh check:', error);
}
}, 3600000);
process.on('SIGINT', () => {
clearInterval(refreshTokenInterval);
loadToken();
process.exit(0);
});
client.on('messageCreate', async (message) => {
if (message.content.startsWith('!fileinfo')) {
try {
if (oAuth2Client.isTokenExpiring()) { //refresh that token!
await refreshAccessToken();
}
const folderPromises = folderIds.map(async (folderId) => {
try {
const folderMetadata = await drive.files.get({
fileId: folderId,
fields: 'name',
});
const randomColor = Math.floor(Math.random()*16777215).toString(16);
const folderName = folderMetadata.data.name;
const { data } = await drive.files.list({
q: `'${folderId}' in parents`,
fields: 'files(id, name, createdTime)',
orderBy: 'createdTime desc',
pageSize: 1,
});
const latestFile = data.files[0];
const embed = {
color: parseInt(randomColor, 16), // Use the decimal representation of the color
title: 'File Information',
fields: [{ name: 'Folder Name', value: folderName }],
};
if (latestFile) {
const fileId = latestFile.id;
const createdTime = moment(latestFile.createdTime).format('YYYY-MM-DD HH:mm:ss');
const fileName = latestFile.name;
embed.fields.push({ name: 'New File Name', value: fileName, inline: true });
embed.fields.push({ name: 'Last Added Time', value: createdTime, inline: true });
} else {
embed.fields.push({ name: 'New File Name', value: 'No files in the folder' });
embed.fields.push({ name: 'Last Added Time', value: '-' });
}
return embed;
} catch (folderError) {
console.error(`Error fetching folder information for folder ID ${folderId}:`, folderError);
return `Error fetching folder information for folder ID: ${folderId}. Details: ${folderError.message}`;
}
});
const folderEmbeds = await Promise.all(folderPromises);
for (const embed of folderEmbeds) {
message.channel.send({ embeds: [embed] });
}
} catch (error) {
console.error('Top-level error:', error);
message.channel.send(`Error fetching folder information. Details: ${error.message}`);
}
}
else if (message.content.startsWith('!last')) {
try {
if (oAuth2Client.isTokenExpiring()) { //fix it!!!
await refreshAccessToken();
}
const latestFolderId = await findLatestFolderId(folderIds);
if (!latestFolderId) {
message.channel.send('No folders found.');
return;
}
const folderMetadata = await drive.files.get({
fileId: latestFolderId,
fields: 'name',
});
const randomColor = Math.floor(Math.random() * 16777215).toString(16);
const folderName = folderMetadata.data.name;
const { data } = await drive.files.list({
q: `'${latestFolderId}' in parents`,
fields: 'files(id, name, createdTime)',
orderBy: 'createdTime desc',
pageSize: 1,
});
const latestFile = data.files[0];
const embed = {
color: parseInt(randomColor, 16),
title: 'File Information',
fields: [{ name: 'Folder Name', value: folderName }],
};
if (latestFile) {
const fileId = latestFile.id;
const createdTime = moment(latestFile.createdTime).format('YYYY-MM-DD HH:mm:ss');
const fileName = latestFile.name;
embed.fields.push({ name: 'New File Name', value: fileName, inline: true });
embed.fields.push({ name: 'Last Added Time', value: createdTime, inline: true });
} else {
embed.fields.push({ name: 'New File Name', value: 'No files in the folder' });
embed.fields.push({ name: 'Last Added Time', value: '-' });
}
message.channel.send({ embeds: [embed] });
} catch (error) {
console.error('Top-level error:', error);
message.channel.send(`Error fetching folder information. Details: ${error.message}`);
}
}
else if (message.content === '!refreshtoken') {
try {
await refreshAccessToken();
message.channel.send('Woof! :3');
} catch (error) {
message.channel.send('Woof...? :Ε');
}
}
}
);
//Extra function that shows from all your files the last one edited. Interesting but will not use it for now.
/*async function findLatestFolderId() {
try {
const { data } = await drive.files.list({
q: `mimeType='application/vnd.google-apps.folder'`,
fields: 'files(id, name, modifiedTime)',
orderBy: 'modifiedTime desc',
pageSize: 1,
});
const latestFolder = data.files[0];
return latestFolder ? latestFolder.id : null;
} catch (error) {
console.error('Error finding the latest folder:', error);
return null;
}
}*/
async function findLatestFolderId(folderIds) {
let latestFolderId = null;
let latestModifiedTime = 0;
for (const folderId of folderIds) {
try {
console.log(`Fetching information for folder ID: ${folderId}`);
const { data } = await drive.files.list({
q: `'${folderId}' in parents`,
fields: 'files(modifiedTime)',
orderBy: 'modifiedTime desc',
pageSize: 1,
});
console.log(`API response for folder ID ${folderId}:`, data);
const files = data.files;
if (files && files.length > 0) {
const latestFile = files[0];
if (new Date(latestFile.modifiedTime) > new Date(latestModifiedTime)) {
latestModifiedTime = latestFile.modifiedTime;
latestFolderId = folderId;
}
}
} catch (error) {
console.error(`Error fetching folder information for folder ID ${folderId}:`, error);
}
}
console.log('Final result - Latest folder ID:', latestFolderId);
return latestFolderId;
}
client.login('BOT_TOKEN'); // Replace with your actual bot token