-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
130 lines (115 loc) · 4.17 KB
/
mod.ts
File metadata and controls
130 lines (115 loc) · 4.17 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
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
import { rest, sendMessage } from "https://deno.land/x/discordeno@12.0.1/mod.ts";
import { json, serve } from "https://deno.land/x/sift@0.3.5/mod.ts";
import { NaticoEmbed } from "https://deno.land/x/natico@2.3.0-rc.1/src/util/Embed.ts";
import { GithubHooks } from "./types.ts";
rest.token = `Bot ${Deno.env.get("TOKEN")!}`;
const NAME = Deno.env.get("NAME") ?? "Aethor";
const ICON = Deno.env.get("ICON") ?? "https://cdn.discordapp.com/avatars/870383692403593226/910b4e1f4e19d745dd080930044f8afc.png?size=2048";
const channels = Deno.env.get("CHANNELS")
? Deno.env
.get("CHANNELS")!
.split("|")
?.map((x) => BigInt(x))
: [818158216156413973n, 871713062120484885n];
const parseJson = (text: string) => {
try {
return JSON.parse(text);
} catch (_) {
return undefined;
}
};
const templateEmbed = () => new NaticoEmbed().setFooter(NAME, ICON).setTimestamp();
const makeIssueEmbed = (c: GithubHooks) => {
const embed = templateEmbed()
.setAuthor(c.sender.login, c.sender.avatar_url, c.sender.html_url)
.setTitle(`[${c.repository.full_name}] Issue opened #${c.issue?.number} ${c.issue?.title}`, c.issue?.html_url)
.setDescription(`${c.issue?.body}`);
if (c.comment) {
embed.setTitle(`[${c.repository.full_name}] New comment on issue #${c.issue?.number}: ${c.issue?.title}`, c.issue?.html_url).setDescription(c.comment.body);
}
if (c.action == "closed") {
embed.setTitle(`[${c.repository.full_name}] Issue closed #${c.issue?.number}: ${c.issue?.title}`, c.issue?.html_url);
embed.description = "";
}
if (c.action == "reopened") {
embed.setTitle(`[${c.repository.full_name}] Issue reopened #${c.issue?.number}: ${c.issue?.title}`, c.issue?.html_url);
embed.description = "";
}
return embed;
};
const makeCommitEmbed = (c: GithubHooks) => {
const embed = templateEmbed()
.setTitle(`[${c.repository.full_name}] ${c.commits?.length || 1} new commit`, c.compare)
.setDescription(`${c.commits?.map((x) => `[\`${x.id?.slice(0, 7)}\`](${x.url}) ${x.message} - ${x.author.name}`)?.join("\n")}`)
.setAuthor(c.sender.login, c.sender.avatar_url, c.sender.html_url);
return embed;
};
const makeForkEmbed = (c: GithubHooks) => {
const embed = templateEmbed()
.setTitle(`[${c.repository.full_name}] Fork created: ${c.forkee?.full_name}`, c.forkee?.html_url)
.setAuthor(c.forkee?.owner?.login as any, c.forkee?.owner.avatar_url, c.forkee?.owner.html_url);
return embed;
};
const makeStarEmbed = (c: GithubHooks) => {
const embed = templateEmbed()
.setTitle(`[${c.repository.full_name}] ${c.starred_at == null ? "Star removed" : "New star added"}`, c.repository.html_url)
.setAuthor(c.sender?.login, c.sender?.avatar_url, c.sender?.html_url)
.setFooter(`${NAME}, ${c.repository.stargazers_count == 1 ? "1 Star" : `${c.repository.stargazers_count} Stars`}`, ICON);
return embed;
};
const makeReleaseEmbed = (c: any) => {
const embed = templateEmbed()
.setTitle(`[${c.repository.full_name}] New release published: ${c.release?.name}`, c.release?.html_url)
.setAuthor(c.sender.login, c.sender.avatar_url, c.sender.html_url);
return embed;
};
//TODO: ADD RELEASE TYPES
const makeEmbed = (c: any) => {
return c.issue
? makeIssueEmbed(c)
: c.commits
? makeCommitEmbed(c)
: c.forkee
? makeForkEmbed(c)
: c.release
? makeReleaseEmbed(c)
: c.starred_at
? makeStarEmbed(c)
: undefined;
};
serve({
"/": async (req: Request) => {
if (Deno.env.get("WEBSITE_AUTH"))
if (req.headers.get("authorization") !== Deno.env.get("WEBSITE_AUTH"))
return json({
success: false,
message: "Theres nothing here",
});
const js: GithubHooks = parseJson(await req.text());
if (!js) {
return json({
success: false,
message: "Theres nothing here",
});
}
try {
const em = makeEmbed(js);
if (em) {
for (const channel of channels) {
await sendMessage(channel, {
embeds: [em],
});
}
}
return json({
success: "true",
});
} catch (e) {
console.error(e);
return json({
success: "false",
error: `${e}`,
});
}
},
});