-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteraction.ts
146 lines (126 loc) · 6.53 KB
/
interaction.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
import "https://deno.land/std@0.182.0/dotenv/load.ts";
import { sign } from "https://cdn.skypack.dev/tweetnacl@v1.0.3?dts";
import { json, serve, validateRequest } from "https://deno.land/x/sift@0.6.0/mod.ts";
import { APIInteraction, InteractionType, APIChatInputApplicationCommandInteractionData, InteractionResponseType, RESTPatchAPIGuildRolePositionsJSONBody, ChannelType, OverwriteType, APIInteractionResponseChannelMessageWithSource } from "https://raw.githubusercontent.com/discordjs/discord-api-types/main/deno/v10.ts";
import { APIInteractionResponse } from "https://raw.githubusercontent.com/discordjs/discord-api-types/main/deno/payloads/v10/mod.ts";
import { makeAuthenticatedRequest } from "./utils.ts";
import { APIApplicationCommandInteractionDataStringOption } from "https://raw.githubusercontent.com/discordjs/discord-api-types/main/deno/payloads/v10/_interactions/applicationCommands.ts";
import { RESTPostAPIGuildChannelJSONBody, RESTPostAPIGuildRoleJSONBody } from "https://raw.githubusercontent.com/discordjs/discord-api-types/main/deno/rest/v10/guild.ts";
// https://deno.com/deploy/docs/tutorial-discord-slash
const publicKey = Deno.env.get("PUBLIC_KEY");
if (!publicKey) {
throw new Error("PUBLIC_KEY environment variable must be set");
}
const rolePositionStr = Deno.env.get("ROLE_POSITION");
if (!rolePositionStr) {
throw new Error("ROLE_POSITION environment variable must be set");
}
const rolePosition = parseInt(rolePositionStr);
if (isNaN(rolePosition)) {
throw new Error("ROLE_POSITION environment variable must be an integer");
}
export function hexToUint8Array(hex: string) {
return new Uint8Array(hex.match(/.{1,2}/g)!.map((val) => parseInt(val, 16)));
}
export function generateErrorMessage(title: string, text: string): APIInteractionResponseChannelMessageWithSource {
return {
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: `An error occurred while ${title}`,
embeds: [
{
title: "Error",
description: text,
color: 0xff0000,
},
],
},
};
}
serve({
"/interaction": async request => {
const { error } = await validateRequest(request, {
POST: {
headers: ["X-Signature-Ed25519", "X-Signature-Timestamp"],
},
});
if (error) {
return json({ error: error.message }, { status: error.status });
}
const body = await request.text();
if (!sign.detached.verify(
new TextEncoder().encode(request.headers.get("X-Signature-Timestamp")! + body),
hexToUint8Array(request.headers.get("X-Signature-Ed25519")!),
hexToUint8Array(publicKey),
)) {
return json({ error: "Invalid request signature" }, { status: 401 });
}
const interaction = JSON.parse(body) as APIInteraction;
if (interaction.type === InteractionType.Ping) {
return json({ type: 1 });
}
if (interaction.type === InteractionType.ApplicationCommand) {
const data = interaction.data as APIChatInputApplicationCommandInteractionData;
if (data.name === "hello") {
const payload: APIInteractionResponse = {
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Hello, world!",
},
};
return json(payload);
} else if (data.name === "newctf") {
const rolename = (data.options!.find(option => option.name === "rolename") as APIApplicationCommandInteractionDataStringOption | undefined)?.value;
const channelname = (data.options!.find(option => option.name === "channelname") as APIApplicationCommandInteractionDataStringOption | undefined)!.value;
const rolePayload: RESTPostAPIGuildRoleJSONBody = {
name: rolename ?? channelname,
hoist: true,
mentionable: true,
};
const roleResponse = await makeAuthenticatedRequest(`/guilds/${interaction.guild_id}/roles`, "POST", rolePayload);
if (roleResponse.status >= 400) {
return json(generateErrorMessage("creating role", await roleResponse.text()));
}
const { id } = await roleResponse.json();
const positionPayload: RESTPatchAPIGuildRolePositionsJSONBody = [{ id, position: rolePosition }];
const positionResponse = await makeAuthenticatedRequest(`/guilds/${interaction.guild_id}/roles`, "PATCH", positionPayload);
if (positionResponse.status >= 400) {
return json(generateErrorMessage("updating role position", await positionResponse.text()));
}
const channelPayload: RESTPostAPIGuildChannelJSONBody = {
name: channelname,
type: ChannelType.GuildText,
parent_id: Deno.env.get("CATEGORY_ID"),
permission_overwrites: [
{
id: interaction.guild_id!,
type: OverwriteType.Role,
allow: "0",
deny: "1024",
},
{
id,
type: OverwriteType.Role,
allow: "1024",
deny: "0",
},
],
};
const channelResponse = await makeAuthenticatedRequest(`/guilds/${interaction.guild_id}/channels`, "POST", channelPayload);
if (channelResponse.status >= 400) {
return json(generateErrorMessage("creating channel", await channelResponse.text()));
}
const payload: APIInteractionResponse = {
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: `Created role ${rolename ?? channelname} and channel ${channelname} (note that voice channels were not updated)`,
},
};
return json(payload);
} else {
return json({ error: "Unrecognized command" }, { status: 400 });
}
}
return json({ error: "Unrecognized interaction type" }, { status: 400 });
},
})