mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-04-25 11:59:11 +00:00
feat: add pick, remove, and update the general /roleypoly command
This commit is contained in:
parent
a4207d5713
commit
1f55d0370a
10 changed files with 251 additions and 34 deletions
|
@ -1,22 +1,103 @@
|
||||||
|
import { CategoryType, RoleSafety } from '@roleypoly/types';
|
||||||
|
import { AuthType, discordFetch, respond } from '@roleypoly/worker-utils';
|
||||||
|
import { difference, keyBy } from 'lodash';
|
||||||
import { interactionsEndpoint } from '../utils/api-tools';
|
import { interactionsEndpoint } from '../utils/api-tools';
|
||||||
import { getGuildData } from '../utils/guild';
|
import { botToken } from '../utils/config';
|
||||||
import { invalid, notFound, ok } from '../utils/responses';
|
import {
|
||||||
|
getGuild,
|
||||||
|
getGuildData,
|
||||||
|
getGuildMember,
|
||||||
|
updateGuildMember,
|
||||||
|
} from '../utils/guild';
|
||||||
|
import { conflict, invalid, notAuthenticated, notFound, ok } from '../utils/responses';
|
||||||
|
|
||||||
export const InteractionsPickRole = interactionsEndpoint(
|
export const InteractionsPickRole = interactionsEndpoint(
|
||||||
async (request: Request): Promise<Response> => {
|
async (request: Request): Promise<Response> => {
|
||||||
|
const mode = request.method === 'PUT' ? 'add' : 'remove';
|
||||||
const reqURL = new URL(request.url);
|
const reqURL = new URL(request.url);
|
||||||
const [, , serverID, userID, roleID] = reqURL.pathname.split('/');
|
const [, , guildID, userID, roleID] = reqURL.pathname.split('/');
|
||||||
if (!serverID || !userID || !roleID) {
|
if (!guildID || !userID || !roleID) {
|
||||||
return invalid();
|
return invalid();
|
||||||
}
|
}
|
||||||
|
|
||||||
const guildData = await getGuildData(serverID);
|
const guildP = getGuild(guildID);
|
||||||
if (!guildData) {
|
const guildDataP = getGuildData(guildID);
|
||||||
|
const guildMemberP = getGuildMember(
|
||||||
|
{ serverID: guildID, userID },
|
||||||
|
{ skipCachePull: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
const [guild, guildData, guildMember] = await Promise.all([
|
||||||
|
guildP,
|
||||||
|
guildDataP,
|
||||||
|
guildMemberP,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!guild || !guildData || !guildMember) {
|
||||||
return notFound();
|
return notFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
// We get exactly one role, but we have to interact with it the same way as UI does.
|
let memberRoles = guildMember.roles;
|
||||||
// So check for safety, disable any "single" mode roles
|
|
||||||
|
if (
|
||||||
|
(mode === 'add' && memberRoles.includes(roleID)) ||
|
||||||
|
(mode !== 'add' && !memberRoles.includes(roleID))
|
||||||
|
) {
|
||||||
|
return conflict();
|
||||||
|
}
|
||||||
|
|
||||||
|
const roleMap = keyBy(guild.roles, 'id');
|
||||||
|
|
||||||
|
const category = guildData.categories.find((category) =>
|
||||||
|
category.roles.includes(roleID)
|
||||||
|
);
|
||||||
|
// No category? illegal.
|
||||||
|
if (!category) {
|
||||||
|
return notFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Category is hidden, this is illegal
|
||||||
|
if (category.hidden) {
|
||||||
|
return notFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Role is unsafe, super illegal.
|
||||||
|
if (roleMap[roleID].safety !== RoleSafety.Safe) {
|
||||||
|
return notAuthenticated();
|
||||||
|
}
|
||||||
|
|
||||||
|
// In add mode, if the category is a single-mode, remove the other roles in the category.
|
||||||
|
if (mode === 'add' && category.type === CategoryType.Single) {
|
||||||
|
memberRoles = difference(memberRoles, category.roles);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mode === 'add') {
|
||||||
|
memberRoles = [...memberRoles, roleID];
|
||||||
|
} else {
|
||||||
|
memberRoles = memberRoles.filter((id) => id !== roleID);
|
||||||
|
}
|
||||||
|
|
||||||
|
const patchMemberRoles = await discordFetch<Member>(
|
||||||
|
`/guilds/${guildID}/members/${userID}`,
|
||||||
|
botToken,
|
||||||
|
AuthType.Bot,
|
||||||
|
{
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: {
|
||||||
|
'content-type': 'application/json',
|
||||||
|
'x-audit-log-reason': `Picked their roles via slash command`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
roles: memberRoles,
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!patchMemberRoles) {
|
||||||
|
return respond({ error: 'discord rejected the request' }, { status: 500 });
|
||||||
|
}
|
||||||
|
|
||||||
|
await updateGuildMember({ serverID: guildID, userID });
|
||||||
|
|
||||||
return ok();
|
return ok();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import { memberPassesAccessControl } from '@roleypoly/api/utils/access-control';
|
|
||||||
import { accessControlViolation } from '@roleypoly/api/utils/responses';
|
|
||||||
import {
|
import {
|
||||||
GuildData,
|
GuildData,
|
||||||
Member,
|
Member,
|
||||||
|
@ -13,7 +11,7 @@ import {
|
||||||
import { AuthType, discordFetch, respond } from '@roleypoly/worker-utils';
|
import { AuthType, discordFetch, respond } from '@roleypoly/worker-utils';
|
||||||
import { difference, groupBy, keyBy, union } from 'lodash';
|
import { difference, groupBy, keyBy, union } from 'lodash';
|
||||||
import { withSession } from '../utils/api-tools';
|
import { withSession } from '../utils/api-tools';
|
||||||
import { botToken } from '../utils/config';
|
import { botToken, uiPublicURI } from '../utils/config';
|
||||||
import {
|
import {
|
||||||
getGuild,
|
getGuild,
|
||||||
getGuildData,
|
getGuildData,
|
||||||
|
@ -58,10 +56,6 @@ export const UpdateRoles = withSession(
|
||||||
|
|
||||||
const guildData = await getGuildData(guildID);
|
const guildData = await getGuildData(guildID);
|
||||||
|
|
||||||
if (!memberPassesAccessControl(guildCheck, guildMember, guildData.accessControl)) {
|
|
||||||
return accessControlViolation();
|
|
||||||
}
|
|
||||||
|
|
||||||
const newRoles = calculateNewRoles({
|
const newRoles = calculateNewRoles({
|
||||||
currentRoles: guildMember.roles,
|
currentRoles: guildMember.roles,
|
||||||
guildRoles: guild.roles,
|
guildRoles: guild.roles,
|
||||||
|
@ -77,7 +71,7 @@ export const UpdateRoles = withSession(
|
||||||
method: 'PATCH',
|
method: 'PATCH',
|
||||||
headers: {
|
headers: {
|
||||||
'content-type': 'application/json',
|
'content-type': 'application/json',
|
||||||
'x-audit-log-reason': `${username}#${discriminator} changes their roles via ${url.hostname}`,
|
'x-audit-log-reason': `Picked their roles via ${uiPublicURI}`,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
roles: newRoles,
|
roles: newRoles,
|
||||||
|
|
|
@ -36,7 +36,8 @@ router.add('POST', 'clear-guild-cache', ClearGuildCache);
|
||||||
|
|
||||||
// Interactions endpoints
|
// Interactions endpoints
|
||||||
router.add('GET', 'interactions-pickable-roles', InteractionsPickableRoles);
|
router.add('GET', 'interactions-pickable-roles', InteractionsPickableRoles);
|
||||||
router.add('POST', 'interactions-pick-role', InteractionsPickRole);
|
router.add('PUT', 'interactions-pick-role', InteractionsPickRole);
|
||||||
|
router.add('DELETE', 'interactions-pick-role', InteractionsPickRole);
|
||||||
|
|
||||||
// Tester Routes
|
// Tester Routes
|
||||||
router.add('GET', 'x-headers', (request) => {
|
router.add('GET', 'x-headers', (request) => {
|
||||||
|
|
|
@ -1,8 +1,3 @@
|
||||||
import { helloWorld } from '@roleypoly/interactions/handlers/interactions/hello-world';
|
|
||||||
import { pickableRoles } from '@roleypoly/interactions/handlers/interactions/pickable-roles';
|
|
||||||
import { roleypoly } from '@roleypoly/interactions/handlers/interactions/roleypoly';
|
|
||||||
import { verifyRequest } from '@roleypoly/interactions/utils/interactions';
|
|
||||||
import { somethingWentWrong } from '@roleypoly/interactions/utils/responses';
|
|
||||||
import {
|
import {
|
||||||
InteractionData,
|
InteractionData,
|
||||||
InteractionRequest,
|
InteractionRequest,
|
||||||
|
@ -11,6 +6,12 @@ import {
|
||||||
InteractionType,
|
InteractionType,
|
||||||
} from '@roleypoly/types';
|
} from '@roleypoly/types';
|
||||||
import { respond } from '@roleypoly/worker-utils';
|
import { respond } from '@roleypoly/worker-utils';
|
||||||
|
import { verifyRequest } from '../utils/interactions';
|
||||||
|
import { somethingWentWrong } from '../utils/responses';
|
||||||
|
import { helloWorld } from './interactions/hello-world';
|
||||||
|
import { pickRole } from './interactions/pick-role';
|
||||||
|
import { pickableRoles } from './interactions/pickable-roles';
|
||||||
|
import { roleypoly } from './interactions/roleypoly';
|
||||||
|
|
||||||
const commands: Record<
|
const commands: Record<
|
||||||
InteractionData['name'],
|
InteractionData['name'],
|
||||||
|
@ -19,6 +20,8 @@ const commands: Record<
|
||||||
'hello-world': helloWorld,
|
'hello-world': helloWorld,
|
||||||
roleypoly: roleypoly,
|
roleypoly: roleypoly,
|
||||||
'pickable-roles': pickableRoles,
|
'pickable-roles': pickableRoles,
|
||||||
|
'pick-role': pickRole('add'),
|
||||||
|
'remove-role': pickRole('remove'),
|
||||||
};
|
};
|
||||||
|
|
||||||
export const interactionHandler = async (request: Request): Promise<Response> => {
|
export const interactionHandler = async (request: Request): Promise<Response> => {
|
||||||
|
|
|
@ -1,16 +1,80 @@
|
||||||
|
import { selectRole } from '@roleypoly/interactions/utils/api';
|
||||||
|
import { invalid, mustBeInGuild } from '@roleypoly/interactions/utils/responses';
|
||||||
import {
|
import {
|
||||||
InteractionCallbackType,
|
InteractionCallbackType,
|
||||||
|
InteractionFlags,
|
||||||
InteractionRequestCommand,
|
InteractionRequestCommand,
|
||||||
InteractionResponse,
|
InteractionResponse,
|
||||||
} from '@roleypoly/types';
|
} from '@roleypoly/types';
|
||||||
|
|
||||||
export const helloWorld = async (
|
export const pickRole =
|
||||||
interaction: InteractionRequestCommand
|
(mode: 'add' | 'remove') =>
|
||||||
): Promise<InteractionResponse> => {
|
async (interaction: InteractionRequestCommand): Promise<InteractionResponse> => {
|
||||||
return {
|
if (!interaction.guild_id) {
|
||||||
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
return mustBeInGuild();
|
||||||
data: {
|
}
|
||||||
content: `Hey there, ${interaction.member?.nick || interaction.user?.username}`,
|
|
||||||
},
|
const userID = interaction.member?.user?.id;
|
||||||
|
if (!userID) {
|
||||||
|
return mustBeInGuild();
|
||||||
|
}
|
||||||
|
|
||||||
|
const roleID = interaction.data.options?.find(
|
||||||
|
(option) => option.name === 'role'
|
||||||
|
)?.value;
|
||||||
|
if (!roleID) {
|
||||||
|
return invalid();
|
||||||
|
}
|
||||||
|
|
||||||
|
const code = await selectRole(mode, interaction.guild_id, userID, roleID);
|
||||||
|
|
||||||
|
if (code === 409) {
|
||||||
|
return {
|
||||||
|
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||||
|
data: {
|
||||||
|
content: `:x: You ${mode === 'add' ? 'already' : "don't"} have that role.`,
|
||||||
|
flags: InteractionFlags.EPHEMERAL,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code === 404) {
|
||||||
|
return {
|
||||||
|
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||||
|
data: {
|
||||||
|
content: `:x: <@&${roleID}> isn't pickable.`,
|
||||||
|
flags: InteractionFlags.EPHEMERAL,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code === 403) {
|
||||||
|
return {
|
||||||
|
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||||
|
data: {
|
||||||
|
content: `:x: <@&${roleID}> has unsafe permissions.`,
|
||||||
|
flags: InteractionFlags.EPHEMERAL,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code !== 200) {
|
||||||
|
return {
|
||||||
|
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||||
|
data: {
|
||||||
|
content: `:x: Something went wrong, please try again later.`,
|
||||||
|
flags: InteractionFlags.EPHEMERAL,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||||
|
data: {
|
||||||
|
content: `:white_check_mark: You ${
|
||||||
|
mode === 'add' ? 'got' : 'removed'
|
||||||
|
} the role: <@&${roleID}>`,
|
||||||
|
flags: InteractionFlags.EPHEMERAL,
|
||||||
|
},
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { uiPublicURI } from '@roleypoly/interactions/utils/config';
|
import { uiPublicURI } from '@roleypoly/interactions/utils/config';
|
||||||
import {
|
import {
|
||||||
|
Embed,
|
||||||
InteractionCallbackType,
|
InteractionCallbackType,
|
||||||
InteractionFlags,
|
InteractionFlags,
|
||||||
InteractionRequestCommand,
|
InteractionRequestCommand,
|
||||||
|
@ -13,7 +14,34 @@ export const roleypoly = async (
|
||||||
return {
|
return {
|
||||||
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||||
data: {
|
data: {
|
||||||
content: `:beginner: Assign your roles here! ${uiPublicURI}/s/${interaction.guild_id}`,
|
embeds: [
|
||||||
|
{
|
||||||
|
color: 0x453e3d,
|
||||||
|
title: `:beginner: Hey there, ${
|
||||||
|
interaction.member?.nick || interaction.member?.user?.username || 'friend'
|
||||||
|
}!`,
|
||||||
|
description: `Try these slash commands, or pick roles from your browser!`,
|
||||||
|
fields: [
|
||||||
|
{ name: 'See all the roles', value: '/pickable-roles' },
|
||||||
|
{ name: 'Pick a role', value: '/pick-role' },
|
||||||
|
{ name: 'Remove a role', value: '/remove-role' },
|
||||||
|
],
|
||||||
|
} as Embed,
|
||||||
|
],
|
||||||
|
components: [
|
||||||
|
{
|
||||||
|
type: 1,
|
||||||
|
components: [
|
||||||
|
// Link to Roleypoly
|
||||||
|
{
|
||||||
|
type: 2,
|
||||||
|
label: `Pick roles on ${new URL(uiPublicURI).hostname}`,
|
||||||
|
url: `${uiPublicURI}/s/${interaction.guild_id}`,
|
||||||
|
style: 5,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
flags: InteractionFlags.EPHEMERAL,
|
flags: InteractionFlags.EPHEMERAL,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -23,3 +23,19 @@ export const getPickableRoles = async (
|
||||||
|
|
||||||
return (await response.json()) as Record<Category['name'], CategorySlug>;
|
return (await response.json()) as Record<Category['name'], CategorySlug>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const selectRole = async (
|
||||||
|
mode: 'add' | 'remove',
|
||||||
|
guildID: string,
|
||||||
|
userID: string,
|
||||||
|
roleID: string
|
||||||
|
): Promise<number> => {
|
||||||
|
const response = await apiFetch(
|
||||||
|
`/interactions-pick-role/${guildID}/${userID}/${roleID}`,
|
||||||
|
{
|
||||||
|
method: mode === 'add' ? 'PUT' : 'DELETE',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return response.status;
|
||||||
|
};
|
||||||
|
|
|
@ -12,6 +12,14 @@ export const mustBeInGuild = (): InteractionResponse => ({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const invalid = (): InteractionResponse => ({
|
||||||
|
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||||
|
data: {
|
||||||
|
content: ':x: You filled that command out wrong...',
|
||||||
|
flags: InteractionFlags.EPHEMERAL,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
export const somethingWentWrong = (): InteractionResponse => ({
|
export const somethingWentWrong = (): InteractionResponse => ({
|
||||||
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||||
data: {
|
data: {
|
||||||
|
|
|
@ -28,7 +28,11 @@ export type InteractionData = {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
resolved?: {};
|
resolved?: {};
|
||||||
options?: {}[];
|
options?: {
|
||||||
|
name: string;
|
||||||
|
type: number;
|
||||||
|
value?: string;
|
||||||
|
}[];
|
||||||
custom_id: string;
|
custom_id: string;
|
||||||
component_type: string;
|
component_type: string;
|
||||||
};
|
};
|
||||||
|
|
|
@ -32,6 +32,7 @@ resource "discord-interactions_global_command" "pick-role" {
|
||||||
name = "role"
|
name = "role"
|
||||||
description = "The role you want"
|
description = "The role you want"
|
||||||
type = 8
|
type = 8
|
||||||
|
required = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,12 +41,29 @@ resource "discord-interactions_guild_command" "pick-role" {
|
||||||
guild_id = each.value
|
guild_id = each.value
|
||||||
|
|
||||||
name = "pick-role"
|
name = "pick-role"
|
||||||
description = "Pick a role! (See which ones can be picked with /pickable-roles)"
|
description = "**[TEST]** Pick a role! (See which ones can be picked with /pickable-roles)"
|
||||||
|
|
||||||
|
|
||||||
option {
|
option {
|
||||||
name = "role"
|
name = "role"
|
||||||
description = "The role you want"
|
description = "The role you want"
|
||||||
type = 8
|
type = 8
|
||||||
|
required = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "discord-interactions_guild_command" "remove-role" {
|
||||||
|
for_each = local.internalTestingGuilds
|
||||||
|
guild_id = each.value
|
||||||
|
|
||||||
|
name = "remove-role"
|
||||||
|
description = "**[TEST]** Pick a role to remove (See which ones can be removed with /pickable-roles)"
|
||||||
|
|
||||||
|
option {
|
||||||
|
name = "role"
|
||||||
|
description = "The role you want to remove"
|
||||||
|
type = 8
|
||||||
|
required = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue