mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-16 17:49:09 +00:00
feat: Slash Commands (#337)
* feat: add discord interactions worker * feat(interactions): update CI/CD and terraform to add interactions * chore: fix lint issues * chore: fix build & emulation * fix(interactions): deployment + handler * chore: remove worker-dist via gitignore * feat: add /pickable-roles and /pick-role basis * feat: add pick, remove, and update the general /roleypoly command * fix: lint missing Member import
This commit is contained in:
parent
dde05c402e
commit
066f68ffef
59 changed files with 1219 additions and 248 deletions
41
packages/interactions/utils/api.ts
Normal file
41
packages/interactions/utils/api.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
import { Category, CategorySlug } from '@roleypoly/types';
|
||||
import { apiPublicURI, interactionsSharedKey } from './config';
|
||||
|
||||
export const apiFetch = (url: string, init: RequestInit = {}) =>
|
||||
fetch(`${apiPublicURI}${url}`, {
|
||||
...init,
|
||||
headers: {
|
||||
...(init.headers || {}),
|
||||
authorization: `Shared ${interactionsSharedKey}`,
|
||||
},
|
||||
});
|
||||
|
||||
export const getPickableRoles = async (
|
||||
guildID: string
|
||||
): Promise<Record<Category['name'], CategorySlug>> => {
|
||||
const response = await apiFetch(`/interactions-pickable-roles/${guildID}`);
|
||||
|
||||
if (response.status !== 200) {
|
||||
throw new Error(
|
||||
`API request failed to /interactions-pickable-roles, got code: ${response.status}`
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
11
packages/interactions/utils/config.ts
Normal file
11
packages/interactions/utils/config.ts
Normal file
|
@ -0,0 +1,11 @@
|
|||
const self = global as any as Record<string, string>;
|
||||
|
||||
const env = (key: string) => self[key] ?? '';
|
||||
|
||||
const safeURI = (x: string) => x.replace(/\/$/, '');
|
||||
const list = (x: string) => x.split(',');
|
||||
|
||||
export const uiPublicURI = safeURI(env('UI_PUBLIC_URI'));
|
||||
export const apiPublicURI = safeURI(env('API_PUBLIC_URI'));
|
||||
export const publicKey = safeURI(env('DISCORD_PUBLIC_KEY'));
|
||||
export const interactionsSharedKey = env('INTERACTIONS_SHARED_KEY');
|
27
packages/interactions/utils/interactions.ts
Normal file
27
packages/interactions/utils/interactions.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { publicKey } from '@roleypoly/interactions/utils/config';
|
||||
import { InteractionRequest } from '@roleypoly/types';
|
||||
import nacl from 'tweetnacl';
|
||||
|
||||
export const verifyRequest = (
|
||||
request: Request,
|
||||
interaction: InteractionRequest
|
||||
): boolean => {
|
||||
const timestamp = request.headers.get('x-signature-timestamp');
|
||||
const signature = request.headers.get('x-signature-ed25519');
|
||||
|
||||
if (!timestamp || !signature) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
!nacl.sign.detached.verify(
|
||||
Buffer.from(timestamp + JSON.stringify(interaction)),
|
||||
Buffer.from(signature, 'hex'),
|
||||
Buffer.from(publicKey, 'hex')
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
29
packages/interactions/utils/responses.ts
Normal file
29
packages/interactions/utils/responses.ts
Normal file
|
@ -0,0 +1,29 @@
|
|||
import {
|
||||
InteractionCallbackType,
|
||||
InteractionFlags,
|
||||
InteractionResponse,
|
||||
} from '@roleypoly/types';
|
||||
|
||||
export const mustBeInGuild = (): InteractionResponse => ({
|
||||
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||
data: {
|
||||
content: ':x: This command has to be used in a server.',
|
||||
flags: InteractionFlags.EPHEMERAL,
|
||||
},
|
||||
});
|
||||
|
||||
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 => ({
|
||||
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||
data: {
|
||||
content: '<a:promareFlame:624850108667789333> Something went terribly wrong.',
|
||||
flags: InteractionFlags.EPHEMERAL,
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue