-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
198 lines (173 loc) · 5.56 KB
/
main.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
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
// Run:
// deno run -A main.ts
//
import { discord } from "./deps.ts";
import { DiscordAPIClient, verify } from "./bot/discord/mod.ts";
import {
APP_BOARDD,
BOARDD_BOARD_MEMBER,
BOARDD_DISCORD_TAG,
BOARDD_FULL_NAME,
BOARDD_GITHUB_TAG,
BOARDD_LINKEDIN_TAG,
BOARDD_PICTURE,
} from "./bot/app/mod.ts";
import type { BoarddOptions } from "./boardd/mod.ts";
import { boardd } from "./boardd/mod.ts";
import * as env from "./env.ts";
const api = new DiscordAPIClient();
if (import.meta.main) {
await main();
}
export async function main() {
// Overwrite the Discord Application Command.
await api.registerCommand({
app: APP_BOARDD,
botID: env.DISCORD_CLIENT_ID,
botToken: env.DISCORD_TOKEN,
});
console.log(
"Invite Boardd to your server:",
`https://discord.com/api/oauth2/authorize?client_id=${env.DISCORD_CLIENT_ID}&scope=applications.commands`,
);
console.log(
"Discord application information:",
`https://discord.com/developers/applications/${env.DISCORD_CLIENT_ID}/bot`,
);
// Start the server.
Deno.serve({ port: env.PORT }, handle);
}
/**
* handle is the HTTP handler for the Boardd application command.
*/
export async function handle(request: Request): Promise<Response> {
const { error, body } = await verify(request, env.DISCORD_PUBLIC_KEY);
if (error !== null) {
return error;
}
// Parse the incoming request as JSON.
const interaction = await JSON.parse(body) as discord.APIInteraction;
switch (interaction.type) {
case discord.InteractionType.Ping: {
return Response.json({ type: discord.InteractionResponseType.Pong });
}
case discord.InteractionType.ApplicationCommand: {
if (
!discord.Utils.isChatInputApplicationCommandInteraction(interaction)
) {
return new Response("Invalid request", { status: 400 });
}
if (!interaction.member?.user) {
return new Response("Invalid request", { status: 400 });
}
if (
!interaction.member.roles
.some((role) => env.DISCORD_BOARD_ROLES.includes(role))
) {
return new Response("Invalid request", { status: 400 });
}
// Make the Boardd options.
const options = makeBoarddOptions(
interaction.member,
interaction.data,
env.DISCORD_ADMIN_ROLES,
);
// Invoke the Boardd operation.
boardd(options)
.then((result) =>
api.editOriginalInteractionResponse({
botID: env.DISCORD_CLIENT_ID,
botToken: env.DISCORD_TOKEN,
interactionToken: interaction.token,
content: result.number === undefined
? `Successfully updated [\`${result.ref}\`](https://acmcsuf.com/code/tree/${result.ref})!`
: `Successfully created <https://acmcsuf.com/pull/${result.number}>!`,
})
)
.catch((error) => {
if (error instanceof Error) {
api.editOriginalInteractionResponse({
botID: env.DISCORD_CLIENT_ID,
botToken: env.DISCORD_TOKEN,
interactionToken: interaction.token,
content: error.message,
});
}
console.error(error);
});
// Acknowledge the interaction.
return Response.json(
{
type:
discord.InteractionResponseType.DeferredChannelMessageWithSource,
data: {
flags: discord.MessageFlags.Ephemeral,
},
} satisfies discord.APIInteractionResponseDeferredChannelMessageWithSource,
);
}
default: {
return new Response("Invalid request", { status: 400 });
}
}
}
/**
* makeBoarddOptions makes the Boardd options from the Discord interaction.
*/
export function makeBoarddOptions(
member: discord.APIInteractionGuildMember,
data: discord.APIChatInputApplicationCommandInteractionData,
adminRoleIDs: string[],
): BoarddOptions {
const options: BoarddOptions = {
githubPAT: env.GITHUB_TOKEN,
actor: {
tag: member.user.username,
nick: member.nick ?? undefined,
isAdmin: member.roles
.some((role) => adminRoleIDs.includes(role)),
},
data: {},
};
const fullNameInput = data.options
?.find((option) => option.name === BOARDD_FULL_NAME);
if (fullNameInput?.type === discord.ApplicationCommandOptionType.String) {
options.data.fullName = fullNameInput.value;
}
const pictureURLInput = data.options
?.find((option) => option.name === BOARDD_PICTURE);
if (
pictureURLInput?.type === discord.ApplicationCommandOptionType.String
) {
options.data.pictureURL = pictureURLInput.value;
}
const githubTagInput = data.options
?.find((option) => option.name === BOARDD_GITHUB_TAG);
if (
githubTagInput?.type === discord.ApplicationCommandOptionType.String
) {
options.data.githubTag = githubTagInput.value;
}
const discordTagInput = data.options
?.find((option) => option.name === BOARDD_DISCORD_TAG);
if (
discordTagInput?.type === discord.ApplicationCommandOptionType.String
) {
options.data.discordTag = discordTagInput.value;
}
const linkedinTagInput = data.options
?.find((option) => option.name === BOARDD_LINKEDIN_TAG);
if (
linkedinTagInput?.type === discord.ApplicationCommandOptionType.String
) {
options.data.linkedinTag = linkedinTagInput.value;
}
const boardMemberTagInput = data.options
?.find((option) => option.name === BOARDD_BOARD_MEMBER);
if (
boardMemberTagInput?.type === discord.ApplicationCommandOptionType.User
) {
options.data.boardMemberTag = boardMemberTagInput.value;
}
return options;
}