-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
send_log.ts
50 lines (46 loc) · 1.26 KB
/
send_log.ts
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
import {
ActionRowBuilder,
DiscordAPIError,
EmbedBuilder,
Guild,
} from "discord.js";
import mongo from "./mongo";
const settings = mongo.db("bot").collection("settings");
export default async function (
url: string,
embed: EmbedBuilder,
guild: Guild,
settingName: string,
actionRows?: ActionRowBuilder[],
): Promise<void> {
const webhookIdArr = url.match(/\d{17,19}/);
if (!webhookIdArr) return;
const webhookData = await guild.client
.fetchWebhook(
webhookIdArr[0],
url.replace(
/https:\/\/discord\.com\/api\/?v?\d{0,2}?\/webhooks\/\d{16,19}\//,
"",
),
)
.catch((e: DiscordAPIError) => e);
if (webhookData instanceof DiscordAPIError) {
if (webhookData.status === 404) {
const $unset: any = {};
$unset[settingName] = "";
$unset[settingName.replace("Webhook", "")] = "";
await settings.updateOne({ guild: guild.id }, { $unset });
}
return;
}
const sendOpts: {
avatarURL?: string;
components?: ActionRowBuilder[];
embeds: EmbedBuilder[];
} = {
avatarURL: guild.client.user?.displayAvatarURL(),
embeds: [embed],
};
if (actionRows) sendOpts.components = actionRows;
await webhookData.send(sendOpts as { [k: string]: any }).catch(console.error);
}