-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathbot.ts
78 lines (66 loc) · 3.16 KB
/
bot.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
import { verify } from './verify'
import { InteractionType, InteractionResponseType, APIInteractionResponse, RESTPostAPIChannelInviteJSONBody, APIInvite, ApplicationCommandOptionType, ChannelType, MessageFlags, APIApplicationCommandInteraction, InviteTargetType, RouteBases, Routes } from 'discord-api-types/v9'
import { APIPingInteraction } from 'discord-api-types/payloads/v9/_interactions/ping'
export async function handleRequest(request: Request): Promise<Response> {
if (!request.headers.get('X-Signature-Ed25519') || !request.headers.get('X-Signature-Timestamp')) return Response.redirect('https://advaith.io')
if (!await verify(request)) return new Response('', { status: 401 })
const interaction = await request.json() as APIPingInteraction | APIApplicationCommandInteraction
if (interaction.type === InteractionType.Ping)
return respond({
type: InteractionResponseType.Pong
})
if (interaction.data.name === 'invite')
return respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: '[Click to add to your server](https://discord.com/api/oauth2/authorize?client_id=819778342818414632&scope=bot%20applications.commands)'
}
})
if (!interaction.data.resolved?.channels)
return respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: 'Please update your Discord app to use this command.',
flags: MessageFlags.Ephemeral
}
})
// set option types for ts
if (!interaction.data.options ||
interaction.data.options[0].type !== ApplicationCommandOptionType.Channel ||
interaction.data.options[1].type !== ApplicationCommandOptionType.String) return new Response()
if (interaction.data.resolved.channels[interaction.data.options[0].value].type !== ChannelType.GuildVoice)
return respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: 'The selected channel must be a voice channel'
}
})
const r = await fetch(`${RouteBases.api}${Routes.channelInvites(interaction.data.options[0].value)}`, {
method: 'POST',
headers: { authorization: `Bot ${token}`, 'content-type': 'application/json' },
body: JSON.stringify({
max_age: 0,
target_type: InviteTargetType.EmbeddedApplication,
target_application_id: interaction.data.options[1].value
} as RESTPostAPIChannelInviteJSONBody)
})
const invite = await r.json() as APIInvite
if (r.status !== 200) {
return respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: `An error occured: ${(invite as any).message}\nMake sure I have the "Create Invite" permission in the voice channel!`,
allowed_mentions: { parse: [] }
}
})
}
return respond({
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: `[Click to open ${invite.target_application!.name} in ${invite.channel!.name}](<https://discord.gg/${invite.code}>)`,
allowed_mentions: { parse: [] }
}
})
}
const respond = (response: APIInteractionResponse) =>
new Response(JSON.stringify(response), {headers: {'content-type': 'application/json'}})