-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
121 lines (111 loc) · 3.85 KB
/
index.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
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
import { serve } from "https://deno.land/std@0.119.0/http/server.ts";
const DISCORD_WEBHOOK = Deno.env.get("DISCORD_WEBHOOK");
if (DISCORD_WEBHOOK === undefined) {
throw new Error("You need to set DISCORD_WEBHOOK environment variable to Webhook URL.");
}
const KOFI_TOKEN = Deno.env.get("KOFI_TOKEN");
if (KOFI_TOKEN === undefined) {
throw new Error("You need to set the KOFI_TOKEN environment variable to your Ko-fi webhook verification token.");
}
async function callWebhook(data: Record<string, any>) {
await fetch(DISCORD_WEBHOOK!, {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify(Object.assign({
username: "Ko-fi",
avatar_url:
"https://cdn.discordapp.com/avatars/836232765942923305/1f6fbc8561728c4de6f0f68a2a943dd6.png",
}, data)),
});
}
type KofiEventType = "Donation" | "Subscription" | "Commission" | "Shop Order";
interface KofiShopItem {
direct_link_code: string;
}
interface KofiEvent {
timestamp: string;
type: KofiEventType;
is_public: boolean;
from_name: string;
message: string;
amount: string;
url: string;
email: string;
currency: string;
is_subscription_payment: boolean;
is_first_subscription_payment: boolean;
kofi_transaction_id: string;
verification_token: string;
shop_items: KofiShopItem[] | null;
tier_name: string | null;
}
console.log("Listening on http://localhost:8000");
serve(async (req) => {
const { pathname: path } = new URL(req.url);
switch (path) {
case "/": {
return new Response("https://github.com/Adivise/Ko-Fi");
}
case "/webhook": {
try {
const form = await req.formData();
const data: KofiEvent = JSON.parse(form.get("data")!.toString());
if (data.verification_token !== KOFI_TOKEN) {
console.log(`[INFO] Someone made unauthorized request! Verification Token: ${data}`);
return new Response("Unauthorized");
}
await callWebhook({ embeds: [{
color: 0xF7EEE0,
author: { name: `${data.is_public ? "" : "(Private) "}${data.from_name}` },
title: data.type === "Donation" ? "Someone bought you a coffee!"
: data.type === "Commission" ? "You got a commission!"
: data.type === "Subscription" ? (data.is_first_subscription_payment ? "Someone subscribed to your Ko-fi!" : "Subscription Payment")
: data.type === "Shop Order" ? "Someone made an order!"
: "Unknown Event",
url: data.url,
fields: [
...(data.message ? [{
name: "Message",
value: data.message,
}] : []),
{
name: "Amount",
value: data.amount,
inline: true,
},
{
name: "Currency",
value: data.currency,
inline: true,
},
...(data.shop_items ? [{
name: "Shop Items",
value: `${data.shop_items?.length} item${data.shop_items.length === 1 ? "" : "s"}`,
inline: true,
}] : []),
...(data.tier_name ? [{
name: "Tier",
value: data.tier_name,
inline: true,
}] : []),
],
timestamp: data.timestamp,
footer: {
text: data.kofi_transaction_id,
},
},
],
});
console.log("[INFO] Delivered hook!");
return new Response("Delivered!");
} catch (e) {
return new Response("400 Bad Request", { status: 400 });
}
}
default: {
return new Response("404 Not Found", { status: 404 });
}
}
});