mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-04-25 11:59:11 +00:00
feat: add discord interactions worker
This commit is contained in:
parent
dde05c402e
commit
9354047447
36 changed files with 486 additions and 178 deletions
|
@ -2,6 +2,7 @@
|
||||||
BOT_CLIENT_ID=000000000000000000
|
BOT_CLIENT_ID=000000000000000000
|
||||||
BOT_CLIENT_SECRET=RnX8pXXXXXXXXXXXXXXXXXXXXXXXXXu-
|
BOT_CLIENT_SECRET=RnX8pXXXXXXXXXXXXXXXXXXXXXXXXXu-
|
||||||
BOT_TOKEN=Mzk2MjI3MTM0MjI3NXXXXXXXXXXXXXXXXXXXXXPUlYoARXXXXXXXXXXXXXX
|
BOT_TOKEN=Mzk2MjI3MTM0MjI3NXXXXXXXXXXXXXXXXXXXXXPUlYoARXXXXXXXXXXXXXX
|
||||||
|
DISCORD_PUBLIC_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
|
||||||
|
|
||||||
# Comma separated; put your user ID here. Gives elevated permissions to everything.
|
# Comma separated; put your user ID here. Gives elevated permissions to everything.
|
||||||
ROOT_USERS=62601275618889728
|
ROOT_USERS=62601275618889728
|
||||||
|
|
|
@ -7,10 +7,13 @@ type URLParams = {
|
||||||
clientID: string;
|
clientID: string;
|
||||||
permissions: number;
|
permissions: number;
|
||||||
guildID?: string;
|
guildID?: string;
|
||||||
|
scopes: string[];
|
||||||
};
|
};
|
||||||
|
|
||||||
const buildURL = (params: URLParams) => {
|
const buildURL = (params: URLParams) => {
|
||||||
let url = `https://discord.com/api/oauth2/authorize?client_id=${params.clientID}&scope=bot&permissions=${params.permissions}`;
|
let url = `https://discord.com/api/oauth2/authorize?client_id=${
|
||||||
|
params.clientID
|
||||||
|
}&scope=${params.scopes.join('%20')}&permissions=${params.permissions}`;
|
||||||
|
|
||||||
if (params.guildID) {
|
if (params.guildID) {
|
||||||
url += `&guild_id=${params.guildID}&disable_guild_select=true`;
|
url += `&guild_id=${params.guildID}&disable_guild_select=true`;
|
||||||
|
@ -31,6 +34,7 @@ export const BotJoin = (request: Request): Response => {
|
||||||
clientID: botClientID,
|
clientID: botClientID,
|
||||||
permissions: 268435456,
|
permissions: 268435456,
|
||||||
guildID,
|
guildID,
|
||||||
|
scopes: ['bot', 'application.commands'],
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
import { memberPassesAccessControl } from '@roleypoly/api/utils/access-control';
|
import { memberPassesAccessControl } from '@roleypoly/api/utils/access-control';
|
||||||
import { accessControlViolation } from '@roleypoly/api/utils/responses';
|
import { accessControlViolation } from '@roleypoly/api/utils/responses';
|
||||||
import { DiscordUser, GuildSlug, PresentableGuild, SessionData } from '@roleypoly/types';
|
import { DiscordUser, GuildSlug, PresentableGuild, SessionData } from '@roleypoly/types';
|
||||||
import { respond, withSession } from '../utils/api-tools';
|
import { respond } from '@roleypoly/worker-utils';
|
||||||
|
import { withSession } from '../utils/api-tools';
|
||||||
import { getGuild, getGuildData, getGuildMember } from '../utils/guild';
|
import { getGuild, getGuildData, getGuildMember } from '../utils/guild';
|
||||||
|
|
||||||
const fail = () => respond({ error: 'guild not found' }, { status: 404 });
|
const fail = () => respond({ error: 'guild not found' }, { status: 404 });
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { SessionData } from '@roleypoly/types';
|
import { SessionData } from '@roleypoly/types';
|
||||||
import { respond, withSession } from '../utils/api-tools';
|
import { respond } from '@roleypoly/worker-utils';
|
||||||
|
import { withSession } from '../utils/api-tools';
|
||||||
|
|
||||||
export const GetSession = withSession((session?: SessionData) => (): Response => {
|
export const GetSession = withSession((session?: SessionData) => (): Response => {
|
||||||
const { user, guilds, sessionID } = session || {};
|
const { user, guilds, sessionID } = session || {};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { GuildSlug } from '@roleypoly/types';
|
import { GuildSlug } from '@roleypoly/types';
|
||||||
import { respond } from '../utils/api-tools';
|
import { respond } from '@roleypoly/worker-utils';
|
||||||
import { getGuild } from '../utils/guild';
|
import { getGuild } from '../utils/guild';
|
||||||
|
|
||||||
export const GetSlug = async (request: Request): Promise<Response> => {
|
export const GetSlug = async (request: Request): Promise<Response> => {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { StateSession } from '@roleypoly/types';
|
import { StateSession } from '@roleypoly/types';
|
||||||
import { getQuery, isAllowedCallbackHost, setupStateSession } from '../utils/api-tools';
|
import { getQuery } from '@roleypoly/worker-utils';
|
||||||
|
import { isAllowedCallbackHost, setupStateSession } from '../utils/api-tools';
|
||||||
import { Bounce } from '../utils/bounce';
|
import { Bounce } from '../utils/bounce';
|
||||||
import { apiPublicURI, botClientID } from '../utils/config';
|
import { apiPublicURI, botClientID } from '../utils/config';
|
||||||
|
|
||||||
|
|
|
@ -5,25 +5,22 @@ import {
|
||||||
SessionData,
|
SessionData,
|
||||||
StateSession,
|
StateSession,
|
||||||
} from '@roleypoly/types';
|
} from '@roleypoly/types';
|
||||||
import KSUID from 'ksuid';
|
|
||||||
import {
|
import {
|
||||||
AuthType,
|
AuthType,
|
||||||
|
discordAPIBase,
|
||||||
discordFetch,
|
discordFetch,
|
||||||
|
userAgent,
|
||||||
|
} from '@roleypoly/worker-utils';
|
||||||
|
import KSUID from 'ksuid';
|
||||||
|
import {
|
||||||
formData,
|
formData,
|
||||||
getStateSession,
|
getStateSession,
|
||||||
isAllowedCallbackHost,
|
isAllowedCallbackHost,
|
||||||
parsePermissions,
|
parsePermissions,
|
||||||
resolveFailures,
|
resolveFailures,
|
||||||
userAgent,
|
|
||||||
} from '../utils/api-tools';
|
} from '../utils/api-tools';
|
||||||
import { Bounce } from '../utils/bounce';
|
import { Bounce } from '../utils/bounce';
|
||||||
import {
|
import { apiPublicURI, botClientID, botClientSecret, uiPublicURI } from '../utils/config';
|
||||||
apiPublicURI,
|
|
||||||
botClientID,
|
|
||||||
botClientSecret,
|
|
||||||
discordAPIBase,
|
|
||||||
uiPublicURI,
|
|
||||||
} from '../utils/config';
|
|
||||||
import { Sessions } from '../utils/kv';
|
import { Sessions } from '../utils/kv';
|
||||||
|
|
||||||
const AuthErrorResponse = (extra?: string) =>
|
const AuthErrorResponse = (extra?: string) =>
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { SessionData } from '@roleypoly/types';
|
import { SessionData } from '@roleypoly/types';
|
||||||
import { formData, respond, userAgent, withSession } from '../utils/api-tools';
|
import { discordAPIBase, respond, userAgent } from '@roleypoly/worker-utils';
|
||||||
import { botClientID, botClientSecret, discordAPIBase } from '../utils/config';
|
import { formData, withSession } from '../utils/api-tools';
|
||||||
|
import { botClientID, botClientSecret } from '../utils/config';
|
||||||
import { Sessions } from '../utils/kv';
|
import { Sessions } from '../utils/kv';
|
||||||
|
|
||||||
export const RevokeSession = withSession(
|
export const RevokeSession = withSession(
|
||||||
|
|
|
@ -10,8 +10,9 @@ import {
|
||||||
SessionData,
|
SessionData,
|
||||||
TransactionType,
|
TransactionType,
|
||||||
} from '@roleypoly/types';
|
} from '@roleypoly/types';
|
||||||
|
import { AuthType, discordFetch, respond } from '@roleypoly/worker-utils';
|
||||||
import { difference, groupBy, keyBy, union } from 'lodash';
|
import { difference, groupBy, keyBy, union } from 'lodash';
|
||||||
import { AuthType, discordFetch, respond, withSession } from '../utils/api-tools';
|
import { withSession } from '../utils/api-tools';
|
||||||
import { botToken } from '../utils/config';
|
import { botToken } from '../utils/config';
|
||||||
import {
|
import {
|
||||||
getGuild,
|
getGuild,
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { Router } from '@roleypoly/worker-utils/router';
|
||||||
import { BotJoin } from './handlers/bot-join';
|
import { BotJoin } from './handlers/bot-join';
|
||||||
import { ClearGuildCache } from './handlers/clear-guild-cache';
|
import { ClearGuildCache } from './handlers/clear-guild-cache';
|
||||||
import { GetPickerData } from './handlers/get-picker-data';
|
import { GetPickerData } from './handlers/get-picker-data';
|
||||||
|
@ -9,7 +10,6 @@ import { RevokeSession } from './handlers/revoke-session';
|
||||||
import { SyncFromLegacy } from './handlers/sync-from-legacy';
|
import { SyncFromLegacy } from './handlers/sync-from-legacy';
|
||||||
import { UpdateGuild } from './handlers/update-guild';
|
import { UpdateGuild } from './handlers/update-guild';
|
||||||
import { UpdateRoles } from './handlers/update-roles';
|
import { UpdateRoles } from './handlers/update-roles';
|
||||||
import { Router } from './router';
|
|
||||||
import { respond } from './utils/api-tools';
|
import { respond } from './utils/api-tools';
|
||||||
import { uiPublicURI } from './utils/config';
|
import { uiPublicURI } from './utils/config';
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
"@roleypoly/misc-utils": "*",
|
"@roleypoly/misc-utils": "*",
|
||||||
"@roleypoly/types": "*",
|
"@roleypoly/types": "*",
|
||||||
"@roleypoly/worker-emulator": "*",
|
"@roleypoly/worker-emulator": "*",
|
||||||
|
"@roleypoly/worker-utils": "*",
|
||||||
"@types/deep-equal": "^1.0.1",
|
"@types/deep-equal": "^1.0.1",
|
||||||
"deep-equal": "^2.0.5",
|
"deep-equal": "^2.0.5",
|
||||||
"ksuid": "^2.0.0",
|
"ksuid": "^2.0.0",
|
||||||
|
|
|
@ -3,10 +3,10 @@ import {
|
||||||
permissions as Permissions,
|
permissions as Permissions,
|
||||||
} from '@roleypoly/misc-utils/hasPermission';
|
} from '@roleypoly/misc-utils/hasPermission';
|
||||||
import { SessionData, UserGuildPermissions } from '@roleypoly/types';
|
import { SessionData, UserGuildPermissions } from '@roleypoly/types';
|
||||||
|
import { Handler, WrappedKVNamespace } from '@roleypoly/worker-utils';
|
||||||
import KSUID from 'ksuid';
|
import KSUID from 'ksuid';
|
||||||
import { Handler } from '../router';
|
import { allowedCallbackHosts, apiPublicURI, rootUsers } from './config';
|
||||||
import { allowedCallbackHosts, apiPublicURI, discordAPIBase, rootUsers } from './config';
|
import { Sessions } from './kv';
|
||||||
import { Sessions, WrappedKVNamespace } from './kv';
|
|
||||||
|
|
||||||
export const formData = (obj: Record<string, any>): string => {
|
export const formData = (obj: Record<string, any>): string => {
|
||||||
return Object.keys(obj)
|
return Object.keys(obj)
|
||||||
|
@ -14,18 +14,8 @@ export const formData = (obj: Record<string, any>): string => {
|
||||||
.join('&');
|
.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 = {}) =>
|
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 =
|
export const resolveFailures =
|
||||||
(
|
(
|
||||||
|
@ -70,44 +60,6 @@ export const getSessionID = (request: Request): { type: string; id: string } | n
|
||||||
return { type, id };
|
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 = {
|
export type CacheLayerOptions = {
|
||||||
skipCachePull?: boolean;
|
skipCachePull?: boolean;
|
||||||
};
|
};
|
||||||
|
@ -195,16 +147,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 => {
|
export const isAllowedCallbackHost = (host: string): boolean => {
|
||||||
return (
|
return (
|
||||||
host === apiPublicURI ||
|
host === apiPublicURI ||
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import { userAgent } from '@roleypoly/api/utils/api-tools';
|
|
||||||
import { uiPublicURI } from '@roleypoly/api/utils/config';
|
import { uiPublicURI } from '@roleypoly/api/utils/config';
|
||||||
import {
|
import {
|
||||||
Category,
|
Category,
|
||||||
|
@ -8,6 +7,7 @@ import {
|
||||||
GuildSlug,
|
GuildSlug,
|
||||||
WebhookValidationStatus,
|
WebhookValidationStatus,
|
||||||
} from '@roleypoly/types';
|
} from '@roleypoly/types';
|
||||||
|
import { userAgent } from '@roleypoly/worker-utils';
|
||||||
import deepEqual from 'deep-equal';
|
import deepEqual from 'deep-equal';
|
||||||
import { sortBy, uniq } from 'lodash';
|
import { sortBy, uniq } from 'lodash';
|
||||||
|
|
||||||
|
|
|
@ -13,5 +13,3 @@ export const apiPublicURI = safeURI(env('API_PUBLIC_URI'));
|
||||||
export const rootUsers = list(env('ROOT_USERS'));
|
export const rootUsers = list(env('ROOT_USERS'));
|
||||||
export const allowedCallbackHosts = list(env('ALLOWED_CALLBACK_HOSTS'));
|
export const allowedCallbackHosts = list(env('ALLOWED_CALLBACK_HOSTS'));
|
||||||
export const importSharedKey = env('BOT_IMPORT_TOKEN');
|
export const importSharedKey = env('BOT_IMPORT_TOKEN');
|
||||||
|
|
||||||
export const discordAPIBase = 'https://discordapp.com/api/v9';
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import { Handler } from '@roleypoly/api/router';
|
|
||||||
import {
|
import {
|
||||||
lowPermissions,
|
lowPermissions,
|
||||||
missingParameters,
|
missingParameters,
|
||||||
|
@ -18,14 +17,8 @@ import {
|
||||||
SessionData,
|
SessionData,
|
||||||
UserGuildPermissions,
|
UserGuildPermissions,
|
||||||
} from '@roleypoly/types';
|
} from '@roleypoly/types';
|
||||||
import {
|
import { AuthType, discordFetch, Handler } from '@roleypoly/worker-utils';
|
||||||
AuthType,
|
import { cacheLayer, CacheLayerOptions, isRoot, withSession } from './api-tools';
|
||||||
cacheLayer,
|
|
||||||
CacheLayerOptions,
|
|
||||||
discordFetch,
|
|
||||||
isRoot,
|
|
||||||
withSession,
|
|
||||||
} from './api-tools';
|
|
||||||
import { botClientID, botToken } from './config';
|
import { botClientID, botToken } from './config';
|
||||||
import { GuildData, Guilds } from './kv';
|
import { GuildData, Guilds } from './kv';
|
||||||
import { useRateLimiter } from './rate-limiting';
|
import { useRateLimiter } from './rate-limiting';
|
||||||
|
|
|
@ -1,83 +1,4 @@
|
||||||
export class WrappedKVNamespace {
|
import { kvOrLocal, WrappedKVNamespace } from '@roleypoly/worker-utils';
|
||||||
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();
|
|
||||||
|
|
||||||
const self = global as any as Record<string, any>;
|
const self = global as any as Record<string, any>;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { respond } from './api-tools';
|
import { respond } from '@roleypoly/worker-utils';
|
||||||
|
|
||||||
export const ok = () => respond({ ok: true });
|
export const ok = () => respond({ ok: true });
|
||||||
|
|
||||||
|
|
5
packages/interactions/handlers/healthz.ts
Normal file
5
packages/interactions/handlers/healthz.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import { respond } from '@roleypoly/worker-utils';
|
||||||
|
|
||||||
|
export const healthz = async (request: Request): Promise<Response> => {
|
||||||
|
return respond({ ok: true });
|
||||||
|
};
|
49
packages/interactions/handlers/interaction.ts
Normal file
49
packages/interactions/handlers/interaction.ts
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
import { helloWorld } from '@roleypoly/interactions/handlers/interactions/hello-world';
|
||||||
|
import { verifyRequest } from '@roleypoly/interactions/utils/interactions';
|
||||||
|
import {
|
||||||
|
InteractionData,
|
||||||
|
InteractionRequest,
|
||||||
|
InteractionRequestCommand,
|
||||||
|
InteractionResponse,
|
||||||
|
InteractionType,
|
||||||
|
} from '@roleypoly/types';
|
||||||
|
import { respond } from '@roleypoly/worker-utils';
|
||||||
|
|
||||||
|
const commands: Record<
|
||||||
|
InteractionData['name'],
|
||||||
|
(request: InteractionRequestCommand) => Promise<InteractionResponse>
|
||||||
|
> = {
|
||||||
|
'hello-world': helloWorld,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const interactionHandler = async (request: Request): Promise<Response> => {
|
||||||
|
if (!(await verifyRequest(request))) {
|
||||||
|
return new Response('invalid request signature', { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const interaction = (await request.json()) as InteractionRequest;
|
||||||
|
|
||||||
|
if (interaction.type === InteractionType.PING) {
|
||||||
|
return respond({ type: 1 });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (interaction.type !== InteractionType.APPLICATION_COMMAND) {
|
||||||
|
return respond({ err: 'not implemented' }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!interaction.data) {
|
||||||
|
return respond({ err: 'data missing' }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const handler = commands[interaction.data.name];
|
||||||
|
if (!handler) {
|
||||||
|
return respond({ err: 'not implemented' }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await handler(interaction as InteractionRequestCommand);
|
||||||
|
return respond(response);
|
||||||
|
} catch (e) {
|
||||||
|
return respond({ err: 'command errored' }, { status: 500 });
|
||||||
|
}
|
||||||
|
};
|
16
packages/interactions/handlers/interactions/hello-world.ts
Normal file
16
packages/interactions/handlers/interactions/hello-world.ts
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
import {
|
||||||
|
InteractionCallbackType,
|
||||||
|
InteractionRequestCommand,
|
||||||
|
InteractionResponse,
|
||||||
|
} from '@roleypoly/types';
|
||||||
|
|
||||||
|
export const helloWorld = async (
|
||||||
|
interaction: InteractionRequestCommand
|
||||||
|
): Promise<InteractionResponse> => {
|
||||||
|
return {
|
||||||
|
type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,
|
||||||
|
data: {
|
||||||
|
content: `Hey there, ${interaction.member?.nick || interaction.user?.username}`,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
27
packages/interactions/index.ts
Normal file
27
packages/interactions/index.ts
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
import { respond } from '@roleypoly/worker-utils';
|
||||||
|
import { Router } from '@roleypoly/worker-utils/router';
|
||||||
|
import { healthz } from './handlers/healthz';
|
||||||
|
import { uiPublicURI } from './utils/config';
|
||||||
|
|
||||||
|
const router = new Router();
|
||||||
|
|
||||||
|
router.add('GET', '/_healthz', healthz);
|
||||||
|
|
||||||
|
// Root Zen <3
|
||||||
|
router.addFallback('root', () => {
|
||||||
|
return respond({
|
||||||
|
__warning: '🦊',
|
||||||
|
this: 'is',
|
||||||
|
a: 'fox-based',
|
||||||
|
web: 'application',
|
||||||
|
please: 'be',
|
||||||
|
mindful: 'of',
|
||||||
|
your: 'surroundings',
|
||||||
|
warning__: '🦊',
|
||||||
|
meta: uiPublicURI,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
addEventListener('fetch', (event: FetchEvent) => {
|
||||||
|
event.respondWith(router.handle(event.request));
|
||||||
|
});
|
15
packages/interactions/package.json
Normal file
15
packages/interactions/package.json
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
{
|
||||||
|
"name": "@roleypoly/interactions",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"scripts": {
|
||||||
|
"build": "yarn workspace @roleypoly/worker-emulator build --basePath `pwd`",
|
||||||
|
"lint:types": "tsc --noEmit",
|
||||||
|
"start": "cfw-emulator"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@cloudflare/workers-types": "^2.2.2",
|
||||||
|
"@roleypoly/types": "*",
|
||||||
|
"@roleypoly/worker-utils": "*",
|
||||||
|
"tweetnacl": "^1.0.3"
|
||||||
|
}
|
||||||
|
}
|
15
packages/interactions/tsconfig.json
Normal file
15
packages/interactions/tsconfig.json
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "./dist",
|
||||||
|
"lib": ["esnext", "webworker", "ES2020.BigInt", "ES2020.Promise"],
|
||||||
|
"types": ["@cloudflare/workers-types"],
|
||||||
|
"target": "ES2019"
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"./*.ts",
|
||||||
|
"./**/*.ts",
|
||||||
|
"../../node_modules/@cloudflare/workers-types/index.d.ts"
|
||||||
|
],
|
||||||
|
"exclude": ["./**/*.spec.ts", "./dist/**"],
|
||||||
|
"extends": "../../tsconfig.json"
|
||||||
|
}
|
10
packages/interactions/utils/config.ts
Normal file
10
packages/interactions/utils/config.ts
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
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'));
|
25
packages/interactions/utils/interactions.ts
Normal file
25
packages/interactions/utils/interactions.ts
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
import { publicKey } from '@roleypoly/interactions/utils/config';
|
||||||
|
import nacl from 'tweetnacl';
|
||||||
|
|
||||||
|
export const verifyRequest = async (request: Request): Promise<boolean> => {
|
||||||
|
const timestamp = request.headers.get('x-signature-timestamp');
|
||||||
|
const signature = request.headers.get('x-signature-ed25519');
|
||||||
|
|
||||||
|
if (!timestamp || !signature) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = await request.json();
|
||||||
|
|
||||||
|
if (
|
||||||
|
!nacl.sign.detached.verify(
|
||||||
|
Buffer.from(timestamp + JSON.stringify(body)),
|
||||||
|
Buffer.from(signature, 'hex'),
|
||||||
|
Buffer.from(publicKey, 'hex')
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
28
packages/interactions/webpack.config.js
Normal file
28
packages/interactions/webpack.config.js
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const mode = process.env.NODE_ENV || 'production';
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
target: 'webworker',
|
||||||
|
entry: path.join(__dirname, 'index.ts'),
|
||||||
|
output: {
|
||||||
|
filename: `worker.${mode}.js`,
|
||||||
|
path: path.join(__dirname, 'dist'),
|
||||||
|
},
|
||||||
|
mode,
|
||||||
|
resolve: {
|
||||||
|
extensions: ['.ts', '.tsx', '.js'],
|
||||||
|
},
|
||||||
|
module: {
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
test: /\.tsx?$/,
|
||||||
|
loader: 'ts-loader',
|
||||||
|
options: {
|
||||||
|
transpileOnly: true,
|
||||||
|
configFile: path.join(__dirname, 'tsconfig.json'),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
7
packages/interactions/worker.config.js
Normal file
7
packages/interactions/worker.config.js
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
const reexportEnv = (keys = []) => {
|
||||||
|
return keys.reduce((acc, key) => ({ ...acc, [key]: process.env[key] }), {});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
environment: reexportEnv(['DISCORD_PUBLIC_KEY', 'UI_PUBLIC_URI', 'API_PUBLIC_URI']),
|
||||||
|
};
|
56
packages/types/Interactions.ts
Normal file
56
packages/types/Interactions.ts
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
import { DiscordUser, Member } from '@roleypoly/types/User';
|
||||||
|
|
||||||
|
export enum InteractionType {
|
||||||
|
PING = 1,
|
||||||
|
APPLICATION_COMMAND = 2,
|
||||||
|
MESSAGE_COMPONENT = 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
export type InteractionRequest = {
|
||||||
|
id: string;
|
||||||
|
application_id: string;
|
||||||
|
token: string;
|
||||||
|
version: 1;
|
||||||
|
type: InteractionType;
|
||||||
|
data?: InteractionData;
|
||||||
|
guild_id?: string;
|
||||||
|
channel_id?: string;
|
||||||
|
member?: Member;
|
||||||
|
user?: DiscordUser;
|
||||||
|
message?: {};
|
||||||
|
};
|
||||||
|
|
||||||
|
export type InteractionRequestCommand = InteractionRequest & {
|
||||||
|
data: InteractionData;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type InteractionData = {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
resolved?: {};
|
||||||
|
options?: {}[];
|
||||||
|
custom_id: string;
|
||||||
|
component_type: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export enum InteractionCallbackType {
|
||||||
|
PONG = 1,
|
||||||
|
CHANNEL_MESSAGE_WITH_SOURCE = 4,
|
||||||
|
DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5,
|
||||||
|
DEFERRED_UPDATE_MESSAGE = 6,
|
||||||
|
UPDATE_MESSAGE = 7,
|
||||||
|
}
|
||||||
|
|
||||||
|
export type InteractionCallbackData = {
|
||||||
|
tts?: boolean;
|
||||||
|
content?: string;
|
||||||
|
embeds?: {};
|
||||||
|
allowed_mentions?: {};
|
||||||
|
flags?: number;
|
||||||
|
components?: {}[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type InteractionResponse = {
|
||||||
|
type: InteractionCallbackType;
|
||||||
|
data?: InteractionCallbackData;
|
||||||
|
};
|
|
@ -1,5 +1,6 @@
|
||||||
export * from './Category';
|
export * from './Category';
|
||||||
export * from './Guild';
|
export * from './Guild';
|
||||||
|
export * from './Interactions';
|
||||||
export * from './Role';
|
export * from './Role';
|
||||||
export * from './Session';
|
export * from './Session';
|
||||||
export * from './User';
|
export * from './User';
|
||||||
|
|
21
packages/worker-utils/api.ts
Normal file
21
packages/worker-utils/api.ts
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
export const respond = (obj: Record<string, any>, init: ResponseInit = {}) =>
|
||||||
|
new Response(JSON.stringify(obj), {
|
||||||
|
...init,
|
||||||
|
headers: {
|
||||||
|
...(init.headers || {}),
|
||||||
|
'content-type': 'application/json',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const userAgent =
|
||||||
|
'DiscordBot (https://github.com/roleypoly/roleypoly, git-main) (+https://roleypoly.com)';
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
38
packages/worker-utils/discord.ts
Normal file
38
packages/worker-utils/discord.ts
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import { userAgent } from './api';
|
||||||
|
|
||||||
|
export const discordAPIBase = 'https://discordapp.com/api/v9';
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
};
|
4
packages/worker-utils/index.ts
Normal file
4
packages/worker-utils/index.ts
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
export * from './api';
|
||||||
|
export * from './discord';
|
||||||
|
export * from './kv';
|
||||||
|
export * from './router';
|
80
packages/worker-utils/kv.ts
Normal file
80
packages/worker-utils/kv.ts
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const kvOrLocal = (namespace: KVNamespace | null): KVNamespace =>
|
||||||
|
namespace || new EmulatedKV();
|
7
packages/worker-utils/package.json
Normal file
7
packages/worker-utils/package.json
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"name": "@roleypoly/worker-utils",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"devDependencies": {
|
||||||
|
"@cloudflare/workers-types": "^2.2.2"
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,3 @@
|
||||||
import { addCORS } from './utils/api-tools';
|
|
||||||
import { uiPublicURI } from './utils/config';
|
|
||||||
|
|
||||||
export type Handler = (request: Request) => Promise<Response> | Response;
|
export type Handler = (request: Request) => Promise<Response> | Response;
|
||||||
|
|
||||||
type RoutingTree = {
|
type RoutingTree = {
|
||||||
|
@ -23,7 +20,11 @@ export class Router {
|
||||||
500: this.serverError,
|
500: this.serverError,
|
||||||
};
|
};
|
||||||
|
|
||||||
private uiURL = new URL(uiPublicURI);
|
private corsOrigins: string[] = [];
|
||||||
|
|
||||||
|
addCORSOrigins(origins: string[]) {
|
||||||
|
this.corsOrigins = [...this.corsOrigins, ...origins];
|
||||||
|
}
|
||||||
|
|
||||||
addFallback(which: keyof Fallbacks, handler: Handler) {
|
addFallback(which: keyof Fallbacks, handler: Handler) {
|
||||||
this.fallbacks[which] = handler;
|
this.fallbacks[which] = handler;
|
||||||
|
@ -40,6 +41,12 @@ export class Router {
|
||||||
}
|
}
|
||||||
|
|
||||||
async handle(request: Request): Promise<Response> {
|
async handle(request: Request): Promise<Response> {
|
||||||
|
const response = await this.processRequest(request);
|
||||||
|
this.injectCORSHeaders(request, response.headers);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async processRequest(request: Request): Promise<Response> {
|
||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
|
|
||||||
if (url.pathname === '/' || url.pathname === '') {
|
if (url.pathname === '/' || url.pathname === '') {
|
||||||
|
@ -60,7 +67,7 @@ export class Router {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lowerMethod === 'options') {
|
if (lowerMethod === 'options') {
|
||||||
return new Response(null, addCORS({}));
|
return new Response(null, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.fallbacks[404](request);
|
return this.fallbacks[404](request);
|
||||||
|
@ -81,4 +88,24 @@ export class Router {
|
||||||
status: 500,
|
status: 500,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private injectCORSHeaders(request: Request, headers: Headers) {
|
||||||
|
headers.set('access-control-allow-methods', '*');
|
||||||
|
headers.set('access-control-allow-headers', '*');
|
||||||
|
|
||||||
|
if (this.corsOrigins.length === 0) {
|
||||||
|
headers.set('access-control-allow-origin', '*');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const originHeader = request.headers.get('origin');
|
||||||
|
if (!originHeader) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const originHostname = new URL(originHeader).hostname;
|
||||||
|
if (this.corsOrigins.includes(originHostname)) {
|
||||||
|
headers.set('access-control-allow-origin', originHostname);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
15
packages/worker-utils/tsconfig.json
Normal file
15
packages/worker-utils/tsconfig.json
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "./dist",
|
||||||
|
"lib": ["esnext", "webworker", "ES2020.BigInt", "ES2020.Promise"],
|
||||||
|
"types": ["@cloudflare/workers-types"],
|
||||||
|
"target": "ES2019"
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"./*.ts",
|
||||||
|
"./**/*.ts",
|
||||||
|
"../../node_modules/@cloudflare/workers-types/index.d.ts"
|
||||||
|
],
|
||||||
|
"exclude": ["./**/*.spec.ts", "./dist/**"],
|
||||||
|
"extends": "../../tsconfig.json"
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue