diff --git a/README.md b/README.md index f6d6b4b..a7a2571 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme) -Discord Deno API that is easy to use +An easy to use Discord API Library for Deno ## Table of Contents @@ -43,10 +43,10 @@ Not made yet See [the contributing file](CONTRIBUTING.md)! -PRs accepted. +PRs are accepted. Small note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. ## License -MIT © 2020 Helloyunho +[MIT © 2020 Helloyunho](LICENSE) diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..9d44897 --- /dev/null +++ b/mod.ts @@ -0,0 +1,56 @@ +export * from './src/gateway/index.ts' +export * from './src/models/client.ts' +export * from './src/models/rest.ts' +export * from './src/models/CacheAdapter.ts' +export * from './src/models/shard.ts' +export * from './src/managers/BaseManager.ts' +export * from './src/managers/BaseChildManager.ts' +export * from './src/managers/ChannelsManager.ts' +export * from './src/managers/EmojisManager.ts' +export * from './src/managers/GatewayCache.ts' +export * from './src/managers/GuildChannelsManager.ts' +export * from './src/managers/GuildsManager.ts' +export * from './src/managers/MembersManager.ts' +export * from './src/managers/MessagesManager.ts' +export * from './src/managers/RolesManager.ts' +export * from './src/managers/UsersManager.ts' +export * from './src/structures/base.ts' +export * from './src/structures/cdn.ts' +export * from './src/structures/channel.ts' +export * from './src/structures/dmChannel.ts' +export * from './src/structures/embed.ts' +export * from './src/structures/emoji.ts' +export * from './src/structures/groupChannel.ts' +export * from './src/structures/guild.ts' +export * from './src/structures/guildCategoryChannel.ts' +export * from './src/structures/guildNewsChannel.ts' +export * from './src/structures/guildTextChannel.ts' +export * from './src/structures/guildVoiceChannel.ts' +export * from './src/structures/invite.ts' +export * from './src/structures/member.ts' +export * from './src/structures/message.ts' +export * from './src/structures/MessageMentions.ts' +export * from './src/structures/presence.ts' +export * from './src/structures/role.ts' +export * from './src/structures/snowflake.ts' +export * from './src/structures/textChannel.ts' +export * from './src/structures/user.ts' +export * from './src/structures/voicestate.ts' +export * from './src/structures/webhook.ts' +export * from './src/types/cdn.ts' +export * from './src/types/channel.ts' +export * from './src/types/emoji.ts' +export * from './src/types/endpoint.ts' +export * from './src/types/gateway.ts' +export * from './src/types/gatewayBot.ts' +export * from './src/types/gatewayResponse.ts' +export * from './src/types/guild.ts' +export * from './src/types/invite.ts' +export * from './src/types/permissionFlags.ts' +export * from './src/types/presence.ts' +export * from './src/types/role.ts' +export * from './src/types/template.ts' +export * from './src/types/user.ts' +export * from './src/types/voice.ts' +export * from './src/types/webhook.ts' +export * from './src/utils/collection.ts' \ No newline at end of file diff --git a/src/gateway/handlers/channelUpdate.ts b/src/gateway/handlers/channelUpdate.ts index 50869c2..869290b 100644 --- a/src/gateway/handlers/channelUpdate.ts +++ b/src/gateway/handlers/channelUpdate.ts @@ -1,4 +1,3 @@ -import { GuildChannelPayload } from "../../managers/GuildChannelsManager.ts" import { Channel } from '../../structures/channel.ts' import { Guild } from "../../structures/guild.ts" import { ChannelPayload } from '../../types/channel.ts' @@ -9,13 +8,13 @@ export const channelUpdate: GatewayEventHandler = async ( gateway: Gateway, d: ChannelPayload ) => { - const oldChannel: Channel | void = await gateway.client.channels.get(d.id) + const oldChannel: Channel | undefined = await gateway.client.channels.get(d.id) if (oldChannel !== undefined) { await gateway.client.channels.set(d.id, d) let guild: undefined | Guild; - if((d as GuildChannelPayload).guild_id) { - guild = await gateway.client.guilds.get((d as GuildChannelPayload).guild_id) || undefined + if((d as any).guild_id !== undefined) { + guild = await gateway.client.guilds.get((d as any).guild_id) as Guild | undefined } if (oldChannel.type !== d.type) { const channel: Channel = getChannelByType(gateway.client, d, guild) ?? oldChannel diff --git a/src/gateway/handlers/guildBanAdd.ts b/src/gateway/handlers/guildBanAdd.ts index 7374a5e..232fc8d 100644 --- a/src/gateway/handlers/guildBanAdd.ts +++ b/src/gateway/handlers/guildBanAdd.ts @@ -2,7 +2,6 @@ import { Gateway, GatewayEventHandler } from '../index.ts' import { Guild } from '../../structures/guild.ts' import { User } from '../../structures/user.ts' import { GuildBanAddPayload } from '../../types/gateway.ts' -import { Member } from "../../structures/member.ts" export const guildBanAdd: GatewayEventHandler = async ( gateway: Gateway, diff --git a/src/gateway/handlers/guildCreate.ts b/src/gateway/handlers/guildCreate.ts index 6352743..bd297fa 100644 --- a/src/gateway/handlers/guildCreate.ts +++ b/src/gateway/handlers/guildCreate.ts @@ -11,39 +11,40 @@ export const guildCreate: GatewayEventHandler = async(gateway: Gateway, d: Guild if (guild !== undefined) { // It was just lazy load, so we don't fire the event as its gonna fire for every guild bot is in await gateway.client.guilds.set(d.id, d) - if((d as GuildPayload).members) { - let members = new MembersManager(gateway.client, guild) - await members.fromPayload((d as GuildPayload).members as MemberPayload[]) + if((d as any).members !== undefined) { + const members = new MembersManager(gateway.client, guild) + await members.fromPayload((d as any).members as MemberPayload[]) guild.members = members } - if((d as GuildPayload).channels) { - for (let ch of (d as GuildPayload).channels as ChannelPayload[]) { + if((d as any).channels !== undefined) { + for (const ch of (d as any).channels as ChannelPayload[]) { (ch as any).guild_id = d.id await gateway.client.channels.set(ch.id, ch) } } - if((d as GuildPayload).roles) { - let roles = new RolesManager(gateway.client, guild) - await roles.fromPayload((d as GuildPayload).roles as RolePayload[]) + if((d as any).roles !== undefined) { + const roles = new RolesManager(gateway.client, guild) + await roles.fromPayload((d as any).roles as RolePayload[]) guild.roles = roles } guild.refreshFromData(d) } else { await gateway.client.guilds.set(d.id, d) - guild = new Guild(gateway.client, d as GuildPayload) - if((d as GuildPayload).members) { - let members = new MembersManager(gateway.client, guild) - await members.fromPayload((d as GuildPayload).members as MemberPayload[]) + guild = new Guild(gateway.client, d) + if((d as any).members !== undefined) { + const members = new MembersManager(gateway.client, guild) + await members.fromPayload((d as any).members as MemberPayload[]) guild.members = members } - if((d as GuildPayload).channels) { - for (let ch of (d as GuildPayload).channels as ChannelPayload[]) { + if((d as any).channels !== undefined) { + for (const ch of (d as any).channels as ChannelPayload[]) { + (ch as any).guild_id = d.id await gateway.client.channels.set(ch.id, ch) } } - if((d as GuildPayload).roles) { - let roles = new RolesManager(gateway.client, guild) - await roles.fromPayload((d as GuildPayload).roles as RolePayload[]) + if((d as any).roles !== undefined) { + const roles = new RolesManager(gateway.client, guild) + await roles.fromPayload((d as any).roles as RolePayload[]) guild.roles = roles } await guild.roles.fromPayload(d.roles) diff --git a/src/gateway/handlers/guildEmojiUpdate.ts b/src/gateway/handlers/guildEmojiUpdate.ts index 5d08bab..0bf5c9f 100644 --- a/src/gateway/handlers/guildEmojiUpdate.ts +++ b/src/gateway/handlers/guildEmojiUpdate.ts @@ -1,14 +1,14 @@ import cache from '../../models/cache.ts' import { Guild } from '../../structures/guild.ts' -import { GuildEmojiUpdatePayload } from '../../types/gatewayTypes.ts' +import { GuildEmojiUpdatePayload } from '../../types/gateway.ts' import { Gateway, GatewayEventHandler } from '../index.ts' -const guildEmojiUpdate: GatewayEventHandler = ( +export const guildEmojiUpdate: GatewayEventHandler = ( gateway: Gateway, d: GuildEmojiUpdatePayload ) => { const guild: Guild = cache.get('guild', d.guild_id) if (guild !== undefined) { - const emojis = guild.emojis + // const emojis = guild.emojis } } diff --git a/src/gateway/handlers/messageCreate.ts b/src/gateway/handlers/messageCreate.ts index 7cae77a..e77382f 100644 --- a/src/gateway/handlers/messageCreate.ts +++ b/src/gateway/handlers/messageCreate.ts @@ -1,4 +1,3 @@ -import { Channel } from '../../structures/channel.ts' import { Message } from '../../structures/message.ts' import { MessageMentions } from '../../structures/MessageMentions.ts' import { TextChannel } from '../../structures/textChannel.ts' @@ -13,15 +12,15 @@ export const messageCreate: GatewayEventHandler = async ( let channel = await gateway.client.channels.get(d.channel_id) // Fetch the channel if not cached if (channel === undefined) - channel = (await gateway.client.channels.fetch(d.channel_id)) as TextChannel + channel = (await gateway.client.channels.fetch(d.channel_id)) as any const user = new User(gateway.client, d.author) await gateway.client.users.set(d.author.id, d.author) let guild - if(d.guild_id) { + if(d.guild_id !== undefined) { guild = await gateway.client.guilds.get(d.guild_id) } - let mentions = new MessageMentions() - let message = new Message(gateway.client, d, channel, user, mentions) - if(guild) message.guild = guild + const mentions = new MessageMentions() + const message = new Message(gateway.client, d, channel as any, user, mentions) + if(guild !== undefined) message.guild = guild gateway.client.emit('messageCreate', message) } diff --git a/src/gateway/index.ts b/src/gateway/index.ts index 067d416..3cd78c3 100644 --- a/src/gateway/index.ts +++ b/src/gateway/index.ts @@ -12,7 +12,6 @@ import { } from '../types/gateway.ts' import { gatewayHandlers } from './handlers/index.ts' import { GATEWAY_BOT } from '../types/endpoint.ts' -import { GatewayBotPayload } from "../types/gatewayBot.ts" import { GatewayCache } from "../managers/GatewayCache.ts" import { ClientActivityPayload } from "../structures/presence.ts" @@ -95,10 +94,9 @@ class Gateway { }, this.heartbeatInterval) if (!this.initialized) { - this.sendIdentify(this.client.forceNewSession) this.initialized = true + await this.sendIdentify(this.client.forceNewSession) } else { - console.log('Calling Resume') // eslint-disable-next-line @typescript-eslint/no-floating-promises this.sendResume() } @@ -290,18 +288,18 @@ class Gateway { this.websocket.close(1000) } - send(data: GatewayResponse) { - if (this.websocket.readyState != this.websocket.OPEN) return false + send(data: GatewayResponse): boolean { + if (this.websocket.readyState !== this.websocket.OPEN) return false this.websocket.send(JSON.stringify({ op: data.op, d: data.d, - s: typeof data.s == "number" ? data.s : null, - t: data.t || null, + s: typeof data.s === "number" ? data.s : null, + t: data.t === undefined ? null : data.t, })) return true } - sendPresence(data: ClientActivityPayload) { + sendPresence(data: ClientActivityPayload): void { this.send({ op: GatewayOpcodes.PRESENCE_UPDATE, d: data diff --git a/src/managers/BaseChildManager.ts b/src/managers/BaseChildManager.ts index a0f3d79..10357ac 100644 --- a/src/managers/BaseChildManager.ts +++ b/src/managers/BaseChildManager.ts @@ -11,12 +11,12 @@ export class BaseChildManager { this.parent = parent } - async get(key: string): Promise { - return await this.parent.get(key) + async get(key: string): Promise { + return this.parent.get(key) } - async set(key: string, value: T) { - return await this.parent.set(key, value) + async set(key: string, value: T): Promise { + return this.parent.set(key, value) } async delete(key: string): Promise { @@ -24,15 +24,15 @@ export class BaseChildManager { } async array(): Promise { - return await this.parent.array() + return this.parent.array() } async collection(): Promise> { - const arr = await this.array() as void | T2[] + const arr = await this.array() as undefined | T2[] if(arr === undefined) return new Collection() - let collection = new Collection() + const collection = new Collection() for (const elem of arr) { - // @ts-ignore + // @ts-expect-error collection.set(elem.id, elem) } return collection diff --git a/src/managers/BaseManager.ts b/src/managers/BaseManager.ts index eac2781..117c099 100644 --- a/src/managers/BaseManager.ts +++ b/src/managers/BaseManager.ts @@ -30,23 +30,23 @@ export class BaseManager { return this.client.cache.delete(this.cacheName, key) } - async array(): Promise { - let arr = await (this.client.cache.array(this.cacheName) as T[]) + async array(): Promise { + const arr = await (this.client.cache.array(this.cacheName) as T[]) return arr.map(e => new this.DataType(this.client, e)) as any } async collection(): Promise> { - const arr = await this.array() as void | T2[] + const arr = await this.array() if(arr === undefined) return new Collection() - let collection = new Collection() + const collection = new Collection() for (const elem of arr) { - // @ts-ignore + // @ts-expect-error collection.set(elem.id, elem) } return collection } - flush() { + flush(): any { return this.client.cache.deleteCache(this.cacheName) } } diff --git a/src/managers/ChannelsManager.ts b/src/managers/ChannelsManager.ts index 331c883..ad7e182 100644 --- a/src/managers/ChannelsManager.ts +++ b/src/managers/ChannelsManager.ts @@ -1,6 +1,5 @@ import { Client } from "../models/client.ts"; import { Channel } from "../structures/channel.ts"; -import { User } from "../structures/user.ts"; import { ChannelPayload } from "../types/channel.ts"; import { CHANNEL } from "../types/endpoint.ts"; import getChannelByType from "../utils/getChannelByType.ts"; @@ -13,39 +12,39 @@ export class ChannelsManager extends BaseManager { // Override get method as Generic async get(key: string): Promise { - let data = await this._get(key) as any - if(!data) return + const data = await this._get(key) + if(data === undefined) return let guild - if(data.guild_id) { - guild = await this.client.guilds.get(data.guild_id) + if((data as any).guild_id !== undefined) { + guild = await this.client.guilds.get((data as any).guild_id) } - let res = getChannelByType(this.client, data as ChannelPayload, guild || undefined) + const res = getChannelByType(this.client, data, guild) return res as any } - async array(): Promise { - let arr = await (this.client.cache.array(this.cacheName) as ChannelPayload[]) - let result: any[] = [] - for(let elem of arr) { + async array(): Promise { + const arr = await (this.client.cache.array(this.cacheName) as ChannelPayload[]) + const result: any[] = [] + for(const elem of arr) { let guild - if((elem as any).guild_id) { + if((elem as any).guild_id !== undefined) { guild = await this.client.guilds.get((elem as any).guild_id) } - result.push(getChannelByType(this.client, elem as ChannelPayload, guild || undefined)) + result.push(getChannelByType(this.client, elem, guild)) } return result } - fetch(id: string): Promise { - return new Promise((res, rej) => { + async fetch(id: string): Promise { + return await new Promise((resolve, reject) => { this.client.rest.get(CHANNEL(id)).then(async data => { this.set(id, data as ChannelPayload) let guild - if((data as any).guild_id) { - guild = await this.client.guilds.get((data as any).guild_id) + if(data.guild_id !== undefined) { + guild = await this.client.guilds.get(data.guild_id) } - res(getChannelByType(this.client, data as ChannelPayload, guild || undefined)) - }).catch(e => rej(e)) + resolve(getChannelByType(this.client, data as ChannelPayload, guild)) + }).catch(e => reject(e)) }) } } diff --git a/src/managers/GuildChannelsManager.ts b/src/managers/GuildChannelsManager.ts index 4937c52..c4a5d8f 100644 --- a/src/managers/GuildChannelsManager.ts +++ b/src/managers/GuildChannelsManager.ts @@ -7,13 +7,12 @@ import { VoiceChannel } from "../structures/guildVoiceChannel.ts"; import { GuildChannelCategoryPayload, GuildTextChannelPayload, GuildVoiceChannelPayload } from "../types/channel.ts"; import { CHANNEL } from "../types/endpoint.ts"; import { BaseChildManager } from "./BaseChildManager.ts"; -import { BaseManager } from "./BaseManager.ts"; import { ChannelsManager } from "./ChannelsManager.ts"; -export type GuildChannelPayload = GuildTextChannelPayload | GuildVoiceChannelPayload | GuildChannelCategoryPayload +export type GuildChannelPayloads = GuildTextChannelPayload | GuildVoiceChannelPayload | GuildChannelCategoryPayload export type GuildChannel = GuildTextChannel | VoiceChannel | CategoryChannel -export class GuildChannelsManager extends BaseChildManager { +export class GuildChannelsManager extends BaseChildManager { guild: Guild constructor(client: Client, parent: ChannelsManager, guild: Guild) { @@ -21,24 +20,24 @@ export class GuildChannelsManager extends BaseChildManager { + async get(id: string): Promise { const res = await this.parent.get(id) - if(res && res.guild.id == this.guild.id) return res - else return + if(res !== undefined && res.guild.id === this.guild.id) return res + else return undefined } - delete(id: string) { + async delete(id: string): Promise { return this.client.rest.delete(CHANNEL(id)) } async array(): Promise { - let arr = await this.parent.array() as Channel[] - return arr as any//.filter((c: any) => c.guild && c.guild.id == this.guild.id) as any + const arr = await this.parent.array() as Channel[] + return arr.filter((c: any) => c.guild !== undefined && c.guild.id === this.guild.id) as any } - async flush() { - let arr = await this.array() - for (let elem of arr) { + async flush(): Promise { + const arr = await this.array() + for (const elem of arr) { this.parent.delete(elem.id) } return true diff --git a/src/managers/GuildsManager.ts b/src/managers/GuildsManager.ts index 39271e3..5c53757 100644 --- a/src/managers/GuildsManager.ts +++ b/src/managers/GuildsManager.ts @@ -1,4 +1,3 @@ -import { guildBanAdd } from "../gateway/handlers/guildBanAdd.ts"; import { Client } from "../models/client.ts"; import { Guild } from "../structures/guild.ts"; import { GUILD } from "../types/endpoint.ts"; @@ -11,18 +10,18 @@ export class GuildManager extends BaseManager { super(client, 'guilds', Guild) } - fetch(id: string) { - return new Promise((res, rej) => { + async fetch(id: string): Promise { + return await new Promise((resolve, reject) => { this.client.rest.get(GUILD(id)).then(async (data: any) => { this.set(id, data) - let guild = new Guild(this.client, data) - if((data as GuildPayload).members) { - let members = new MembersManager(this.client, guild) + const guild = new Guild(this.client, data) + if((data as GuildPayload).members !== undefined) { + const members = new MembersManager(this.client, guild) await members.fromPayload((data as GuildPayload).members as MemberPayload[]) guild.members = members } - res(guild) - }).catch(e => rej(e)) + resolve(guild) + }).catch(e => reject(e)) }) } } diff --git a/src/managers/MembersManager.ts b/src/managers/MembersManager.ts index 92ea7b6..6d6746b 100644 --- a/src/managers/MembersManager.ts +++ b/src/managers/MembersManager.ts @@ -1,30 +1,28 @@ import { Client } from "../models/client.ts"; import { Guild } from "../structures/guild.ts"; import { Member } from "../structures/member.ts"; -import { Role } from "../structures/role.ts"; -import { GUILD_MEMBER, GUILD_ROLE } from "../types/endpoint.ts"; +import { GUILD_MEMBER } from "../types/endpoint.ts"; import { MemberPayload } from "../types/guild.ts"; -import { RolePayload } from "../types/role.ts"; import { BaseManager } from "./BaseManager.ts"; export class MembersManager extends BaseManager { guild: Guild constructor(client: Client, guild: Guild) { - super(client, `g${guild.id}m`, Member) + super(client, `members:${guild.id}`, Member) this.guild = guild } - fetch(id: string) { - return new Promise((res, rej) => { + async fetch(id: string): Promise { + return await new Promise((resolve, reject) => { this.client.rest.get(GUILD_MEMBER(this.guild.id, id)).then(data => { this.set(id, data as MemberPayload) - res(new Member(this.client, data as MemberPayload)) - }).catch(e => rej(e)) + resolve(new Member(this.client, data as MemberPayload)) + }).catch(e => reject(e)) }) } - async fromPayload(members: MemberPayload[]) { + async fromPayload(members: MemberPayload[]): Promise { for(const member of members) { await this.set(member.user.id, member) } diff --git a/src/managers/MessagesManager.ts b/src/managers/MessagesManager.ts index 459cf29..52723f9 100644 --- a/src/managers/MessagesManager.ts +++ b/src/managers/MessagesManager.ts @@ -5,7 +5,6 @@ import { TextChannel } from "../structures/textChannel.ts"; import { User } from "../structures/user.ts"; import { MessagePayload } from "../types/channel.ts"; import { CHANNEL_MESSAGE } from "../types/endpoint.ts"; -import { UserPayload } from "../types/user.ts"; import { BaseManager } from "./BaseManager.ts"; export class MessagesManager extends BaseManager { @@ -15,27 +14,27 @@ export class MessagesManager extends BaseManager { async get(key: string): Promise { const raw = await this._get(key) - if(!raw) return + if(raw === undefined) return let channel = await this.client.channels.get(raw.channel_id) - if(!channel) channel = await this.client.channels.fetch(raw.channel_id) - if(!channel) return - let author = new User(this.client, raw.author) - let mentions = new MessageMentions() + if(channel === undefined) channel = await this.client.channels.fetch(raw.channel_id) + if(channel === undefined) return + const author = new User(this.client, raw.author) + const mentions = new MessageMentions() return new this.DataType(this.client, raw, channel, author, mentions) as any } - fetch(channelID: string, id: string) { - return new Promise((res, rej) => { + async fetch(channelID: string, id: string): Promise { + return await new Promise((resolve, reject) => { this.client.rest.get(CHANNEL_MESSAGE(channelID, id)).then(async data => { this.set(id, data as MessagePayload) - let channel = await this.client.channels.get(channelID) - if(!channel) channel = await this.client.channels.fetch(channelID) as TextChannel - let author = new User(this.client, (data as MessagePayload).author as UserPayload) + let channel: any = await this.client.channels.get(channelID) + if(channel === undefined) channel = await this.client.channels.fetch(channelID) + const author = new User(this.client, (data as MessagePayload).author) await this.client.users.set(author.id, (data as MessagePayload).author) // TODO: Make this thing work (MessageMentions) - let mentions = new MessageMentions() - res(new Message(this.client, data as MessagePayload, channel, author, mentions)) - }).catch(e => rej(e)) + const mentions = new MessageMentions() + resolve(new Message(this.client, data as MessagePayload, channel as TextChannel, author, mentions)) + }).catch(e => reject(e)) }) } } diff --git a/src/managers/RolesManager.ts b/src/managers/RolesManager.ts index 3cc1889..6637082 100644 --- a/src/managers/RolesManager.ts +++ b/src/managers/RolesManager.ts @@ -25,7 +25,7 @@ export class RolesManager extends BaseManager { }) } - async fromPayload(roles: RolePayload[]) { + async fromPayload(roles: RolePayload[]): Promise { for(const role of roles) { await this.set(role.id, role) } diff --git a/src/models/CacheAdapter.ts b/src/models/CacheAdapter.ts index 96b2615..6355622 100644 --- a/src/models/CacheAdapter.ts +++ b/src/models/CacheAdapter.ts @@ -52,7 +52,8 @@ export class DefaultCacheAdapter implements ICacheAdapter { return cache.array() } - async deleteCache(cacheName: string) { + async deleteCache(cacheName: string): Promise { + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete return delete this.data[cacheName] } } @@ -120,8 +121,8 @@ export class RedisCacheAdapter implements ICacheAdapter { return data?.map((e: string) => JSON.parse(e)) } - async deleteCache(cacheName: string) { + async deleteCache(cacheName: string): Promise { await this._checkReady() - return await this.redis?.del(cacheName) + return await this.redis?.del(cacheName) !== 0 } } diff --git a/src/models/client.ts b/src/models/client.ts index e4c9013..2a44554 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -9,7 +9,7 @@ import { GuildManager } from "../managers/GuildsManager.ts" import { EmojisManager } from "../managers/EmojisManager.ts" import { ChannelsManager } from "../managers/ChannelsManager.ts" import { MessagesManager } from "../managers/MessagesManager.ts" -import { ActivityGame, ClientActivity, ClientActivityPayload, ClientPresence } from "../structures/presence.ts" +import { ActivityGame, ClientActivity, ClientPresence } from "../structures/presence.ts" /** Some Client Options to modify behaviour */ export interface ClientOptions { @@ -54,14 +54,14 @@ export class Client extends EventEmitter { return this } - setPresence(presence: ClientPresence | ClientActivity | ActivityGame) { + setPresence(presence: ClientPresence | ClientActivity | ActivityGame): void { if(presence instanceof ClientPresence) { this.presence = presence } else this.presence = new ClientPresence(presence) this.gateway?.sendPresence(this.presence.create()) } - debug(tag: string, msg: string) { + debug(tag: string, msg: string): void { this.emit("debug", `[${tag}] ${msg}`) } diff --git a/src/structures/guild.ts b/src/structures/guild.ts index b5484d0..7dc629f 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -1,18 +1,13 @@ import { Client } from '../models/client.ts' import { GuildFeatures, GuildPayload } from '../types/guild.ts' -import { PresenceUpdatePayload } from '../types/presence.ts' +import { PresenceUpdatePayload } from '../types/gateway.ts' import { Base } from './base.ts' -import { Channel } from './channel.ts' import { Emoji } from './emoji.ts' -import { Member } from './member.ts' import { VoiceState } from './voiceState.ts' import cache from '../models/cache.ts' -import getChannelByType from '../utils/getChannelByType.ts' import { RolesManager } from "../managers/RolesManager.ts" -import { Role } from "./role.ts" import { GuildChannelsManager } from "../managers/GuildChannelsManager.ts" import { MembersManager } from "../managers/MembersManager.ts" -import { ChannelsManager } from "../managers/ChannelsManager.ts" export class Guild extends Base { id: string diff --git a/src/structures/message.ts b/src/structures/message.ts index 33b4fed..123730f 100644 --- a/src/structures/message.ts +++ b/src/structures/message.ts @@ -14,7 +14,6 @@ import { User } from './user.ts' import { Member } from './member.ts' import { Embed } from './embed.ts' import { CHANNEL_MESSAGE } from '../types/endpoint.ts' -import { Channel } from "./channel.ts" import { MessageMentions } from "./MessageMentions.ts" import { TextChannel } from "./textChannel.ts" import { DMChannel } from "./dmChannel.ts" @@ -124,11 +123,11 @@ export class Message extends Base { this.flags = data.flags ?? this.flags } - edit (text?: string, option?: MessageOption): Promise { + async edit (text?: string, option?: MessageOption): Promise { return this.channel.edit(this.id, text, option) } - reply(text: string, options?: MessageOption) { + async reply(text: string, options?: MessageOption): Promise { // TODO: Use inline replies once they're out if(this.channel instanceof DMChannel) return this.channel.send(text, options) return this.channel.send(`${this.author.mention}, ${text}`, options) diff --git a/src/structures/presence.ts b/src/structures/presence.ts index f4c7175..b3feea0 100644 --- a/src/structures/presence.ts +++ b/src/structures/presence.ts @@ -37,13 +37,13 @@ export class ClientPresence { afk?: boolean constructor(data?: ClientActivity | ClientActivityPayload | ActivityGame) { - if (data) { + if (data !== undefined) { if((data as ClientActivity).activity !== undefined) { Object.assign(this, data) } else if((data as ClientActivityPayload).activities !== undefined) { } else if((data as ActivityGame).name !== undefined) { - if(!this.activity) { + if(this.activity === undefined) { this.activity = data as ActivityGame } else if(this.activity instanceof Array) { this.activity.push(data as ActivityGame) @@ -52,68 +52,71 @@ export class ClientPresence { } } - parse(payload: ClientActivityPayload) { + parse(payload: ClientActivityPayload): ClientPresence { this.afk = payload.afk this.activity = payload.activities ?? undefined this.since = payload.since this.status = payload.status + return this } - static parse(payload: ClientActivityPayload) { + static parse(payload: ClientActivityPayload): ClientPresence { return new ClientPresence().parse(payload) } create(): ClientActivityPayload { return { - afk: this.afk || false, + afk: this.afk === undefined ? false : this.afk, activities: this.createActivity(), - since: this.since || null, - status: this.status || 'online' + since: this.since === undefined ? null : this.since, + status: this.status === undefined ? 'online' : this.status } } - createActivity() { - let activity = this.activity == undefined ? null : (this.activity instanceof Array ? this.activity : [this.activity]) || null - if(activity == null) return activity + createActivity(): ActivityGame[] | null { + // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions + const activity = this.activity === undefined ? null : (this.activity instanceof Array ? this.activity : [this.activity]) || null + if(activity === null) return activity else { activity.map(e => { - if(typeof e.type == "string") e.type = ActivityTypes[e.type] + if(typeof e.type === "string") e.type = ActivityTypes[e.type] return e }) return activity } } - setStatus(status: StatusType) { + setStatus(status: StatusType): ClientPresence { this.status = status return this } - setActivity(activity: ActivityGame) { + setActivity(activity: ActivityGame): ClientPresence { this.activity = activity return this } - setActivities(activities: ActivityGame[]) { + setActivities(activities: ActivityGame[]): ClientPresence { this.activity = activities return this } - setAFK(afk: boolean) { + setAFK(afk: boolean): ClientPresence { this.afk = afk + return this } - removeAFK() { + removeAFK(): ClientPresence { this.afk = false return this } - toggleAFK() { - this.afk = !(this.afk || true) + toggleAFK(): ClientPresence { + this.afk = this.afk === undefined ? true : !this.afk return this } - setSince(since?: number) { + setSince(since?: number): ClientPresence { this.since = since return this } diff --git a/src/structures/textChannel.ts b/src/structures/textChannel.ts index 2f31268..ee06f28 100644 --- a/src/structures/textChannel.ts +++ b/src/structures/textChannel.ts @@ -4,7 +4,6 @@ import { CHANNEL_MESSAGE, CHANNEL_MESSAGES } from '../types/endpoint.ts' import { Channel } from './channel.ts' import { Message } from './message.ts' import { MessageMentions } from './MessageMentions.ts' -import { User } from "./user.ts" export class TextChannel extends Channel { lastMessageID?: string @@ -36,7 +35,7 @@ export class TextChannel extends Channel { allowed_mentions: option?.allowedMention }) - return new Message(this.client, resp as any, this, this.client.user as User, new MessageMentions()) + return new Message(this.client, resp as any, this, this.client.user as any, new MessageMentions()) } async edit ( diff --git a/src/types/gateway.ts b/src/types/gateway.ts index 1ab4f68..224c3d1 100644 --- a/src/types/gateway.ts +++ b/src/types/gateway.ts @@ -1,5 +1,6 @@ // https://discord.com/developers/docs/topics/opcodes-and-status-codes#gateway // https://discord.com/developers/docs/topics/gateway#commands-and-events-gateway-events +import { StatusType } from "../../mod.ts" import { EmojiPayload } from './emoji.ts' import { MemberPayload } from './guild.ts' import { ActivityPayload } from './presence.ts' @@ -291,7 +292,7 @@ export interface MessageReactionRemoveAllPayload { export interface PresenceUpdatePayload { user: UserPayload guild_id: string - status: string + status: StatusType activities: ActivityPayload[] client_status: UpdateStatus[] } diff --git a/src/types/guild.ts b/src/types/guild.ts index ea73d52..d8438d6 100644 --- a/src/types/guild.ts +++ b/src/types/guild.ts @@ -1,6 +1,6 @@ import { ChannelPayload } from './channel.ts' import { EmojiPayload } from './emoji.ts' -import { PresenceUpdatePayload } from './presence.ts' +import { PresenceUpdatePayload } from './gateway.ts' import { RolePayload } from './role.ts' import { UserPayload } from './user.ts' import { VoiceStatePayload } from './voice.ts' @@ -63,23 +63,23 @@ export interface MemberPayload { mute: boolean } -enum MessageNotification { +export enum MessageNotification { ALL_MESSAGES = 0, ONLY_MENTIONS = 1 } -enum ContentFilter { +export enum ContentFilter { DISABLED = 0, MEMBERS_WITHOUT_ROLES = 1, ALL_MEMBERS = 3 } -enum MFA { +export enum MFA { NONE = 0, ELEVATED = 1 } -enum Verification { +export enum Verification { NONE = 0, LOW = 1, MEDIUM = 2, @@ -87,14 +87,14 @@ enum Verification { VERY_HIGH = 4 } -enum PremiumTier { +export enum PremiumTier { NONE = 0, TIER_1 = 1, TIER_2 = 2, TIER_3 = 3 } -enum SystemChannelFlags { +export enum SystemChannelFlags { SUPPRESS_JOIN_NOTIFICATIONS = 1 << 0, SUPPRESS_PREMIUM_SUBSCRIPTIONS = 1 << 1 } diff --git a/src/types/presence.ts b/src/types/presence.ts index a04e46b..c127856 100644 --- a/src/types/presence.ts +++ b/src/types/presence.ts @@ -1,6 +1,3 @@ -import { StatusType } from "../structures/presence.ts"; -import { User } from "../structures/user.ts"; - export interface ClientStatus { desktop?: string mobile?: string @@ -60,12 +57,4 @@ export enum ActivityFlags { JOIN_REQUEST = 1 << 3, SYNC = 1 << 4, PLAY = 1 << 5 -} - -export interface PresenceUpdatePayload { - user: User - guild_id: string - status: StatusType - activities: ActivityPayload - client_status: ClientStatus } \ No newline at end of file diff --git a/src/types/voice.ts b/src/types/voice.ts index 706d212..c6b3143 100644 --- a/src/types/voice.ts +++ b/src/types/voice.ts @@ -1,7 +1,7 @@ // https://discord.com/developers/docs/topics/opcodes-and-status-codes#voice import { MemberPayload } from './guild.ts' -enum VoiceOpcodes { // VoiceOpcodes 추가 - UnderC - +export enum VoiceOpcodes { // VoiceOpcodes 추가 - UnderC - IDENTIFY = 0, SELECT_PROTOCOL = 1, READY = 2, @@ -15,7 +15,7 @@ enum VoiceOpcodes { // VoiceOpcodes 추가 - UnderC - CLIENT_DISCONNECT = 13 } -enum VoiceCloseCodes { +export enum VoiceCloseCodes { UNKNOWN_OPCODE = 4001, NOT_AUTHENTICATED = 4003, AUTHENTICATION_FAILED = 4004, diff --git a/src/utils/collection.ts b/src/utils/collection.ts index 728fcea..67bc83e 100644 --- a/src/utils/collection.ts +++ b/src/utils/collection.ts @@ -1,15 +1,9 @@ export class Collection extends Map { - maxSize?: number; - - set(key: K, value: V) { - if (this.maxSize || this.maxSize === 0) { - if (this.size >= this.maxSize) return this - } - + set(key: K, value: V): this { return super.set(key, value) } - array() { + array(): V[] { return [...this.values()] } @@ -21,53 +15,49 @@ export class Collection extends Map { return [...this.values()][this.size - 1] } - random() { + random(): V { const arr = [...this.values()] return arr[Math.floor(Math.random() * arr.length)] } - find(callback: (value: V, key: K) => boolean) { + find(callback: (value: V, key: K) => boolean): V | undefined { for (const key of this.keys()) { - const value = this.get(key)! + const value = this.get(key) as V + // eslint-disable-next-line standard/no-callback-literal if (callback(value, key)) return value } - // If nothing matched - } - filter(callback: (value: V, key: K) => boolean) { + filter(callback: (value: V, key: K) => boolean): Collection { const relevant = new Collection() this.forEach((value, key) => { if (callback(value, key)) relevant.set(key, value) - }); - - return relevant; + }) + return relevant } - map(callback: (value: V, key: K) => T) { + map(callback: (value: V, key: K) => T): T[] { const results = [] for (const key of this.keys()) { - const value = this.get(key)! + const value = this.get(key) as V results.push(callback(value, key)) } return results } - some(callback: (value: V, key: K) => boolean) { + some(callback: (value: V, key: K) => boolean): boolean { for (const key of this.keys()) { - const value = this.get(key)! + const value = this.get(key) as V if (callback(value, key)) return true } - return false } - every(callback: (value: V, key: K) => boolean) { + every(callback: (value: V, key: K) => boolean): boolean { for (const key of this.keys()) { - const value = this.get(key)! + const value = this.get(key) as V if (!callback(value, key)) return false } - return true } @@ -75,21 +65,21 @@ export class Collection extends Map { callback: (accumulator: T, value: V, key: K) => T, initialValue?: T, ): T { - let accumulator: T = initialValue! + let accumulator: T = initialValue as T for (const key of this.keys()) { - const value = this.get(key)! + const value = this.get(key) as V accumulator = callback(accumulator, value, key) } return accumulator } - static fromObject(object: { [key: string]: V }) { + static fromObject(object: { [key: string]: V }): Collection { return new Collection(Object.entries(object)) } - toObject() { - return Object.entries(this) + toObject(): { [name: string]: V } { + return Object.fromEntries(this) } } \ No newline at end of file