Created
August 17, 2020 00:00
-
-
Save cborac/0e1096759dbd6503b767eed2c0a0ea18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DataUtil from '../../constants/util/DataUtil'; | |
import REST from './REST'; | |
import { APICHANNEL, APIEMBED, APIGUILD, APIGUILDPREVIEW, APIMEMBER, APIMESSAGE, APIROLE, APIUSER } from '../../constants/Types/Responses'; | |
import Permissions from "../../Structures/Permissions" | |
import { BitFieldResolvable } from '../../Structures/Bitfield'; | |
import Emoji from '../../Structures/Emoji'; | |
import ColorConvertable from '../../Structures/ColorConvertable'; | |
const { stringify: json } = JSON | |
export default class Routes { | |
REST: REST; | |
users: ((id: Snowflake) => Promise<APIUSER>) & { me: { get: () => Promise<APIUSER>; patch: ({ username, avatar }: { username?: string; avatar?: Data; }) => Promise<APIUSER>; guilds: ((id: Snowflake) => { delete: () => Promise<void>; }) & { get: (before?: Snowflake, after?: Snowflake, limit?: number) => Promise<any[]>; }; channels: { post: (recipient_id: Snowflake) => Promise<any>; get: () => Promise<any>; }; }; }; | |
channels: (id: Snowflake) => { get: () => Promise<APICHANNEL>; patch: (options: { name?: string; type?: 0 | 5; position?: number; topic?: string; nsfw?: boolean; rate_limit_per_user?: number; bitrate?: number; user_limit?: number; permission_overwrites?: { id: Snowflake; type: 'role' | 'member'; allow: BitFieldResolvable; deny: BitFieldResolvable; }[]; parent_id: Snowflake; }) => Promise<APICHANNEL>; delete: () => Promise<APICHANNEL>; messages: ((message_id: Snowflake) => any) & any; }; | |
constructor(rest: REST) { | |
this.REST = rest | |
this.users = Object.assign((id: Snowflake): Promise<APIUSER> => this.REST.request(['users', id], 'GET'), { | |
me: { | |
get: (): Promise<APIUSER> => this.REST.request(['users', '@me'], 'GET'), | |
patch: async ({ username, avatar }: { username?: string, avatar?: Data }): Promise<APIUSER> => { | |
if (avatar) avatar = await DataUtil.image(avatar) | |
return this.REST.request(['users', '@me'], 'PATCH', json({ | |
username, | |
avatar | |
})) | |
}, | |
guilds: Object.assign((id: Snowflake) => ({ | |
delete: (): Promise<void> => this.REST.request(['users', '@me', 'guilds', id], 'DELETE') | |
}), { | |
get: (before?: Snowflake, after?: Snowflake, limit: number = 100): Promise<any[]> => { | |
const query = new URLSearchParams() | |
query.append('before', before) | |
query.append('after', after) | |
query.append('limit', limit.toString()) | |
return this.REST.request(['users', '@me', 'guilds'], 'GET', query, { 'Content-Type': undefined }) | |
} | |
}), | |
channels: { | |
post: (recipient_id: Snowflake): Promise<any> => this.REST.request(['users', '@me', 'guilds'], 'POST', json({ recipient_id })), | |
get: (): Promise<any> => this.REST.request(['users', '@me', 'channels'], 'GET') | |
} | |
}, | |
}) | |
this.channels = (id: Snowflake) => ({ | |
get: (): Promise<APICHANNEL> => this.REST.request(['channels', id], 'GET'), | |
patch: (options: { name?: string, type?: 0 | 5, position?: number, topic?: string, nsfw?: boolean, rate_limit_per_user?: number, bitrate?: number, user_limit?: number, permission_overwrites?: { id: Snowflake, type: 'role' | 'member', allow: BitFieldResolvable, deny: BitFieldResolvable }[], parent_id: Snowflake }): Promise<APICHANNEL> => { | |
if (options.permission_overwrites) options.permission_overwrites = options.permission_overwrites.map(x => ({ id: x.id, type: x.type, allow: new Permissions(x.allow).bitfield, deny: new Permissions(x.deny).bitfield })) | |
return this.REST.request(['channels', id], 'PATCH', json(options)) | |
}, | |
delete: (): Promise<APICHANNEL> => this.REST.request(['channels', id], 'DELETE'), | |
messages: Object.assign((message_id: Snowflake) => ({ | |
reactions: Object.assign((emoji: Emoji) => { | |
const _e_ = encodeURIComponent(emoji.unicode ? emoji.name : emoji.name + ':' + emoji.id) | |
return Object.assign((userid: Snowflake) => ({ | |
delete: () => this.REST.request(['channels', id, 'messages', message_id, 'reactions', _e_, userid], 'DELETE'), | |
}), | |
{ | |
me: { | |
get: () => this.REST.request(['channels', id, 'messages', message_id, 'reactions', _e_, '@me'], 'PUT'), | |
delete: () => this.REST.request(['channels', id, 'messages', message_id, 'reactions', _e_, '@me'], 'PUT') | |
}, | |
get: (before?: Snowflake, after?: Snowflake, limit?: _1_100_) => { | |
const query = new URLSearchParams() | |
query.set('before', before) | |
query.set('after', after) | |
query.set('limit', limit.toString()) | |
this.REST.request(['channels', id, 'messages', message_id, 'reactions', _e_], 'DELETE', query, { 'Content-Type': undefined }) | |
}, | |
delete: () => this.REST.request(['channels', id, 'messages', message_id, 'reactions', _e_], 'DELETE'), | |
} | |
) | |
}, | |
{ | |
delete: () => this.REST.request(['channels', id, 'messages', message_id, 'reactions'], 'DELETE') | |
}), | |
patch: (opts: { content?: string, embed?: APIEMBED }) => this.REST.request(['channels', id, 'messages', message_id], 'PATCH', json(opts)), | |
delete: () => this.REST.request(['channels', id, 'messages', message_id], 'DELETE') | |
}), { | |
get: ({ around, before, after, limit }: { around?: Snowflake, before?: Snowflake, after?: Snowflake, limit?: _1_100_ }): Promise<APIMESSAGE[]> => { | |
const query = new URLSearchParams() | |
query.set('around', around) | |
query.set('before', before) | |
query.set('after', after) | |
query.set('limit', limit.toString()) | |
return this.REST.request(['channels', id, 'messages'], 'GET', query, { 'Content-Type': undefined }) | |
}, | |
post: ({ content, nonce, tts, file, embed }: { content: string, nonce: string | number, tts: boolean, file?: File, embed?: APIEMBED }): Promise<APIMESSAGE> => { | |
if (file) { | |
const payload_json = json({ content, nonce, tts, embed }) | |
const form = new FormData() | |
form.append('payload_json', payload_json) | |
form.append('file', file) | |
return this.REST.request(['channels', id, 'messages'], 'POST', form, { 'Content-Type': 'multipart/form-data', 'Content-Disposition': file.name }) | |
} else return this.REST.request(['channels', id, 'messages'], 'POST', json({ content, nonce, tts, embed })) | |
}, | |
bulk_delete: { | |
post: (messages: Snowflake[]): Promise<void> => this.REST.request(['channels', id, 'messages', 'bulk-delete'], 'POST', json({ messages })) | |
} | |
}) | |
}) | |
this.guilds = (id: Snowflake) => ({ | |
get: (): Promise<APIGUILD> => this.REST.request(['guilds', id], 'GET'), | |
preview: { | |
get: (): Promise<APIGUILDPREVIEW> => this.REST.request(['guilds', id, 'preview'], 'GET') | |
}, | |
patch: (guild: APIGUILD): Promise<APIGUILD> => this.REST.request(['guilds', id], 'PATCH', json(guild)), | |
delete: (): Promise<void> => this.REST.request(['guilds', id], 'DELETE'), | |
channels: { | |
get: (): Promise<APICHANNEL[]> => this.REST.request(['guilds', id, 'channels'], 'GET'), | |
post: (channel: APICHANNEL): Promise<APICHANNEL> => this.REST.request(['guilds', id, 'channels'], 'POST', json(channel)), | |
patch: ({ id }: { id: Snowflake }, position: number) => this.REST.request(['guilds', id, 'channels'], 'PATCH', json([id, position])) | |
}, | |
members: Object.assign((member_id: Snowflake) => ({ | |
put: (options: { access_token: string, nick?: string, roles?: Snowflake[], mute?: boolean, deaf?: boolean }): Promise<APIMEMBER> => this.REST.request(['guilds', id, 'members', member_id], 'PUT', json(options)), | |
patch: (options: { nick: string, roles: Snowflake[], mute: boolean, deaf: boolean, channel_id: Snowflake }): Promise<void> => this.REST.request(['guilds', id, 'members', member_id], 'PATCH', json(options)), | |
delete: (): Promise<void> => this.REST.request(['guilds', id, 'members', member_id], 'DELETE'), | |
roles: (role_id: Snowflake) => ({ | |
put: (): Promise<void> => this.REST.request(['guilds', id, 'members', member_id, 'roles', role_id], 'PUT'), | |
delete: (): Promise<void> => this.REST.request(['guilds', id, 'members', member_id, 'roles', role_id], 'DELETE'), | |
}) | |
}), { | |
get: (options: { limit: string, after: Snowflake }): Promise<APIMEMBER> => this.REST.request(['guilds', id, 'members'], 'GET', new URLSearchParams(options)), | |
me: (nick: string): Promise<string> => this.REST.request(['guilds', id, 'members', '@me', 'nick'], 'PATCH', json({ nick })) | |
}), | |
roles: Object.assign((role_id: Snowflake) => ({ | |
delete: (): Promise<void> => this.REST.request(['guilds', id, 'roles', role_id], 'DELETE') | |
}), { | |
get: (): Promise<APIROLE[]> => this.REST.request(['guilds', id, 'roles'], 'GET'), | |
post: (options: { name: string, permissions: BitFieldResolvable, color: ColorConvertable, hoist: boolean, mentionable: boolean }): Promise<APIROLE> => this.REST.request(['guilds', id, 'roles'], 'POST', json(options)), | |
patch: (options: { id: Snowflake, position: number }): Promise<APIROLE[]> => this.REST.request(['guilds', id, 'roles'], 'PATCH', json(options)), | |
}), | |
bans: Object.assign((user_id: Snowflake) => ({ | |
get: (): Promise<{ reason?: string, user: APIUSER }> => this.REST.request(['guilds', id, 'bans', user_id], 'GET'), | |
put: (): Promise<void> => this.REST.request(['guilds', id, 'bans', user_id], 'PUT') | |
}), { | |
get: (): Promise<{ reason?: string, user: APIUSER }[]> => this.REST.request(['guilds', id, 'bans'], 'GET') | |
}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment