diff --git a/.eslintrc.js b/.eslintrc.js index 910b2d3..200257b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -12,8 +12,11 @@ module.exports = { parser: '@typescript-eslint/parser', parserOptions: { ecmaVersion: 12, - sourceType: 'module' + sourceType: 'module', + project: 'tsconfig.json' }, plugins: ['@typescript-eslint'], - rules: {} + rules: { + '@typescript-eslint/restrict-template-expressions': 'off' + } } diff --git a/src/consts/urlsAndVersions.ts b/src/consts/urlsAndVersions.ts index 128dc52..4c3c239 100644 --- a/src/consts/urlsAndVersions.ts +++ b/src/consts/urlsAndVersions.ts @@ -1,7 +1,7 @@ -export const DISCORD_API_URL = 'https://discord.com/api' +export const DISCORD_API_URL: string = 'https://discord.com/api' -export const DISCORD_GATEWAY_URL = 'wss://gateway.discord.gg' +export const DISCORD_GATEWAY_URL: string = 'wss://gateway.discord.gg' -export const DISCORD_CDN_URL = 'https://cdn.discordapp.com' +export const DISCORD_CDN_URL: string = 'https://cdn.discordapp.com' -export const DISCORD_API_VERSION = 8 +export const DISCORD_API_VERSION: number = 8 diff --git a/src/models/cache.ts b/src/models/cache.ts index 5a2c0c8..10fe16c 100644 --- a/src/models/cache.ts +++ b/src/models/cache.ts @@ -1,6 +1,6 @@ let caches: any = {} -const get = (cacheName: string, key: string) => { +const get = (cacheName: string, key: string): any => { const gotCache: Map = caches[cacheName] if (gotCache === undefined || !(gotCache instanceof Map)) { return undefined @@ -10,7 +10,7 @@ const get = (cacheName: string, key: string) => { return gotMap } -const set = (cacheName: string, key: string, value: any) => { +const set = (cacheName: string, key: string, value: any): any => { let gotCache: Map = caches[cacheName] if (gotCache === undefined || !(gotCache instanceof Map)) { gotCache = caches[cacheName] = new Map() @@ -21,7 +21,7 @@ const set = (cacheName: string, key: string, value: any) => { return value } -const del = (cacheName: string, key: string) => { +const del = (cacheName: string, key: string): boolean | undefined => { const gotCache: Map = caches[cacheName] if (gotCache === undefined || !(gotCache instanceof Map)) { return @@ -30,16 +30,17 @@ const del = (cacheName: string, key: string) => { return gotCache.delete(key) } -const deleteCache = (cacheName: string) => { +const deleteCache = (cacheName: string): void => { const gotCache = caches[cacheName] if (gotCache === undefined) { return } + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete caches[cacheName] } -const resetCaches = () => { +const resetCaches = (): void => { caches = {} } diff --git a/src/models/client.ts b/src/models/client.ts index e29c9eb..140cf89 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -2,25 +2,27 @@ import { User } from '../structures/user.ts' import { GatewayIntents } from '../types/gatewayTypes.ts' import { Gateway } from './gateway.ts' import { Rest } from './rest.ts' - +import EventEmitter from 'https://deno.land/std@0.74.0/node/events.ts' /** * Discord Client. */ -export class Client { +export class Client extends EventEmitter { gateway?: Gateway rest?: Rest user?: User ping = 0 token?: string - constructor () {} + // constructor () { + // super() + // } /** * This function is used for connect to discord. * @param token Your token. This is required. * @param intents Gateway intents in array. This is required. */ - connect (token: string, intents: GatewayIntents[]) { + connect (token: string, intents: GatewayIntents[]): void { this.token = token this.gateway = new Gateway(this, token, intents) } diff --git a/src/models/gateway.ts b/src/models/gateway.ts index e85e8aa..045aeee 100644 --- a/src/models/gateway.ts +++ b/src/models/gateway.ts @@ -14,6 +14,14 @@ import { GuildPayload } from '../types/guildTypes.ts' import { User } from '../structures/user.ts' import * as cache from './cache.ts' import { Guild } from '../structures/guild.ts' +import { Channel } from '../structures/channel.ts' +import { ChannelTypes } from '../types/channelTypes.ts' +import { DMChannel } from '../structures/dmChannel.ts' +import { GroupDMChannel } from '../structures/groupChannel.ts' +import { GuildTextChannel } from '../structures/guildTextChannel.ts' +import { VoiceChannel } from '../structures/guildVoiceChannel.ts' +import { CategoryChannel } from '../structures/guildCategoryChannel.ts' +import { NewsChannel } from '../structures/guildNewsChannel.ts' /** * Handles Discord gateway connection. @@ -27,13 +35,12 @@ class Gateway { intents: GatewayIntents[] connected = false initialized = false - heartbeatInterval = 0 - heartbeatIntervalID?: number - heartbeatCheckerIntervalID?: number - sequenceID?: number - sessionID?: string + private heartbeatInterval = 0 + private heartbeatIntervalID?: number + private sequenceID?: number + private sessionID?: string lastPingTimestemp = 0 - heartbeatServerResponded = false + private heartbeatServerResponded = false client: Client constructor (client: Client, token: string, intents: GatewayIntents[]) { @@ -41,6 +48,7 @@ class Gateway { this.intents = intents this.client = client this.websocket = new WebSocket( + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `${DISCORD_GATEWAY_URL}/?v=${DISCORD_API_VERSION}&encoding=json`, [] ) @@ -51,11 +59,11 @@ class Gateway { this.websocket.onerror = this.onerror.bind(this) } - private onopen () { + private onopen (): void { this.connected = true } - private onmessage (event: MessageEvent) { + private onmessage (event: MessageEvent): void { let data = event.data if (data instanceof ArrayBuffer) { data = new Uint8Array(data) @@ -75,7 +83,6 @@ class Gateway { this.heartbeatServerResponded = false } else { clearInterval(this.heartbeatIntervalID) - clearInterval(this.heartbeatCheckerIntervalID) this.websocket.close() this.initWebsocket() return @@ -94,16 +101,7 @@ class Gateway { this.sendIdentify() this.initialized = true } else { - this.websocket.send( - JSON.stringify({ - op: GatewayOpcodes.RESUME, - d: { - token: this.token, - session_id: this.sessionID, - seq: this.sequenceID - } - }) - ) + this.sendResume() } break @@ -113,7 +111,13 @@ class Gateway { break case GatewayOpcodes.INVALID_SESSION: - setTimeout(this.sendIdentify, 3000) + // Because we know this gonna be bool + // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions + if (!d) { + setTimeout(this.sendResume, 3000) + } else { + setTimeout(this.sendIdentify, 3000) + } break case GatewayOpcodes.DISPATCH: @@ -126,9 +130,86 @@ class Gateway { this.client.user = new User(this.client, d.user) this.sessionID = d.session_id d.guilds.forEach((guild: GuildPayload) => { - cache.set('guilds', guild.id, new Guild(this.client, guild)) + Guild.autoInit(this.client, { + endpoint: 'guild', + restURLfuncArgs: [guild.id] + }) }) + this.client.emit('ready') break + case GatewayEvents.Channel_Create: { + let channel: Channel | undefined + switch (d.type) { + case ChannelTypes.DM: + channel = new DMChannel(this.client, d) + break + case ChannelTypes.GROUP_DM: + channel = new GroupDMChannel(this.client, d) + break + case ChannelTypes.GUILD_TEXT: + channel = new GuildTextChannel(this.client, d) + break + case ChannelTypes.GUILD_VOICE: + channel = new VoiceChannel(this.client, d) + break + case ChannelTypes.GUILD_CATEGORY: + channel = new CategoryChannel(this.client, d) + break + case ChannelTypes.GUILD_NEWS: + channel = new NewsChannel(this.client, d) + break + default: + break + } + + if (channel !== undefined) { + cache.set('channel', channel.id, channel) + this.client.emit('channelCreate', channel) + } + break + } + case GatewayEvents.Channel_Update: { + const oldChannel: Channel = cache.get('channel', d.id) + + if (oldChannel.type !== d.type) { + let channel: Channel = oldChannel + switch (d.type) { + case ChannelTypes.DM: + channel = new DMChannel(this.client, d) + break + case ChannelTypes.GROUP_DM: + channel = new GroupDMChannel(this.client, d) + break + case ChannelTypes.GUILD_TEXT: + channel = new GuildTextChannel(this.client, d) + break + case ChannelTypes.GUILD_VOICE: + channel = new VoiceChannel(this.client, d) + break + case ChannelTypes.GUILD_CATEGORY: + channel = new CategoryChannel(this.client, d) + break + case ChannelTypes.GUILD_NEWS: + channel = new NewsChannel(this.client, d) + break + default: + break + } + cache.set('channel', channel.id, channel) + this.client.emit('channelUpdate', oldChannel, channel) + } else { + const before = oldChannel.refreshFromData(d) + this.client.emit('channelUpdate', before, oldChannel) + } + break + } + case GatewayEvents.Channel_Delete: { + const channel: Channel = cache.get('channel', d.id) + cache.del('channel', d.id) + + this.client.emit('channelDelete', channel) + break + } default: break } @@ -138,17 +219,18 @@ class Gateway { } } - private onclose (event: CloseEvent) { + private onclose (event: CloseEvent): void { + console.log(event.code) // TODO: Handle close event codes. } - private onerror (event: Event | ErrorEvent) { + private onerror (event: Event | ErrorEvent): void { const eventError = event as ErrorEvent console.log(eventError) } - private sendIdentify () { + private sendIdentify (): void { this.websocket.send( JSON.stringify({ op: GatewayOpcodes.IDENTIFY, @@ -176,8 +258,22 @@ class Gateway { ) } - initWebsocket () { + private sendResume (): void { + this.websocket.send( + JSON.stringify({ + op: GatewayOpcodes.RESUME, + d: { + token: this.token, + session_id: this.sessionID, + seq: this.sequenceID + } + }) + ) + } + + initWebsocket (): void { this.websocket = new WebSocket( + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `${DISCORD_GATEWAY_URL}/?v=${DISCORD_API_VERSION}&encoding=json`, [] ) @@ -188,7 +284,7 @@ class Gateway { this.websocket.onerror = this.onerror.bind(this) } - close () { + close (): void { this.websocket.close(1000) } } diff --git a/src/models/rest.ts b/src/models/rest.ts index cfff658..2c38e62 100644 --- a/src/models/rest.ts +++ b/src/models/rest.ts @@ -5,7 +5,7 @@ class Rest { constructor (client: Client) { this.client = client } - //TODO: make endpoints function + // TODO: make endpoints function } export { Rest } diff --git a/src/structures/base.ts b/src/structures/base.ts index ede3340..e25df91 100644 --- a/src/structures/base.ts +++ b/src/structures/base.ts @@ -1,63 +1,113 @@ import { Client } from '../models/client.ts' import * as cache from '../models/cache.ts' -import endpoint from '../types/endpoint.ts' +import endpoints from '../types/endpoint.ts' interface IInit { useCache?: boolean - cacheName: string - endpoint: string, + endpoint: string restURLfuncArgs: string[] } export class Base { client: Client + static cacheName?: string + propertyConverterOverride: { [k: string]: string } = {} static useCache?: boolean = true - static restFunc?: ((...restURLfuncArgs: string[]) => string) + static restFunc?: (...restURLfuncArgs: string[]) => string constructor (client: Client, _data?: any) { this.client = client } - static async autoInit (client: Client, init: IInit) { - this.useCache = init.useCache; - const cacheID = init.restURLfuncArgs.join(':') - if (this.useCache) { - const cached = cache.get( - init.cacheName, - cacheID - ) + static async autoInit ( + client: Client, + { useCache, endpoint, restURLfuncArgs }: IInit + ): Promise { + this.useCache = useCache + const cacheID = restURLfuncArgs.join(':') + if (this.useCache !== undefined) { + const cached = cache.get(this.cacheName ?? this.name, cacheID) if (cached !== undefined) { return cached } } - this.restFunc = endpoint.find(v => v.name === init.endpoint) + this.restFunc = endpoints.find(v => v.name === endpoint) // TODO: Make error for this - if(this.restFunc) { - const resp = await fetch(this.restFunc(...init.restURLfuncArgs), { + if (this.restFunc !== undefined) { + const resp = await fetch(this.restFunc(...restURLfuncArgs), { headers: { Authorization: `Bot ${client.token}` } }) const jsonParsed = await resp.json() - cache.set(init.cacheName, cacheID, new this(client, jsonParsed)) - + cache.set( + this.cacheName ?? this.name, + cacheID, + new this(client, jsonParsed) + ) + return new this(client, jsonParsed) } } - async refresh (client: Client, init: IInit) { - const restFunc: ((...restURLfuncArgs: string[]) => string) | undefined = endpoint.find(v => v.name === init.endpoint) + async refreshFromAPI ( + client: Client, + { endpoint, restURLfuncArgs }: IInit + ): Promise { + const oldOne = Object.assign(Object.create(this), this) + const restFunc: + | ((...restURLfuncArgs: string[]) => string) + | undefined = endpoints.find(v => v.name === endpoint) // TODO: Make error for this - if(restFunc) { - const resp = await fetch(restFunc(...init.restURLfuncArgs), { + if (restFunc !== undefined) { + const resp = await fetch(restFunc(...restURLfuncArgs), { headers: { Authorization: `Bot ${client.token}` } }) const jsonParsed = await resp.json() - - Object.assign(this, jsonParsed) + const result: { [k: string]: any } = {} + Object.keys(jsonParsed).forEach(key => { + result[this.convertPropertyNameToStandard(key)] = jsonParsed[key] + }) + + Object.assign(this, result) } + + return oldOne } + + refreshFromData (data: { [k: string]: any }): this { + const oldOne = Object.assign(Object.create(this), this) + const result: { [k: string]: any } = {} + Object.keys(data).forEach(key => { + result[this.convertPropertyNameToStandard(key)] = data[key] + }) + + Object.assign(this, result) + return oldOne + } + + convertPropertyNameToStandard (name: string): string { + if (name in this.propertyConverterOverride) { + return this.propertyConverterOverride[name] + } + + name = name.replaceAll('_id', 'ID') + name = name + .split('_') + .map((value, index) => { + if (index !== 0) { + value = value[0].toUpperCase() + value.slice(1) + } + return value + }) + .join('') + return name + } + + // toJSON() {} } + +// 오류를 해결하기 위해 저는 2개로 접속하겠습니다. VS2019 diff --git a/src/structures/cdn.ts b/src/structures/cdn.ts index a56520c..b76ab18 100644 --- a/src/structures/cdn.ts +++ b/src/structures/cdn.ts @@ -1,11 +1,11 @@ -import { ImageFormats, ImageSize } from "../types/cdnTypes.ts"; +import { ImageFormats, ImageSize } from '../types/cdnTypes.ts' export const ImageURL = ( - url: string, - format: ImageFormats, - size?: ImageSize | 128 -) => { - if (url.includes('a_')) { - return url + '.gif' + '?size=' + size - } else return url + '.' + format + '?size=' + size -} \ No newline at end of file + url: string, + format: ImageFormats, + size?: ImageSize | 128 +): string => { + if (url.includes('a_')) { + return `${url}.gif?size=${size}` + } else return `${url}.${format}?size=${size}` +} diff --git a/src/structures/channel.ts b/src/structures/channel.ts index 2f12522..f72334a 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -1,35 +1,18 @@ import { Client } from '../models/client.ts' -import { - ChannelPayload, - GuildChannelCategoryPayload, - GuildNewsChannelPayload, - GuildTextChannelPayload, - GuildVoiceChannelPayload, - DMChannelPayload, - GroupDMChannelPayload, - ChannelTypes -} from '../types/channelTypes.ts' +import { ChannelPayload, ChannelTypes } from '../types/channelTypes.ts' import { Base } from './base.ts' -import { CategoryChannel } from './guildCategoryChannel.ts' -import { VoiceChannel } from './guildVoiceChannel.ts' -import { NewsChannel } from './guildnewsChannel.ts' -import { DMChannel } from './dmChannel.ts' -import { GroupDMChannel } from './groupChannel.ts' -import { TextChannel } from './textChannel.ts' export class Channel extends Base { type: ChannelTypes id: string static cacheName = 'channel' - static cacheArgIndex = 0 + get mention (): string { + return `<#${this.id}>` + } constructor (client: Client, data: ChannelPayload) { super(client, data) this.type = data.type this.id = data.id } - - get mention () { - return `<#${this.id}>` - } } diff --git a/src/structures/embed.ts b/src/structures/embed.ts index 8ab5ee7..4bc2757 100644 --- a/src/structures/embed.ts +++ b/src/structures/embed.ts @@ -1,5 +1,4 @@ import { Client } from '../models/client.ts' -import { Base } from './base.ts' import { EmbedAuthor, EmbedField, @@ -27,7 +26,7 @@ export class Embed { author?: EmbedAuthor fields?: EmbedField[] constructor (client: Client, data?: EmbedPayload) { - if(data) { + if (data !== undefined) { this.title = data.title this.type = data.type this.description = data.description @@ -43,22 +42,4 @@ export class Embed { this.fields = data.fields } } - - toJSON () { - return { - title: this.title, - type: this.type, - description: this.description, - url: this.url, - timestamp: this.timestamp, - color: this.color, - footer: this.footer, - image: this.image, - thumbnail: this.thumbnail, - video: this.video, - provider: this.provider, - author: this.author, - fields: this.fields - } - } } diff --git a/src/structures/emoji.ts b/src/structures/emoji.ts index 132a77d..4f8ee70 100644 --- a/src/structures/emoji.ts +++ b/src/structures/emoji.ts @@ -1,19 +1,28 @@ import { Client } from '../models/client.ts' import { EmojiPayload } from '../types/emojiTypes.ts' -import { UserPayload } from '../types/userTypes.ts' import { Base } from './base.ts' +import { User } from './user.ts' export class Emoji extends Base { + // eslint-disable-next-line @typescript-eslint/prefer-readonly + private data: EmojiPayload + client: Client id: string name: string roles?: [] - user?: UserPayload + + get user (): User | undefined { + if (this.data.user !== undefined) { + return new User(this.client, this.data.user) + } + } + requireColons?: boolean managed?: boolean animated?: boolean available?: boolean - get CustomEmoji () { + get getEmojiString (): string { if (this.animated === false) { return `<:${this.name}:${this.id}>` } else return `` @@ -21,10 +30,11 @@ export class Emoji extends Base { constructor (client: Client, data: EmojiPayload) { super(client, data) + this.data = data + this.client = client this.id = data.id this.name = data.name this.roles = data.roles - this.user = data.user this.requireColons = data.require_colons this.managed = data.managed this.animated = data.animated diff --git a/src/structures/groupChannel.ts b/src/structures/groupChannel.ts index 245d22c..8ffbb5f 100644 --- a/src/structures/groupChannel.ts +++ b/src/structures/groupChannel.ts @@ -1,9 +1,8 @@ import { Client } from '../models/client.ts' import { GroupDMChannelPayload } from '../types/channelTypes.ts' -import { Base } from "./base.ts" import { Channel } from './channel.ts' -export class GroupDMChannel extends Channel{ +export class GroupDMChannel extends Channel { name: string icon?: string ownerID: string diff --git a/src/structures/guild.ts b/src/structures/guild.ts index 8bf144e..d2f6c9f 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -1,17 +1,12 @@ import { Client } from '../models/client.ts' -import { ChannelPayload } from '../types/channelTypes.ts' -import { EmojiPayload } from '../types/emojiTypes.ts' - -import { - GuildFeatures, - GuildPayload, - MemberPayload -} from '../types/guildTypes.ts' +import { GuildFeatures, GuildPayload } from '../types/guildTypes.ts' import { PresenceUpdatePayload } from '../types/presenceTypes.ts' -import { RolePayload } from '../types/roleTypes.ts' -import { VoiceStatePayload } from '../types/voiceTypes.ts' import { Base } from './base.ts' -import * as cache from '../models/cache.ts' +import { Channel } from './channel.ts' +import { Emoji } from './emoji.ts' +import { Member } from './member.ts' +import { Role } from './role.ts' +import { VoiceState } from './voiceState.ts' export class Guild extends Base { id: string @@ -31,8 +26,8 @@ export class Guild extends Base { verificationLevel: string defaultMessageNotifications: string explicitContentFilter: string - roles: RolePayload[] - emojis: EmojiPayload[] + roles: Role[] + emojis: Emoji[] features: GuildFeatures[] mfaLevel: string applicationID?: string @@ -43,9 +38,9 @@ export class Guild extends Base { large?: boolean unavailable: boolean memberCount?: number - voiceStates?: VoiceStatePayload[] - members?: MemberPayload[] - channels?: ChannelPayload[] + voiceStates?: VoiceState[] + members?: Member[] + channels?: Channel[] presences?: PresenceUpdatePayload[] maxPresences?: number maxMembers?: number @@ -79,8 +74,8 @@ export class Guild extends Base { this.verificationLevel = data.verification_level this.defaultMessageNotifications = data.default_message_notifications this.explicitContentFilter = data.explicit_content_filter - this.roles = data.roles - this.emojis = data.emojis + this.roles = data.roles.map(v => new Role(client, v)) + this.emojis = data.emojis.map(v => new Emoji(client, v)) this.features = data.features this.mfaLevel = data.mfa_level this.systemChannelID = data.system_channel_id @@ -90,9 +85,9 @@ export class Guild extends Base { this.large = data.large this.unavailable = data.unavailable this.memberCount = data.member_count - this.voiceStates = data.voice_states - this.members = data.members - this.channels = data.channels + this.voiceStates = data.voice_states?.map(v => new VoiceState(client, v)) + this.members = data.members?.map(v => new Member(client, v)) + this.channels = data.channels?.map(v => new Channel(client, v)) this.presences = data.presences this.maxPresences = data.max_presences this.maxMembers = data.max_members @@ -107,5 +102,4 @@ export class Guild extends Base { this.approximateNumberCount = data.approximate_number_count this.approximatePresenceCount = data.approximate_presence_count } - } diff --git a/src/structures/guildChannel.ts b/src/structures/guildChannel.ts index 65c52dc..fa39e88 100644 --- a/src/structures/guildChannel.ts +++ b/src/structures/guildChannel.ts @@ -1,8 +1,6 @@ import { Client } from '../models/client.ts' import { GuildChannelPayload, Overwrite } from '../types/channelTypes.ts' import { Channel } from './channel.ts' -import * as cache from '../models/cache.ts' -import { Guild } from './guild.ts' export class GuildChannel extends Channel { guildID: string @@ -21,5 +19,4 @@ export class GuildChannel extends Channel { this.nsfw = data.nsfw this.parentID = data.parent_id } - } diff --git a/src/structures/guildTextChannel.ts b/src/structures/guildTextChannel.ts index 5fe0075..62d160d 100644 --- a/src/structures/guildTextChannel.ts +++ b/src/structures/guildTextChannel.ts @@ -6,7 +6,7 @@ export class GuildTextChannel extends GuildChannel { rateLimit: number topic?: string - get mention () { + get mention (): string { return `<#${this.id}>` } diff --git a/src/structures/guildVoiceChannel.ts b/src/structures/guildVoiceChannel.ts index d95a058..8821aeb 100644 --- a/src/structures/guildVoiceChannel.ts +++ b/src/structures/guildVoiceChannel.ts @@ -1,7 +1,5 @@ import { Client } from '../models/client.ts' import { GuildVoiceChannelPayload, Overwrite } from '../types/channelTypes.ts' -import { Base } from './base.ts' -import { Member } from './member.ts' import { Channel } from './channel.ts' export class VoiceChannel extends Channel { diff --git a/src/structures/guildnewsChannel.ts b/src/structures/guildnewsChannel.ts index 7da22db..51055ba 100644 --- a/src/structures/guildnewsChannel.ts +++ b/src/structures/guildnewsChannel.ts @@ -1,9 +1,9 @@ import { Client } from '../models/client.ts' import { Channel } from './channel.ts' import { GuildNewsChannelPayload } from '../types/channelTypes.ts' -import { Base } from "./base.ts" export class NewsChannel extends Channel { + // eslint-disable-next-line @typescript-eslint/no-useless-constructor constructor (client: Client, data: GuildNewsChannelPayload) { super(client, data) } diff --git a/src/structures/invite.ts b/src/structures/invite.ts index 4739b31..7cfd628 100644 --- a/src/structures/invite.ts +++ b/src/structures/invite.ts @@ -15,7 +15,7 @@ export class Invite extends Base { approximatePresenceCount?: number approximateMemberCount?: number - get link () { + get link (): string { return `https://discord.gg/${this.code}` } diff --git a/src/structures/member.ts b/src/structures/member.ts index e3e64c9..843aa31 100644 --- a/src/structures/member.ts +++ b/src/structures/member.ts @@ -1,9 +1,5 @@ import { Client } from '../models/client.ts' import { MemberPayload } from '../types/guildTypes.ts' -import { RolePayload } from '../types/roleTypes.ts' -import { UserPayload } from '../types/userTypes.ts' -import { Base } from './base.ts' -import { Guild } from './guild.ts' import { Role } from './role.ts' import { User } from './user.ts' diff --git a/src/structures/message.ts b/src/structures/message.ts index d76558c..105b35c 100644 --- a/src/structures/message.ts +++ b/src/structures/message.ts @@ -2,33 +2,44 @@ import { Base } from './base.ts' import { Attachment, ChannelMention, - EmbedPayload, MessageActivity, MessageApplication, + MessageOption, MessagePayload, MessageReference, Reaction } from '../types/channelTypes.ts' import { Client } from '../models/client.ts' -import { UserPayload } from '../types/userTypes.ts' -import { RolePayload } from '../types/roleTypes.ts' +import { User } from './user.ts' +import { Member } from './member.ts' +import { Embed } from './embed.ts' +import { Role } from './role.ts' +import { CHANNEL_MESSAGE } from '../types/endpoint.ts' export class Message extends Base { + // eslint-disable-next-line @typescript-eslint/prefer-readonly + private data: MessagePayload id: string channelID: string guildID?: string - author: UserPayload - member?: any + author: User content: string timestamp: string editedTimestamp?: string tts: boolean + + get member (): Member | undefined { + if (this.data.member !== undefined) { + return new Member(this.client, this.data.member) + } + } + mentionEveryone: boolean - mentions: UserPayload[] - mentionRoles: RolePayload[] + mentions: Member[] + mentionRoles: Role[] mentionChannels?: ChannelMention[] attachments: Attachment[] - embeds: EmbedPayload[] + embeds: Embed[] reactions?: Reaction[] nonce?: string | number pinned: boolean @@ -41,20 +52,20 @@ export class Message extends Base { constructor (client: Client, data: MessagePayload) { super(client) + this.data = data this.id = data.id this.channelID = data.channel_id this.guildID = data.guild_id - this.author = data.author - this.member = data.member + this.author = new User(client, data.author) this.content = data.content this.timestamp = data.timestamp this.editedTimestamp = data.edited_timestamp this.tts = data.tts this.mentionEveryone = data.mention_everyone - this.mentions = data.mentions - this.mentionRoles = data.mention_roles + this.mentions = data.mentions.map(v => new Member(client, v)) + this.mentionRoles = data.mention_roles.map(v => new Role(client, v)) this.attachments = data.attachments - this.embeds = data.embeds + this.embeds = data.embeds.map(v => new Embed(client, v)) this.reactions = data.reactions this.nonce = data.nonce this.pinned = data.pinned @@ -65,4 +76,26 @@ export class Message extends Base { this.messageReference = data.message_reference this.flags = data.flags } + + async editMessage (text?: string, option?: MessageOption): Promise { + if (text !== undefined && option !== undefined) { + throw new Error('Either text or option is necessary.') + } + const resp = await fetch(CHANNEL_MESSAGE(this.channelID, this.id), { + headers: { + Authorization: `Bot ${this.client.token}`, + 'Content-Type': 'application/json' + }, + method: 'PATCH', + body: JSON.stringify({ + content: text, + embed: option?.embed, + file: option?.file, + tts: option?.tts, + allowed_mentions: option?.allowedMention + }) + }) + + return new Message(this.client, await resp.json()) + } } diff --git a/src/structures/role.ts b/src/structures/role.ts index 848c233..3c8fb84 100644 --- a/src/structures/role.ts +++ b/src/structures/role.ts @@ -12,7 +12,7 @@ export class Role extends Base { managed: boolean mentionable: boolean - get mention () { + get mention (): string { return `<@&${this.id}>` } diff --git a/src/structures/snowflake.ts b/src/structures/snowflake.ts index 369a98e..f0788a7 100644 --- a/src/structures/snowflake.ts +++ b/src/structures/snowflake.ts @@ -4,19 +4,19 @@ export class Snowflake { this.snowflake = BigInt.asUintN(64, BigInt(id)) } - get timestamp () { - return ((this.snowflake >> BigInt(22)) + BigInt(1420070400000)).toString() + get timestamp (): string { + return ((this.snowflake >> 22n) + 1420070400000n).toString() } - get workerID () { - return ((this.snowflake & BigInt(0x3e0000)) >> BigInt(17)).toString() + get workerID (): string { + return ((this.snowflake & 0x3e0000n) >> 17n).toString() } - get processID () { - return ((this.snowflake & BigInt(0x1f000)) >> BigInt(12)).toString() + get processID (): string { + return ((this.snowflake & 0x1f00n) >> 12n).toString() } - get increment () { - return (this.snowflake & BigInt(0xfff)).toString() + get increment (): string { + return (this.snowflake & 0xfffn).toString() } } diff --git a/src/structures/textChannel.ts b/src/structures/textChannel.ts index 1bc6873..5280889 100644 --- a/src/structures/textChannel.ts +++ b/src/structures/textChannel.ts @@ -1,17 +1,64 @@ import { Client } from '../models/client.ts' -import { TextChannelPayload } from '../types/channelTypes.ts' -import { Base } from "./base.ts" +import { MessageOption, TextChannelPayload } from '../types/channelTypes.ts' +import { CHANNEL_MESSAGE, CHANNEL_MESSAGES } from '../types/endpoint.ts' import { Channel } from './channel.ts' -import { Embed } from './embed.ts' +import { Message } from './message.ts' + export class TextChannel extends Channel { - lastMessageId?: string + lastMessageID?: string lastPinTimestamp?: string constructor (client: Client, data: TextChannelPayload) { super(client, data) - this.lastMessageId = data.last_message_id + this.lastMessageID = data.last_message_id this.lastPinTimestamp = data.last_pin_timestamp } - send (content: string | Embed, option?: {}) {} //TODO: send function + async send (text?: string, option?: MessageOption): Promise { + if (text !== undefined && option !== undefined) { + throw new Error('Either text or option is necessary.') + } + const resp = await fetch(CHANNEL_MESSAGES(this.id), { + headers: { + Authorization: `Bot ${this.client.token}`, + 'Content-Type': 'application/json' + }, + method: 'POST', + body: JSON.stringify({ + content: text, + embed: option?.embed, + file: option?.file, + tts: option?.tts, + allowed_mentions: option?.allowedMention + }) + }) + + return new Message(this.client, await resp.json()) + } + + async editMessage ( + messageID: string, + text?: string, + option?: MessageOption + ): Promise { + if (text !== undefined && option !== undefined) { + throw new Error('Either text or option is necessary.') + } + const resp = await fetch(CHANNEL_MESSAGE(this.id, messageID), { + headers: { + Authorization: `Bot ${this.client.token}`, + 'Content-Type': 'application/json' + }, + method: 'PATCH', + body: JSON.stringify({ + content: text, + embed: option?.embed, + file: option?.file, + tts: option?.tts, + allowed_mentions: option?.allowedMention + }) + }) + + return new Message(this.client, await resp.json()) + } } diff --git a/src/structures/user.ts b/src/structures/user.ts index 4997982..e34dd74 100644 --- a/src/structures/user.ts +++ b/src/structures/user.ts @@ -1,7 +1,6 @@ import { Client } from '../models/client.ts' import { UserPayload } from '../types/userTypes.ts' import { Base } from './base.ts' -import * as cache from '../models/cache.ts' export class User extends Base { id: string @@ -18,11 +17,11 @@ export class User extends Base { premiumType?: 0 | 1 | 2 publicFlags?: number - get nickMention () { + get nickMention (): string { return `<@!${this.id}>` } - get mention () { + get mention (): string { return `<@${this.id}>` } diff --git a/src/structures/voicestate.ts b/src/structures/voicestate.ts index d19b035..e0f2cc0 100644 --- a/src/structures/voicestate.ts +++ b/src/structures/voicestate.ts @@ -2,7 +2,6 @@ import { Client } from '../models/client.ts' import { MemberPayload } from '../types/guildTypes.ts' import { VoiceStatePayload } from '../types/voiceTypes.ts' import { Base } from './base.ts' -import { Member } from './member.ts' export class VoiceState extends Base { guildID?: string diff --git a/src/test/index.ts b/src/test/index.ts index f4fd13b..92160f2 100644 --- a/src/test/index.ts +++ b/src/test/index.ts @@ -1,34 +1,26 @@ import { Client } from '../models/client.ts' -import { Guild } from '../structures/guild.ts' import { GatewayIntents } from '../types/gatewayTypes.ts' import { TOKEN } from './config.ts' import * as cache from '../models/cache.ts' import { Member } from '../structures/member.ts' +import { Channel } from '../structures/channel.ts' +import { GuildTextChannel } from '../structures/guildTextChannel.ts' const bot = new Client() -bot.connect(TOKEN, [GatewayIntents.GUILD_MEMBERS, GatewayIntents.GUILD_PRESENCES, GatewayIntents.GUILD_MESSAGES]) - - -const member = await Member.autoInit(bot, { - cacheName: 'member', - endpoint: 'GUILD_MEMBER', - restURLfuncArgs: ['', ''] +bot.on('ready', () => { + console.log('READY!') }) -console.log('getted (cached) ' + member.nick) -setInterval(async () => { - //refreshed check - console.log('refreshed check: ' + member.nick) - //cached - console.log('cache: '+( cache.get('member', '')).nick) -}, 10000) -setInterval(async() => { - member.refresh(bot, { - cacheName: 'member', - endpoint: 'GUILD_MEMBER', - restURLfuncArgs: ['', ''] - }) - //refreshed - console.log('refreshed: ' + member.nick) -}, 20000) \ No newline at end of file +bot.on('channelUpdate', (before: Channel, after: Channel) => { + if (before instanceof GuildTextChannel && after instanceof GuildTextChannel) { + console.log(before.name) + console.log(after.name) + } +}) + +bot.connect(TOKEN, [ + GatewayIntents.GUILD_MEMBERS, + GatewayIntents.GUILD_PRESENCES, + GatewayIntents.GUILD_MESSAGES +]) diff --git a/src/types/channelTypes.ts b/src/types/channelTypes.ts index 0872ecc..3471517 100644 --- a/src/types/channelTypes.ts +++ b/src/types/channelTypes.ts @@ -1,3 +1,4 @@ +import { Embed } from '../structures/embed.ts' import { EmojiPayload } from './emojiTypes.ts' import { MemberPayload } from './guildTypes.ts' import { RolePayload } from './roleTypes.ts' @@ -82,7 +83,7 @@ interface MessagePayload { edited_timestamp?: string tts: boolean mention_everyone: boolean - mentions: UserPayload[] + mentions: MemberPayload[] mention_roles: RolePayload[] mention_channels?: ChannelMention[] attachments: Attachment[] @@ -98,6 +99,17 @@ interface MessagePayload { flags?: number } +interface MessageOption { + tts: boolean + embed: Embed + file: Attachment + allowedMention?: { + parse: ['everyone', 'users', 'roles'] + roles: string[] + users: string[] + } +} + interface ChannelMention { id: string guild_id: string @@ -272,6 +284,7 @@ export { MessageApplication, MessageReference, MessagePayload, + MessageOption, EmbedPayload, EmbedTypes, EmbedFooter, diff --git a/src/types/endpoint.ts b/src/types/endpoint.ts index 168fedc..77e2f0e 100644 --- a/src/types/endpoint.ts +++ b/src/types/endpoint.ts @@ -1,185 +1,190 @@ -//Written by Choi Donghan, Catry - import { DISCORD_API_URL, DISCORD_API_VERSION, DISCORD_CDN_URL } from '../consts/urlsAndVersions.ts' -//Guild Endpoints -const GUILDS = () => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds` -const GUILD = (guildID: string) => +// Guild Endpoints +const GUILDS = (): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds` +const GUILD = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}` -const GUILD_AUDIT_LOGS = (guildID: string) => +const GUILD_AUDIT_LOGS = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/audit-logs` -const GUILD_WIDGET = (guildID: string) => +const GUILD_WIDGET = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/widget` -const GUILD_EMOJI = (guildID: string, emoji_id: string) => - `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/emojis/${emoji_id}` -const GUILD_ROLE = (guildID: string, roleID: string) => +const GUILD_EMOJI = (guildID: string, emojiID: string): string => + `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/emojis/${emojiID}` +const GUILD_ROLE = (guildID: string, roleID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/roles/${roleID}` -const GUILD_ROLES = (guildID: string) => +const GUILD_ROLES = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/roles` -const GUILD_INTEGRATION = (guildID: string, integrationID: string) => +const GUILD_INTEGRATION = (guildID: string, integrationID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/integrations/${integrationID}` -const GUILD_INTEGRATIONS = (guildID: string) => +const GUILD_INTEGRATIONS = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/integrations` -const GUILD_INTEGARTION_SYNC = (guildID: string) => +const GUILD_INTEGARTION_SYNC = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/integrations?include_appilications=true` -const GUILD_BAN = (guildID: string, userID: string) => +const GUILD_BAN = (guildID: string, userID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/bans/${userID}` -const GUILD_BANS = (guildID: string) => +const GUILD_BANS = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/bans` -const GUILD_CHANNEL = (channelID: string) => +const GUILD_CHANNEL = (channelID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}` -const GUILD_CHANNELS = (guildID: string, channelID: string) => +const GUILD_CHANNELS = (guildID: string, channelID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/channels` -const GUILD_MEMBER = (guildID: string, memberID: string) => +const GUILD_MEMBER = (guildID: string, memberID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/members/${memberID}` -const GUILD_MEMBERS = (guildID: string) => +const GUILD_MEMBERS = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/members` -const GUILD_MEMBER_ROLE = (guildID: string, memberID: string, roleID: string) => +const GUILD_MEMBER_ROLE = ( + guildID: string, + memberID: string, + roleID: string +): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/members/${memberID}/roles/${roleID}` -const GUILD_INVITES = (guildID: string) => +const GUILD_INVITES = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/invites` -const GUILD_LEAVE = (guildID: string) => +const GUILD_LEAVE = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/users/@me/guilds/${guildID}` -const GUILD_PRUNE = (guildID: string) => +const GUILD_PRUNE = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/prune` -const GUILD_VANITY_URL = (guildID: string) => +const GUILD_VANITY_URL = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/vanity-url` -const GUILD_NICK = (guildID: string) => +const GUILD_NICK = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/members/@me/nick` -const GUILD_WIDGET_IMAGE = (guildID: string) => +const GUILD_WIDGET_IMAGE = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/widget.png` -const GUILD_PREVIEW = (guildID: string) => +const GUILD_PREVIEW = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/preview` -//Channel Endpoints -const CHANNEL = (channelID: string) => +// Channel Endpoints +const CHANNEL = (channelID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}` -const CHANNELS = (channelID: string) => +const CHANNELS = (channelID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${channelID}/channels` -const CHANNEL_MESSAGE = (channelID: string, messageID: string) => +const CHANNEL_MESSAGE = (channelID: string, messageID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/messages/${messageID}` -const CHANNEL_MESSAGES = (channelID: string) => +const CHANNEL_MESSAGES = (channelID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/messages` -const CHANNEL_CROSSPOST = (channelID: string, messageID: string) => +const CHANNEL_CROSSPOST = (channelID: string, messageID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/messages/${messageID}/crosspost` -const MESSAGE_REACTIONS = (channelID: string, messageID: string) => +const MESSAGE_REACTIONS = (channelID: string, messageID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/messages/${messageID}/reactions` const MESSAGE_REACTION = ( channelID: string, messageID: string, emoji: string -) => +): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/messages/${messageID}/reactions/${emoji}` const MESSAGE_REACTION_ME = ( channelID: string, messageID: string, emojiID: string -) => +): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/messages/${messageID}/reactions/${emojiID}/@me` const MESSAGE_REACTION_USER = ( channelID: string, messageID: string, emojiID: string, userID: string -) => +): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/messages/${messageID}/reactions/${emojiID}/${userID}` -const CHANNEL_BULK_DELETE = (channelID: string) => +const CHANNEL_BULK_DELETE = (channelID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/messages/bulk-delete` -const CHANNEL_FOLLOW = (channelID: string) => +const CHANNEL_FOLLOW = (channelID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/followers` -const CHANNEL_INVITES = (channelID: string) => +const CHANNEL_INVITES = (channelID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/invites` -const CHANNEL_PIN = (channelID: string, messageID: string) => +const CHANNEL_PIN = (channelID: string, messageID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/pins/${messageID}` -const CHANNEL_PINS = (channelID: string) => +const CHANNEL_PINS = (channelID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/pins` -const CHANNEL_PERMISSION = (channelID: string, overrideID: string) => +const CHANNEL_PERMISSION = (channelID: string, overrideID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/permissions/${overrideID}` -const CHANNEL_TYPING = (channelID: string) => +const CHANNEL_TYPING = (channelID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/typing` -const GROUP_RECIPIENT = (channelID: string, userID: string) => +const GROUP_RECIPIENT = (channelID: string, userID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/recipient/${userID}` -//User Endpoints -const CURRENT_USER = () => +// User Endpoints +const CURRENT_USER = (): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/users/@me` -const CURRENT_USER_GUILDS = () => +const CURRENT_USER_GUILDS = (): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/users/@me/guilds` -const USER_DM = () => +const USER_DM = (): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/users/@me/channels` -const USER_CONNECTIONS = () => +const USER_CONNECTIONS = (): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/users/@me/connections` -const LEAVE_GUILD = (guildID: string) => +const LEAVE_GUILD = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/users/@me/guilds/${guildID}` -const USER = (userID: string) => +const USER = (userID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/users/${userID}` -//Webhook Endpoints -const CHANNEL_WEBHOOKS = (channelID: string) => +// Webhook Endpoints +const CHANNEL_WEBHOOKS = (channelID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/webhooks` -const GUILD_WEBHOOK = (guildID: string) => +const GUILD_WEBHOOK = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/webhooks` -const WEBHOOK = (webhookID: string) => +const WEBHOOK = (webhookID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/webhooks/${webhookID}` -const WEBHOOK_WITH_TOKEN = (webhookID: string, webhookTOKEN: string) => +const WEBHOOK_WITH_TOKEN = (webhookID: string, webhookTOKEN: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/webhooks/${webhookID}/${webhookTOKEN}` -const SLACK_WEBHOOK = (webhookID: string, webhookTOKEN: string) => +const SLACK_WEBHOOK = (webhookID: string, webhookTOKEN: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/webhooks/${webhookID}/${webhookTOKEN}/slack` -const GITHUB_WEBHOOK = (webhookID: string, webhookTOKEN: string) => +const GITHUB_WEBHOOK = (webhookID: string, webhookTOKEN: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/webhooks/${webhookID}/${webhookTOKEN}/github` -//Gateway Endpoints -const GATEWAY = () => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/gateway` -const GATEWAY_BOT = () => +// Gateway Endpoints +const GATEWAY = (): string => + `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/gateway` +const GATEWAY_BOT = (): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/gateway/bot` -//CDN Endpoints -const CUSTOM_EMOJI = (emojiID: string) => `${DISCORD_CDN_URL}/emojis/${emojiID}` -const GUILD_ICON = (guildID: string, iconID: string) => +// CDN Endpoints +const CUSTOM_EMOJI = (emojiID: string): string => + `${DISCORD_CDN_URL}/emojis/${emojiID}` +const GUILD_ICON = (guildID: string, iconID: string): string => `${DISCORD_CDN_URL}/icons/${guildID}/${iconID}` -const GUILD_SPLASH = (guildID: string, guildSPLASH: string) => +const GUILD_SPLASH = (guildID: string, guildSPLASH: string): string => `${DISCORD_CDN_URL}/splashes/${guildID}/${guildSPLASH}` const GUILD_DISCOVERY_SPLASH = ( guildID: string, guildDiscoverySplash: string -) => `${DISCORD_CDN_URL}/discovery-splashes/${guildID}/${guildDiscoverySplash}` -const GUILD_BANNER = (guildID: string, guildBANNER: string) => +): string => + `${DISCORD_CDN_URL}/discovery-splashes/${guildID}/${guildDiscoverySplash}` +const GUILD_BANNER = (guildID: string, guildBANNER: string): string => `${DISCORD_CDN_URL}/banners/${guildID}/${guildBANNER}` -const DEFAULT_USER_AVATAR = (iconID: string) => +const DEFAULT_USER_AVATAR = (iconID: string): string => `${DISCORD_CDN_URL}/embed/avatars/${iconID}` -const USER_AVATAR = (userID: string, iconID: string) => +const USER_AVATAR = (userID: string, iconID: string): string => `${DISCORD_CDN_URL}/avatars/${userID}/${iconID}` -const APPLICATION_ASSET = (applicationID: string, assetID: string) => +const APPLICATION_ASSET = (applicationID: string, assetID: string): string => `${DISCORD_CDN_URL}/app-icons/${applicationID}/${assetID}` const ACHIEVEMENT_ICON = ( applicationID: string, achievementID: string, iconHASH: string -) => +): string => `${DISCORD_CDN_URL}/app-assets/${applicationID}/achievements/${achievementID}/icons/${iconHASH}` -const TEAM_ICON = (teamID: string, iconID: string) => +const TEAM_ICON = (teamID: string, iconID: string): string => `${DISCORD_CDN_URL}/team-icons/${teamID}/${iconID}` -//Emoji Endpoints -const EMOJI = (guildID: string, emojiID: string) => +// Emoji Endpoints +const EMOJI = (guildID: string, emojiID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/emojis/${emojiID}` -const EMOJIS = (guildID: string) => +const EMOJIS = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/emojis` -//Template Endpoint -const TEMPLATE = (templateCODE: string) => +// Template Endpoint +const TEMPLATE = (templateCODE: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/templates/${templateCODE}` -//Invite Endpoint -const INVITE = (inviteCODE: string) => +// Invite Endpoint +const INVITE = (inviteCODE: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/invites/${inviteCODE}` -//Voice Endpoint -const VOICE_REGIONS = (guildID: string) => +// Voice Endpoint +const VOICE_REGIONS = (guildID: string): string => `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/regions` export default [ @@ -254,3 +259,76 @@ export default [ INVITE, VOICE_REGIONS ] + +export { + GUILDS, + GUILD, + GUILD_AUDIT_LOGS, + GUILD_WIDGET, + GUILD_EMOJI, + GUILD_ROLE, + GUILD_ROLES, + GUILD_INTEGRATION, + GUILD_INTEGRATIONS, + GUILD_INTEGARTION_SYNC, + GUILD_WIDGET_IMAGE, + GUILD_BAN, + GUILD_BANS, + GUILD_CHANNEL, + GUILD_CHANNELS, + GUILD_MEMBER, + GUILD_MEMBERS, + GUILD_MEMBER_ROLE, + GUILD_INVITES, + GUILD_LEAVE, + GUILD_PRUNE, + GUILD_VANITY_URL, + GUILD_NICK, + GUILD_PREVIEW, + CHANNEL, + CHANNELS, + CHANNEL_MESSAGE, + CHANNEL_MESSAGES, + CHANNEL_CROSSPOST, + MESSAGE_REACTIONS, + MESSAGE_REACTION, + MESSAGE_REACTION_ME, + MESSAGE_REACTION_USER, + CHANNEL_BULK_DELETE, + CHANNEL_FOLLOW, + CHANNEL_INVITES, + CHANNEL_PIN, + CHANNEL_PINS, + CHANNEL_PERMISSION, + CHANNEL_TYPING, + GROUP_RECIPIENT, + CURRENT_USER, + CURRENT_USER_GUILDS, + USER_DM, + USER_CONNECTIONS, + LEAVE_GUILD, + USER, + CHANNEL_WEBHOOKS, + GUILD_WEBHOOK, + WEBHOOK, + WEBHOOK_WITH_TOKEN, + SLACK_WEBHOOK, + GITHUB_WEBHOOK, + GATEWAY, + GATEWAY_BOT, + CUSTOM_EMOJI, + GUILD_ICON, + GUILD_SPLASH, + GUILD_DISCOVERY_SPLASH, + GUILD_BANNER, + DEFAULT_USER_AVATAR, + USER_AVATAR, + APPLICATION_ASSET, + ACHIEVEMENT_ICON, + TEAM_ICON, + EMOJI, + EMOJIS, + TEMPLATE, + INVITE, + VOICE_REGIONS +}