mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-04-24 11:29:12 +00:00
* 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
38 lines
791 B
TypeScript
38 lines
791 B
TypeScript
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;
|
|
}
|
|
};
|