Fixed 173 Lint Errors

This commit is contained in:
DjDeveloperr 2020-11-03 14:51:29 +05:30
parent 9b37e185b0
commit 0fed83cb2d
27 changed files with 228 additions and 205 deletions

View File

@ -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)

56
mod.ts Normal file
View File

@ -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'

View File

@ -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

View File

@ -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,

View File

@ -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)

View File

@ -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
}
}

View File

@ -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<TextChannel>(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)
}

View File

@ -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

View File

@ -11,12 +11,12 @@ export class BaseChildManager<T, T2> {
this.parent = parent
}
async get(key: string): Promise<T2 | void> {
return await this.parent.get(key)
async get(key: string): Promise<T2 | undefined> {
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<void> {
return this.parent.set(key, value)
}
async delete(key: string): Promise<any> {
@ -24,15 +24,15 @@ export class BaseChildManager<T, T2> {
}
async array(): Promise<any> {
return await this.parent.array()
return this.parent.array()
}
async collection(): Promise<Collection<string, T2>> {
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

View File

@ -30,23 +30,23 @@ export class BaseManager<T, T2> {
return this.client.cache.delete(this.cacheName, key)
}
async array(): Promise<void | T2[]> {
let arr = await (this.client.cache.array(this.cacheName) as T[])
async array(): Promise<undefined | T2[]> {
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<Collection<string, T2>> {
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)
}
}

View File

@ -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<ChannelPayload, Channel> {
// Override get method as Generic
async get<T = Channel>(key: string): Promise<T | undefined> {
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<void | Channel[]> {
let arr = await (this.client.cache.array(this.cacheName) as ChannelPayload[])
let result: any[] = []
for(let elem of arr) {
async array(): Promise<undefined | Channel[]> {
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<Channel> {
return new Promise((res, rej) => {
async fetch(id: string): Promise<Channel> {
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))
})
}
}

View File

@ -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<GuildChannelPayload, GuildChannel> {
export class GuildChannelsManager extends BaseChildManager<GuildChannelPayloads, GuildChannel> {
guild: Guild
constructor(client: Client, parent: ChannelsManager, guild: Guild) {
@ -21,24 +20,24 @@ export class GuildChannelsManager extends BaseChildManager<GuildChannelPayload,
this.guild = guild
}
async get(id: string): Promise<GuildChannel | void> {
async get(id: string): Promise<GuildChannel | undefined> {
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<boolean> {
return this.client.rest.delete(CHANNEL(id))
}
async array(): Promise<GuildChannel[]> {
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<boolean> {
const arr = await this.array()
for (const elem of arr) {
this.parent.delete(elem.id)
}
return true

View File

@ -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<GuildPayload, Guild> {
super(client, 'guilds', Guild)
}
fetch(id: string) {
return new Promise((res, rej) => {
async fetch(id: string): Promise<Guild> {
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))
})
}
}

View File

@ -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<MemberPayload, Member> {
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<Member> {
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<void> {
for(const member of members) {
await this.set(member.user.id, member)
}

View File

@ -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<MessagePayload, Message> {
@ -15,27 +14,27 @@ export class MessagesManager extends BaseManager<MessagePayload, Message> {
async get(key: string): Promise<Message | undefined> {
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<Message> {
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<TextChannel>(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<TextChannel>(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))
})
}
}

View File

@ -25,7 +25,7 @@ export class RolesManager extends BaseManager<RolePayload, Role> {
})
}
async fromPayload(roles: RolePayload[]) {
async fromPayload(roles: RolePayload[]): Promise<boolean> {
for(const role of roles) {
await this.set(role.id, role)
}

View File

@ -52,7 +52,8 @@ export class DefaultCacheAdapter implements ICacheAdapter {
return cache.array()
}
async deleteCache(cacheName: string) {
async deleteCache(cacheName: string): Promise<boolean> {
// 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<boolean> {
await this._checkReady()
return await this.redis?.del(cacheName)
return await this.redis?.del(cacheName) !== 0
}
}

View File

@ -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}`)
}

View File

@ -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

View File

@ -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<Message> {
async edit (text?: string, option?: MessageOption): Promise<Message> {
return this.channel.edit(this.id, text, option)
}
reply(text: string, options?: MessageOption) {
async reply(text: string, options?: MessageOption): Promise<Message> {
// 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)

View File

@ -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
}

View File

@ -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 (

View File

@ -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[]
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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,

View File

@ -1,15 +1,9 @@
export class Collection<K = string, V = any> extends Map<K, V> {
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<K = string, V = any> extends Map<K, V> {
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<K, V> {
const relevant = new Collection<K, V>()
this.forEach((value, key) => {
if (callback(value, key)) relevant.set(key, value)
});
return relevant;
})
return relevant
}
map<T>(callback: (value: V, key: K) => T) {
map<T>(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<K = string, V = any> extends Map<K, V> {
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<V>(object: { [key: string]: V }) {
static fromObject<V>(object: { [key: string]: V }): Collection<string, V> {
return new Collection<string, V>(Object.entries(object))
}
toObject() {
return Object.entries(this)
toObject(): { [name: string]: V } {
return Object.fromEntries(this)
}
}