mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-16 09:39: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
|
@ -1,12 +1,18 @@
|
|||
import { notAuthenticated } from '@roleypoly/api/utils/responses';
|
||||
import {
|
||||
evaluatePermission,
|
||||
permissions as Permissions,
|
||||
} from '@roleypoly/misc-utils/hasPermission';
|
||||
import { SessionData, UserGuildPermissions } from '@roleypoly/types';
|
||||
import { Handler, WrappedKVNamespace } from '@roleypoly/worker-utils';
|
||||
import KSUID from 'ksuid';
|
||||
import { Handler } from '../router';
|
||||
import { allowedCallbackHosts, apiPublicURI, discordAPIBase, rootUsers } from './config';
|
||||
import { Sessions, WrappedKVNamespace } from './kv';
|
||||
import {
|
||||
allowedCallbackHosts,
|
||||
apiPublicURI,
|
||||
interactionsSharedKey,
|
||||
rootUsers,
|
||||
} from './config';
|
||||
import { Sessions } from './kv';
|
||||
|
||||
export const formData = (obj: Record<string, any>): string => {
|
||||
return Object.keys(obj)
|
||||
|
@ -14,18 +20,8 @@ export const formData = (obj: Record<string, any>): string => {
|
|||
.join('&');
|
||||
};
|
||||
|
||||
export const addCORS = (init: ResponseInit = {}) => ({
|
||||
...init,
|
||||
headers: {
|
||||
...(init.headers || {}),
|
||||
'access-control-allow-origin': '*',
|
||||
'access-control-allow-methods': '*',
|
||||
'access-control-allow-headers': '*',
|
||||
},
|
||||
});
|
||||
|
||||
export const respond = (obj: Record<string, any>, init: ResponseInit = {}) =>
|
||||
new Response(JSON.stringify(obj), addCORS(init));
|
||||
new Response(JSON.stringify(obj), init);
|
||||
|
||||
export const resolveFailures =
|
||||
(
|
||||
|
@ -70,44 +66,6 @@ export const getSessionID = (request: Request): { type: string; id: string } | n
|
|||
return { type, id };
|
||||
};
|
||||
|
||||
export const userAgent =
|
||||
'DiscordBot (https://github.com/roleypoly/roleypoly, git-main) (+https://roleypoly.com)';
|
||||
|
||||
export enum AuthType {
|
||||
Bearer = 'Bearer',
|
||||
Bot = 'Bot',
|
||||
}
|
||||
|
||||
export const discordFetch = async <T>(
|
||||
url: string,
|
||||
auth: string,
|
||||
authType: AuthType = AuthType.Bearer,
|
||||
init?: RequestInit
|
||||
): Promise<T | null> => {
|
||||
const response = await fetch(discordAPIBase + url, {
|
||||
...(init || {}),
|
||||
headers: {
|
||||
...(init?.headers || {}),
|
||||
authorization: `${AuthType[authType]} ${auth}`,
|
||||
'user-agent': userAgent,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.status >= 400) {
|
||||
console.error('discordFetch failed', {
|
||||
url,
|
||||
authType,
|
||||
payload: await response.text(),
|
||||
});
|
||||
}
|
||||
|
||||
if (response.ok) {
|
||||
return (await response.json()) as T;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
export type CacheLayerOptions = {
|
||||
skipCachePull?: boolean;
|
||||
};
|
||||
|
@ -195,16 +153,6 @@ export const onlyRootUsers = (handler: Handler): Handler =>
|
|||
);
|
||||
});
|
||||
|
||||
export const getQuery = (request: Request): { [x: string]: string } => {
|
||||
const output: { [x: string]: string } = {};
|
||||
|
||||
for (let [key, value] of new URL(request.url).searchParams.entries()) {
|
||||
output[key] = value;
|
||||
}
|
||||
|
||||
return output;
|
||||
};
|
||||
|
||||
export const isAllowedCallbackHost = (host: string): boolean => {
|
||||
return (
|
||||
host === apiPublicURI ||
|
||||
|
@ -215,3 +163,14 @@ export const isAllowedCallbackHost = (host: string): boolean => {
|
|||
null
|
||||
);
|
||||
};
|
||||
|
||||
export const interactionsEndpoint =
|
||||
(handler: Handler): Handler =>
|
||||
async (request: Request): Promise<Response> => {
|
||||
const authHeader = request.headers.get('authorization') || '';
|
||||
if (authHeader !== `Shared ${interactionsSharedKey}`) {
|
||||
return notAuthenticated();
|
||||
}
|
||||
|
||||
return handler(request);
|
||||
};
|
||||
|
|
|
@ -1,35 +1,21 @@
|
|||
import { userAgent } from '@roleypoly/api/utils/api-tools';
|
||||
import { uiPublicURI } from '@roleypoly/api/utils/config';
|
||||
import {
|
||||
Category,
|
||||
DiscordUser,
|
||||
Embed,
|
||||
GuildData,
|
||||
GuildDataUpdate,
|
||||
GuildSlug,
|
||||
WebhookValidationStatus,
|
||||
} from '@roleypoly/types';
|
||||
import { userAgent } from '@roleypoly/worker-utils';
|
||||
import deepEqual from 'deep-equal';
|
||||
import { sortBy, uniq } from 'lodash';
|
||||
|
||||
type WebhookEmbed = {
|
||||
fields: {
|
||||
name: string;
|
||||
value: string;
|
||||
inline: boolean;
|
||||
}[];
|
||||
timestamp: string;
|
||||
title: string;
|
||||
color: number;
|
||||
author?: {
|
||||
name: string;
|
||||
icon_url: string;
|
||||
};
|
||||
};
|
||||
|
||||
type WebhookPayload = {
|
||||
username: string;
|
||||
avatar_url: string;
|
||||
embeds: WebhookEmbed[];
|
||||
embeds: Embed[];
|
||||
provider: {
|
||||
name: string;
|
||||
url: string;
|
||||
|
@ -39,7 +25,7 @@ type WebhookPayload = {
|
|||
type ChangeHandler = (
|
||||
oldValue: GuildDataUpdate[keyof GuildDataUpdate],
|
||||
newValue: GuildData[keyof GuildDataUpdate]
|
||||
) => WebhookEmbed[];
|
||||
) => Embed[];
|
||||
|
||||
const changeHandlers: Record<keyof GuildDataUpdate, ChangeHandler> = {
|
||||
message: (oldValue, newValue) => [
|
||||
|
|
|
@ -13,5 +13,4 @@ export const apiPublicURI = safeURI(env('API_PUBLIC_URI'));
|
|||
export const rootUsers = list(env('ROOT_USERS'));
|
||||
export const allowedCallbackHosts = list(env('ALLOWED_CALLBACK_HOSTS'));
|
||||
export const importSharedKey = env('BOT_IMPORT_TOKEN');
|
||||
|
||||
export const discordAPIBase = 'https://discordapp.com/api/v9';
|
||||
export const interactionsSharedKey = env('INTERACTIONS_SHARED_KEY');
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import { Handler } from '@roleypoly/api/router';
|
||||
import {
|
||||
lowPermissions,
|
||||
missingParameters,
|
||||
|
@ -18,14 +17,8 @@ import {
|
|||
SessionData,
|
||||
UserGuildPermissions,
|
||||
} from '@roleypoly/types';
|
||||
import {
|
||||
AuthType,
|
||||
cacheLayer,
|
||||
CacheLayerOptions,
|
||||
discordFetch,
|
||||
isRoot,
|
||||
withSession,
|
||||
} from './api-tools';
|
||||
import { AuthType, discordFetch, Handler } from '@roleypoly/worker-utils';
|
||||
import { cacheLayer, CacheLayerOptions, isRoot, withSession } from './api-tools';
|
||||
import { botClientID, botToken } from './config';
|
||||
import { GuildData, Guilds } from './kv';
|
||||
import { useRateLimiter } from './rate-limiting';
|
||||
|
|
|
@ -1,83 +1,4 @@
|
|||
export class WrappedKVNamespace {
|
||||
constructor(private kvNamespace: KVNamespace) {}
|
||||
|
||||
async get<T>(key: string): Promise<T | null> {
|
||||
const data = await this.kvNamespace.get(key, 'text');
|
||||
if (!data) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return JSON.parse(data) as T;
|
||||
}
|
||||
|
||||
async put<T>(key: string, value: T, ttlSeconds?: number) {
|
||||
await this.kvNamespace.put(key, JSON.stringify(value), {
|
||||
expirationTtl: ttlSeconds,
|
||||
});
|
||||
}
|
||||
|
||||
list = this.kvNamespace.list;
|
||||
getWithMetadata = this.kvNamespace.getWithMetadata;
|
||||
delete = this.kvNamespace.delete;
|
||||
}
|
||||
|
||||
class EmulatedKV implements KVNamespace {
|
||||
constructor() {
|
||||
console.warn('EmulatedKV used. Data will be lost.');
|
||||
}
|
||||
|
||||
private data: Map<string, any> = new Map();
|
||||
|
||||
async get<T>(key: string): Promise<T | null> {
|
||||
if (!this.data.has(key)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.data.get(key);
|
||||
}
|
||||
|
||||
async getWithMetadata<T, Metadata = unknown>(
|
||||
key: string
|
||||
): KVValueWithMetadata<T, Metadata> {
|
||||
return {
|
||||
value: await this.get<T>(key),
|
||||
metadata: {} as Metadata,
|
||||
};
|
||||
}
|
||||
|
||||
async put(key: string, value: string | ReadableStream<any> | ArrayBuffer | FormData) {
|
||||
this.data.set(key, value);
|
||||
}
|
||||
|
||||
async delete(key: string) {
|
||||
this.data.delete(key);
|
||||
}
|
||||
|
||||
async list(options?: { prefix?: string; limit?: number; cursor?: string }): Promise<{
|
||||
keys: { name: string; expiration?: number; metadata?: unknown }[];
|
||||
list_complete: boolean;
|
||||
cursor: string;
|
||||
}> {
|
||||
let keys: { name: string }[] = [];
|
||||
|
||||
for (let key of this.data.keys()) {
|
||||
if (options?.prefix && !key.startsWith(options.prefix)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
keys.push({ name: key });
|
||||
}
|
||||
|
||||
return {
|
||||
keys,
|
||||
cursor: '0',
|
||||
list_complete: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const kvOrLocal = (namespace: KVNamespace | null): KVNamespace =>
|
||||
namespace || new EmulatedKV();
|
||||
import { kvOrLocal, WrappedKVNamespace } from '@roleypoly/worker-utils';
|
||||
|
||||
const self = global as any as Record<string, any>;
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { WrappedKVNamespace } from './kv';
|
||||
import { WrappedKVNamespace } from '@roleypoly/worker-utils';
|
||||
|
||||
export const useRateLimiter =
|
||||
(kv: WrappedKVNamespace, key: string, timeoutSeconds: number) =>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { respond } from './api-tools';
|
||||
import { respond } from '@roleypoly/worker-utils';
|
||||
|
||||
export const ok = () => respond({ ok: true });
|
||||
|
||||
|
@ -20,3 +20,6 @@ export const rateLimited = () =>
|
|||
|
||||
export const invalid = (obj: any = {}) =>
|
||||
respond({ err: 'client sent something invalid', data: obj }, { status: 400 });
|
||||
|
||||
export const notAuthenticated = () =>
|
||||
respond({ err: 'not authenticated' }, { status: 403 });
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue