-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
144 lines (124 loc) · 4.45 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
import * as env from "./env.ts";
import { discord } from "./deps.ts";
import { DiscordAPIClient, verify } from "./bot/discord/mod.ts";
import { APP_TLDR } from "./bot/app/app.ts";
import { tldr, TLDROptions } from "./tldr.ts";
const api = new DiscordAPIClient();
if (import.meta.main) {
await main();
}
export function main() {
// Define onListen callback.
async function onListen() {
// Overwrite the Discord Application Command.
await api.registerCommand({
app: APP_TLDR,
botID: env.DISCORD_CLIENT_ID,
botToken: env.DISCORD_TOKEN,
});
// Log the application command.
console.log(
"TLDR application command:\n",
`- Local: http://localhost:${env.PORT}/\n`,
`- Invite: https://discord.com/api/oauth2/authorize?client_id=${env.DISCORD_CLIENT_ID}&scope=applications.commands%20guilds.members.read%20bot&permissions=0\n`,
`- Info: https://discord.com/developers/applications/${env.DISCORD_CLIENT_ID}/information`,
);
}
// Start the server.
Deno.serve(
{ port: env.PORT, onListen },
handle,
);
}
/**
* handle is the HTTP handler for the TLDR 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: {
// Assert the interaction is a context menu interaction.
if (
!discord.Utils.isContextMenuApplicationCommandInteraction(interaction)
) {
return new Response("Invalid request", { status: 400 });
}
// Assert the interaction member has the required role.
if (!interaction.member?.roles.includes(env.DISCORD_ROLE_ID)) {
return new Response("Invalid request", { status: 400 });
}
// Assert the interaction data is a message.
if (interaction.data.type !== discord.ApplicationCommandType.Message) {
return new Response("Invalid request", { status: 400 });
}
// Assert the guild ID is present.
if (!interaction.guild_id) {
return new Response("Invalid request", { status: 400 });
}
// Get the message.
const message = Object.values(interaction.data.resolved.messages)[0];
if (!message) {
return new Response("Invalid request", { status: 400 });
}
// Assert the message is not from the bot.
if (message.author.id === env.DISCORD_CLIENT_ID) {
return new Response("Invalid request", { status: 400 });
}
// Get the guild member author.
const author = await api.retrieveGuildUser({
botToken: env.DISCORD_TOKEN,
guildID: interaction.guild_id,
userID: message.author.id,
});
// Make the TLDROptions.
const options: TLDROptions = {
apiKey: env.PALM_API_KEY,
author: author.nick ?? message.author.username,
message: message.content,
};
// Create message URL.
const messageURL =
`https://discord.com/channels/${interaction.guild_id}/${message.channel_id}/${message.id}`;
// Send the TLDR.
tldr(options)
.then((result) => {
api.editOriginalInteractionResponse({
botID: env.DISCORD_CLIENT_ID,
botToken: env.DISCORD_TOKEN,
interactionToken: interaction.token,
content: formatContent(`TL;DR: ${result}`, messageURL),
});
})
.catch((error) => {
if (error instanceof Error) {
api.editOriginalInteractionResponse({
botID: env.DISCORD_CLIENT_ID,
botToken: env.DISCORD_TOKEN,
interactionToken: interaction.token,
content: formatContent(`Error: ${error.message}`, messageURL),
});
}
});
return Response.json(
{
type:
discord.InteractionResponseType.DeferredChannelMessageWithSource,
} satisfies discord.APIInteractionResponseDeferredChannelMessageWithSource,
);
}
default: {
return new Response("Invalid request", { status: 400 });
}
}
}
function formatContent(content: string, messageURL: string) {
return `${content} \n\n↩${messageURL}`;
}