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
|
import { Client, Events, GatewayIntentBits } from 'discord.js';
import removeMd from 'remove-markdown';
import { writeFileSync, mkdirSync, rmSync } from 'fs';
import { marked } from 'marked';
import { Database } from "bun:sqlite";
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessageReactions] });
const db = new Database(process.env.DB_LOCATION, { create: true });
function parseMessage(content: string) {
const lines = content.split('\n');
const authorMatch = content.match(/\*from(?: the)? ([^*]+)\*/i);
const titleLine = lines.slice(0, 2).find(l => /^#+\s+.+|^\*\*.+\*\*|^.+\*\*[^*]+\*\*$/.test(l)) ?? null;
const author = authorMatch ? authorMatch[1].trim() : null;
const title = titleLine ? titleLine.replace(/^#+\s+/, '').replace(/\*\*/g, '').trim() : null;
const message = lines
.filter(l => l !== titleLine)
.join('\n')
.replace(/\*from(?: the)? [^*]+\*\n?/i, '')
.trimStart();
return { author, title, message };
}
client.on(Events.ClientReady, bot => {
console.log(`Logged in as ${bot.user.tag}!`);
client.user?.setStatus('invisible');
try {
db.run(`CREATE TABLE IF NOT EXISTS announcements (
id INTEGER PRIMARY KEY,
msg_id INTERGER,
title TEXT,
author TEXT,
message TEXT,
created_at INTEGER
)`);
db.run(`CREATE TABLE IF NOT EXISTS announcements_attachments (
id INTEGER PRIMARY KEY,
msg_id INTERGER,
file_name TEXT
)`);
db.run(`CREATE TABLE IF NOT EXISTS exclude_person (
id INTEGER PRIMARY KEY,
user_id INTERGER
)`);
db.run(`CREATE TABLE IF NOT EXISTS announcements_reactions (
id INTEGER PRIMARY KEY,
msg_id INTERGER,
user_id INTERGER,
reaction TEXT
)`);
} catch (e) {
console.error(e);
}
});
client.on(Events.MessageCreate, async (msg) => {
const query = db.prepare('SELECT * FROM exclude_person WHERE user_id = ?');
const result = query.get(msg.author.id);
if (result) return;
if (msg.author.bot) return;
if (msg.channel.id !== process.env.ANNOUNCEMENT_CHANNEL) return;
if (!(msg.content.startsWith('#') || msg.content.startsWith('*') || msg.content.match(/^:[a-z0-9_]+:/i) || msg.content.match(/^\p{Emoji}/u))) return;
const { title, author, message } = parseMessage(msg.content);
if (!title || !author || !message) return;
try {
const insert = db.prepare(`INSERT INTO announcements (title, msg_id, author, message, created_at) VALUES (($title), ($msg_id), ($author), ($message), ($created_at))`);
insert.run({
$title: removeMd(title),
$msg_id: msg.id,
$author: removeMd(author),
$message: await marked(message),
$created_at: msg.createdTimestamp.toString()
})
if (msg.attachments.size > 0) {
try {
mkdirSync(`../public/images/${msg.id}`, { recursive: true });
for (const a of msg.attachments.values()) {
const response = await fetch(a.url);
const buffer = Buffer.from(await response.arrayBuffer());
writeFileSync(`../public/images/${msg.id}/${a.name}`, buffer);
const insert = db.prepare(`INSERT INTO announcements_attachments (msg_id, file_name) VALUES ($msg_id, $file_name)`);
insert.run({
$msg_id: msg.id,
$file_name: a.name
});
}
} catch (e) {
console.error(e);
return;
}
}
} catch (e) {
console.error(e);
}
});
client.on(Events.MessageUpdate, async (msg, msgnew) => {
if (!msg.author) return;
if (!msg.content) return;
if (msg.author.bot) return;
if (msg.channel.id !== process.env.ANNOUNCEMENT_CHANNEL) return;
if (!(msgnew.content.startsWith('#') || msgnew.content.startsWith('*') || msgnew.content.match(/^:[a-z0-9_]+:/i) || msgnew.content.match(/^\p{Emoji}/u))) return;
const { title, author, message } = parseMessage(msgnew.content);
if (!title || !author || !message) return;
try {
const removedAttachments = msg.attachments.filter(a => !msgnew.attachments.has(a.id));
for (const a of removedAttachments.values()) {
const deleteAttachment = db.prepare(`DELETE FROM announcements_attachments WHERE msg_id = ($msg_id) AND file_name = ($file_name)`);
deleteAttachment.run({
$msg_id: msg.id,
$file_name: a.name
});
}
} catch (e) {
console.error(e);
}
try {
const update = db.prepare(`UPDATE announcements SET title = ($title), author = ($author), message = ($message) WHERE msg_id = ($msg_id)`);
update.run({
$title: removeMd(title),
$author: removeMd(author),
$message: await marked(message),
$msg_id: msg.id
})
} catch (e) {
console.error(e);
}
});
client.on(Events.MessageDelete, async (msg) => {
if (!msg.author) return;
if (!msg.content) return;
if (msg.author.bot) return;
if (msg.author.id === client.user!.id) return;
if (msg.channel.id !== process.env.ANNOUNCEMENT_CHANNEL) return;
try {
const deleteAnnouncement = db.prepare(`DELETE FROM announcements WHERE msg_id = ($msg_id)`);
deleteAnnouncement.run({
$msg_id: msg.id
})
const deleteAnnouncementReactions = db.prepare(`DELETE FROM announcements_reactions WHERE msg_id = ($msg_id)`);
deleteAnnouncementReactions.run({
$msg_id: msg.id
})
if (msg.attachments.size > 0) {
const deleteAttachment = db.prepare(`DELETE FROM announcements_attachments WHERE msg_id = ($msg_id)`);
deleteAttachment.run({
$msg_id: msg.id
});
rmSync(`../public/images/${msg.id}`, { recursive: true });
}
} catch (e) {
console.error(e);
}
});
client.on(Events.MessageReactionAdd, async (reaction, user) => {
if (!reaction.message.author) return;
if (user.bot) return;
if (reaction.message.channel.id !== process.env.ANNOUNCEMENT_CHANNEL) return;
try {
const insert = db.prepare(`INSERT INTO announcements_reactions (msg_id, user_id, reaction) VALUES (($msg_id), ($user_id), ($reaction))`);
insert.run({
$msg_id: reaction.message.id,
$user_id: user.id,
$reaction: reaction.emoji.toString()
})
} catch (e) {
console.error(e);
}
})
client.on(Events.MessageReactionRemove, async (reaction, user) => {
if (!reaction.message.author) return;
if (user.bot) return;
if (reaction.message.channel.id !== process.env.ANNOUNCEMENT_CHANNEL) return;
try {
const deleteReaction = db.prepare(`DELETE FROM announcements WHERE msg_id = ($msg_id) AND user_id = ($user_id) AND reaction = ($reaction)`);
deleteReaction.run({
$msg_id: reaction.message.id,
$user_id: user.id,
$reaction: reaction.emoji.toString()
})
} catch (e) {
console.error(e);
}
})
client.login(process.env.TOKEN);
|