2022-04-14 12:41:23 +02:00
|
|
|
|
import { APIApplicationCommandInteraction, APIMessageComponentInteraction, APIPingInteraction, ApplicationCommandType, ComponentType, InteractionResponseType, InteractionType, MessageFlags, RouteBases, Routes } from 'discord-api-types/v9';
|
|
|
|
|
import { badFormatting } from './utils/badFormatting';
|
2022-04-10 21:09:37 +02:00
|
|
|
|
import { isJSON } from './utils/isJson';
|
2022-04-14 12:41:23 +02:00
|
|
|
|
import { resolveButtonComponents, resolveSelectMenuComponents } from './utils/resolveComponents';
|
|
|
|
|
import { respond } from './utils/respond';
|
2022-04-10 21:09:37 +02:00
|
|
|
|
import { verify } from './utils/verify';
|
2021-12-21 12:55:10 +01:00
|
|
|
|
|
|
|
|
|
export const handleRequest = async(request: Request): Promise<Response> => {
|
2022-01-02 18:54:19 +01:00
|
|
|
|
if (!request.headers.get('X-Signature-Ed25519') || !request.headers.get('X-Signature-Timestamp')) return Response.redirect('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
|
|
|
|
|
if (!await verify(request)) return new Response('', { status: 401 });
|
|
|
|
|
|
|
|
|
|
const interaction = await request.json() as APIPingInteraction | APIApplicationCommandInteraction | APIMessageComponentInteraction;
|
|
|
|
|
|
2022-04-14 12:41:23 +02:00
|
|
|
|
console.log(interaction);
|
2022-01-02 18:54:19 +01:00
|
|
|
|
if (interaction.type === InteractionType.Ping)
|
|
|
|
|
return respond({
|
|
|
|
|
type: InteractionResponseType.Pong
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-14 12:41:23 +02:00
|
|
|
|
if (interaction.type === InteractionType.ApplicationCommand && interaction.data.type === ApplicationCommandType.ChatInput && interaction.data.name === 'setup') {
|
2022-01-02 18:54:19 +01:00
|
|
|
|
if ((Number(interaction.member?.permissions) & 0x10) !== 0x10) return respond({
|
|
|
|
|
type: InteractionResponseType.ChannelMessageWithSource,
|
|
|
|
|
data: {
|
|
|
|
|
flags: 64,
|
|
|
|
|
content: 'Required permissions: `MANAGE_ROLES`'
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-14 12:41:23 +02:00
|
|
|
|
// @ts-expect-error No typings for value
|
|
|
|
|
const json = isJSON(interaction.data.options?.[0]?.value) ? JSON.parse(interaction.data.options?.[0]?.value) : null;
|
2022-01-02 18:54:19 +01:00
|
|
|
|
if (!json) return badFormatting();
|
|
|
|
|
|
|
|
|
|
const channelId = json.channel;
|
2022-04-14 11:48:02 +02:00
|
|
|
|
let message = json.message?.toString();
|
2022-01-02 18:54:19 +01:00
|
|
|
|
let roles = json.roles;
|
|
|
|
|
|
|
|
|
|
if (!channelId) return badFormatting();
|
2022-04-14 11:48:02 +02:00
|
|
|
|
if (!message) message = '';
|
2022-01-02 18:54:19 +01:00
|
|
|
|
if (!roles || Object.values(json.roles).filter((role: any) => role.id && role.label).length === 0 || roles.length === 0 || roles.length > 25) return badFormatting(roles.length > 25);
|
|
|
|
|
|
2022-04-14 12:41:23 +02:00
|
|
|
|
const finalComponents = json.type === 1 ? resolveButtonComponents(roles) : resolveSelectMenuComponents(roles, json.placeholder?.toString());
|
2022-04-08 17:58:30 +02:00
|
|
|
|
|
2022-03-11 18:19:48 +01:00
|
|
|
|
const fetched = await fetch(`${RouteBases.api}/channels/${channelId}/messages`, {
|
2022-01-02 18:54:19 +01:00
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Authorization': `Bot ${CLIENT_TOKEN}`,
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
content: message,
|
|
|
|
|
components: finalComponents
|
|
|
|
|
})
|
2022-04-14 11:48:02 +02:00
|
|
|
|
}).catch(e => e);
|
2022-01-02 18:54:19 +01:00
|
|
|
|
|
|
|
|
|
return respond({
|
|
|
|
|
type: InteractionResponseType.ChannelMessageWithSource,
|
|
|
|
|
data: {
|
|
|
|
|
flags: 64,
|
2022-04-14 11:48:02 +02:00
|
|
|
|
content: fetched?.ok ? 'Done!' : 'Error, bad channel id/missing permissions.'
|
2022-01-02 18:54:19 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else if (interaction.type === InteractionType.MessageComponent) {
|
2022-04-14 12:41:23 +02:00
|
|
|
|
const roleId = interaction.data.component_type === ComponentType.Button ? interaction.data.custom_id : interaction.data.values[0];
|
2022-01-02 18:54:19 +01:00
|
|
|
|
const url = `${RouteBases.api}${Routes.guildMemberRole(interaction.guild_id || '', interaction.member?.user.id || '', roleId)}`;
|
|
|
|
|
|
|
|
|
|
let method = '';
|
|
|
|
|
let content = '';
|
|
|
|
|
|
|
|
|
|
if (!interaction?.member?.roles?.includes(roleId)) {
|
|
|
|
|
content = `Gave the <@&${roleId}> role!`;
|
|
|
|
|
method = 'PUT';
|
|
|
|
|
} else {
|
|
|
|
|
content = `Removed the <@&${roleId}> role!`;
|
|
|
|
|
method = 'DELETE';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await fetch(url, {
|
|
|
|
|
method: method,
|
|
|
|
|
headers: {
|
|
|
|
|
'Authorization': `Bot ${CLIENT_TOKEN}`
|
|
|
|
|
}
|
|
|
|
|
}).catch(e => e);
|
2021-12-21 12:55:10 +01:00
|
|
|
|
|
2022-01-02 18:54:19 +01:00
|
|
|
|
return respond({
|
|
|
|
|
type: InteractionResponseType.ChannelMessageWithSource,
|
|
|
|
|
data: {
|
|
|
|
|
flags: MessageFlags.Ephemeral,
|
|
|
|
|
content: content,
|
|
|
|
|
allowed_mentions: { parse: [] }
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-04-14 12:41:23 +02:00
|
|
|
|
} else {
|
|
|
|
|
return respond({
|
|
|
|
|
type: InteractionResponseType.ChannelMessageWithSource,
|
|
|
|
|
data: {
|
|
|
|
|
flags: MessageFlags.Ephemeral,
|
|
|
|
|
content: 'Beep boop, boop beep?'
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-01-02 18:54:19 +01:00
|
|
|
|
}
|
|
|
|
|
};
|