-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
126 lines (106 loc) · 3.93 KB
/
main.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
const fs = require('fs');
const discord = require('discord.js');
const DISCORD_MAX_DESCRIPTION_LENGTH = 4096;
const DISCORD_GUILD = '238666723824238602';
const DISCORD_CHANNEL = '308772291863642112';
const ORDERED_LIST_REGEX = /^[0-9]+\.(.*)/;
const DISCORD_TOKEN = process.env.DISCORD_TOKEN;
const PATH_TO_MARKDOWN = (process.env.GITHUB_WORKSPACE || '.') + '/README.md';
const getMarkdownText = () => fs.readFileSync(PATH_TO_MARKDOWN, 'utf8');
const parseMarkdownToSegments = text => {
const lines = text.split('\n')
.map(line => line.trim())
.filter(Boolean);
const segments = [];
let i = 0;
let a = '';
for (const line of lines) {
if (line.startsWith('##')) {
const title = line.split('##')[1].trim();
segments.push([title]);
continue;
}
const match = ORDERED_LIST_REGEX.exec(line);
const segmentIndex = segments.length - 1
if (match) {
const el = match[1].trim();
segments[segmentIndex].push({ bullet: false, text: el });
} else if (line.startsWith('-')) {
const el = line.split('-').slice(1).join('-').trim();
segments[segmentIndex].push({ bullet: true, text: el });
} else {
segments[segmentIndex][segments[segmentIndex].length - 1].text += ` ${line}`;
}
}
segments.map((segment) => segment.map((line) => line.text ?
line.text.replace('<br>', '\n') :
line.replace('<br>', '\n')));
return segments;
}
const segmentsToEmbeds = (segments) => {
const embedTexts = [];
for (const segment of segments) {
const [title, ...lines] = segment;
const parts = [''];
let size = 0;
let i = 0;
for (const line of lines) {
const text = line.text;
const isBullet = line.bullet;
let actualText;
if (!isBullet) {
i++;
actualText = `**${i}.** ${text}`;
} else {
actualText = `• ${text}`;
}
if ((size + actualText.length + 1) >= DISCORD_MAX_DESCRIPTION_LENGTH) {
parts.push(actualText);
size = actualText.length;
} else {
parts[parts.length - 1] += `\n\n${actualText}`;
size += actualText.length + 1;
}
}
embedTexts.push(parts.map(part => part.trim().replace('<br>', '')));
}
return embedTexts;
}
const client = new discord.Client();
client.on('ready', async () => {
const guild = client.guilds.cache.get(DISCORD_GUILD);
if (!guild) {
process.exit(1);
}
const channel = guild.channels.cache.get(DISCORD_CHANNEL);
if (!channel) {
process.exit(1);
}
const markdownText = getMarkdownText();
const segments = parseMarkdownToSegments(markdownText);
const embedTexts = segmentsToEmbeds(segments);
const messagesToDelete = await channel.messages.fetch({ limit: 100 });
await Promise.all(messagesToDelete.mapValues((message => message.delete())));
let i = 0;
for (const segment of segments) {
const [title, ...rest] = segment;
for (let k = 0; k < embedTexts[i].length; k++) {
let embed = new discord.MessageEmbed();
if (k === 0) {
const mdFragment = title.toLowerCase().replace(/ /g, '-').replace('#', '');
embed.setAuthor(title, guild.iconURL, `https://progdisc.club/rules/#${mdFragment}`);
}
if (i === 0) {
embed.setAuthor(guild.name, guild.iconURL(), 'https://progdisc.club');
embed.setFooter('Updated on');
embed.setTimestamp(new Date());
}
embed.setColor('#4286F4');
embed.setDescription(embedTexts[i][k]);
await channel.send('', embed);
}
i++;
}
process.exit(0);
});
client.login(DISCORD_TOKEN);