This repository was archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (49 loc) · 1.57 KB
/
index.js
File metadata and controls
61 lines (49 loc) · 1.57 KB
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
require('dotenv').config();
const fs = require('fs');
const Discord = require('discord.js');
const client = new Discord.Client();
client.login(process.env.TOKEN);
const MARKETPLACE_CHANNEL_ID = '336895311081373707';
const MARKETPLACE_JSON_PATH = `${__dirname}/json/market.json`;
client.on('ready', async () => {
console.log(`connected as "${client.user.tag}"`);
try {
await updateMarketplaceJson(client);
await client.user.setActivity(':eyes:', { type: 'WATCHING' });
} catch(err) {
logError(err);
}
disconnect();
});
client.on('error', (err) => {
logError(err);
disconnect();
});
const updateMarketplaceJson = async (client) => {
console.log('updating market.json');
const mpChannel = await client.channels.fetch(MARKETPLACE_CHANNEL_ID);
const pinnedMessages = await mpChannel.messages.fetchPinned();
const output = pinnedMessages.map((pin) => {
return {
user: `${pin.author.username}#${pin.author.discriminator}`,
message: pin.content,
created: pin.createdTimestamp,
avatar_url: pin.author.avatarURL() ||
`https://cdn.discordapp.com/embed/avatars/${pin.author.discriminator % 5}.png`,
message_id: pin.id,
embeds: pin.embeds.map(embed => embed.url),
attachments: pin.attachments.map(attachment => attachment.url),
};
});
await fs.promises.writeFile(
MARKETPLACE_JSON_PATH,
JSON.stringify(output, null, 2)
);
};
const logError = (err) => {
console.error(`\nERROR ----\n${err.message}\n----------\n`);
}
const disconnect = () => {
console.log('done, disconnecting');
client.destroy();
}