Merge pull request #1 from AkiaCode/main

Fix lint & Added reconnect handler
This commit is contained in:
DjDeveloper 2020-11-04 08:57:10 +05:30 committed by GitHub
commit a40ce553ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 52 additions and 45 deletions

View file

@ -4,13 +4,13 @@
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
An easy to use Discord API Library for Deno
**An easy to use Discord API Library for Deno**
## Table of Contents
- [Usage](#usage)
- [Docs](#docs)
- [Maintainers](#maintainers)
- [Maintainer](#maintainer)
- [Contributing](#contributing)
- [License](#license)
@ -35,7 +35,7 @@ bot.connect(TOKEN, [GatewayIntents.GUILD_MESSAGES])
Not made yet
## Maintainers
## Maintainer
[@Helloyunho](https://github.com/Helloyunho)

View file

@ -11,18 +11,18 @@ 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 any).members !== undefined) {
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 any).channels !== undefined) {
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 any).roles !== undefined) {
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
@ -31,18 +31,18 @@ export const guildCreate: GatewayEventHandler = async(gateway: Gateway, d: Guild
} else {
await gateway.client.guilds.set(d.id, d)
guild = new Guild(gateway.client, d)
if((d as any).members !== undefined) {
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 any).channels !== undefined) {
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 any).roles !== undefined) {
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

View file

@ -12,12 +12,13 @@ import { ready } from './ready.ts'
import { guildBanRemove } from './guildBanRemove.ts'
import { messageCreate } from "./messageCreate.ts"
import { resume } from "./resume.ts"
import { reconnect } from './reconnect.ts'
export const gatewayHandlers: {
[eventCode in GatewayEvents]: GatewayEventHandler | undefined
} = {
READY: ready,
RECONNECT: undefined,
RECONNECT: reconnect,
RESUMED: resume,
CHANNEL_CREATE: channelCreate,
CHANNEL_DELETE: channelDelete,

View file

@ -21,6 +21,6 @@ export const messageCreate: GatewayEventHandler = async (
}
const mentions = new MessageMentions()
const message = new Message(gateway.client, d, channel as any, user, mentions)
if(guild !== undefined) message.guild = guild
if (guild !== undefined) message.guild = guild
gateway.client.emit('messageCreate', message)
}

View file

@ -0,0 +1,6 @@
import { Gateway } from "../index.ts"
import { GatewayEventHandler } from "../index.ts"
export const reconnect: GatewayEventHandler = async (gateway: Gateway, d: any) => {
gateway.reconnect()
}

View file

@ -5,6 +5,6 @@ import { Gateway, GatewayEventHandler } from '../index.ts'
export const resume: GatewayEventHandler = async (gateway: Gateway, d: any) => {
gateway.debug(`Session Resumed!`)
gateway.client.emit('resume')
if(gateway.client.user === undefined) gateway.client.user = new User(gateway.client, await gateway.client.rest.get(CLIENT_USER()) as any)
if (gateway.client.user === undefined) gateway.client.user = new User(gateway.client, await gateway.client.rest.get(CLIENT_USER()) as any)
gateway.client.emit('ready')
}

View file

@ -227,7 +227,7 @@ class Gateway {
token: this.token,
properties: {
$os: Deno.build.os,
$browser: 'discord.deno',
$browser: 'discord.deno', //TODO: Change lib name
$device: 'discord.deno'
},
compress: true,

View file

@ -29,7 +29,7 @@ export class BaseChildManager<T, T2> {
async collection(): Promise<Collection<string, T2>> {
const arr = await this.array() as undefined | T2[]
if(arr === undefined) return new Collection()
if (arr === undefined) return new Collection()
const collection = new Collection()
for (const elem of arr) {
// @ts-expect-error

View file

@ -30,14 +30,14 @@ export class BaseManager<T, T2> {
return this.client.cache.delete(this.cacheName, key)
}
async array(): Promise<undefined | T2[]> {
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>> {
async collection (): Promise<Collection<string, T2>> {
const arr = await this.array()
if(arr === undefined) return new Collection()
if (arr === undefined) return new Collection()
const collection = new Collection()
for (const elem of arr) {
// @ts-expect-error

View file

@ -13,9 +13,9 @@ export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
// Override get method as Generic
async get<T = Channel>(key: string): Promise<T | undefined> {
const data = await this._get(key)
if(data === undefined) return
if (data === undefined) return
let guild
if((data as any).guild_id !== undefined) {
if ((data as any).guild_id !== undefined) {
guild = await this.client.guilds.get((data as any).guild_id)
}
const res = getChannelByType(this.client, data, guild)
@ -25,9 +25,9 @@ export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
async array(): Promise<undefined | Channel[]> {
const arr = await (this.client.cache.array(this.cacheName) as ChannelPayload[])
const result: any[] = []
for(const elem of arr) {
for (const elem of arr) {
let guild
if((elem as any).guild_id !== undefined) {
if ((elem as any).guild_id !== undefined) {
guild = await this.client.guilds.get((elem as any).guild_id)
}
result.push(getChannelByType(this.client, elem, guild))
@ -40,7 +40,7 @@ export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
this.client.rest.get(CHANNEL(id)).then(async data => {
this.set(id, data as ChannelPayload)
let guild
if(data.guild_id !== undefined) {
if (data.guild_id !== undefined) {
guild = await this.client.guilds.get(data.guild_id)
}
resolve(getChannelByType(this.client, data as ChannelPayload, guild))

View file

@ -22,7 +22,7 @@ export class GuildChannelsManager extends BaseChildManager<GuildChannelPayloads,
async get(id: string): Promise<GuildChannel | undefined> {
const res = await this.parent.get(id)
if(res !== undefined && res.guild.id === this.guild.id) return res
if (res !== undefined && res.guild.id === this.guild.id) return res
else return undefined
}

View file

@ -15,7 +15,7 @@ export class GuildManager extends BaseManager<GuildPayload, Guild> {
this.client.rest.get(GUILD(id)).then(async (data: any) => {
this.set(id, data)
const guild = new Guild(this.client, data)
if((data as GuildPayload).members !== undefined) {
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

View file

@ -23,7 +23,7 @@ export class MembersManager extends BaseManager<MemberPayload, Member> {
}
async fromPayload(members: MemberPayload[]): Promise<void> {
for(const member of members) {
for (const member of members) {
await this.set(member.user.id, member)
}
}

View file

@ -14,10 +14,10 @@ export class MessagesManager extends BaseManager<MessagePayload, Message> {
async get(key: string): Promise<Message | undefined> {
const raw = await this._get(key)
if(raw === undefined) return
if (raw === undefined) return
let channel = await this.client.channels.get(raw.channel_id)
if(channel === undefined) channel = await this.client.channels.fetch(raw.channel_id)
if(channel === undefined) return
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
@ -28,7 +28,7 @@ export class MessagesManager extends BaseManager<MessagePayload, Message> {
this.client.rest.get(CHANNEL_MESSAGE(channelID, id)).then(async data => {
this.set(id, data as MessagePayload)
let channel: any = await this.client.channels.get<TextChannel>(channelID)
if(channel === undefined) channel = await this.client.channels.fetch(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)

View file

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

View file

@ -54,14 +54,14 @@ export class Client extends EventEmitter {
return this
}
setPresence(presence: ClientPresence | ClientActivity | ActivityGame): void {
if(presence instanceof ClientPresence) {
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): void {
debug (tag: string, msg: string): void {
this.emit("debug", `[${tag}] ${msg}`)
}

View file

@ -129,7 +129,7 @@ export class Message extends Base {
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)
if (this.channel instanceof DMChannel) return this.channel.send(text, options)
return this.channel.send(`${this.author.mention}, ${text}`, options)
}

View file

@ -38,14 +38,14 @@ export class ClientPresence {
constructor(data?: ClientActivity | ClientActivityPayload | ActivityGame) {
if (data !== undefined) {
if((data as ClientActivity).activity !== undefined) {
if ((data as ClientActivity).activity !== undefined) {
Object.assign(this, data)
} else if((data as ClientActivityPayload).activities !== undefined) {
} else if ((data as ClientActivityPayload).activities !== undefined) {
} else if((data as ActivityGame).name !== undefined) {
if(this.activity === undefined) {
} else if ((data as ActivityGame).name !== undefined) {
if (this.activity === undefined) {
this.activity = data as ActivityGame
} else if(this.activity instanceof Array) {
} else if (this.activity instanceof Array) {
this.activity.push(data as ActivityGame)
} else this.activity = [ this.activity, data as ActivityGame ]
}
@ -76,10 +76,10 @@ export class ClientPresence {
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
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

View file

@ -39,16 +39,16 @@ const getChannelByType = (
| undefined => {
switch (data.type) {
case ChannelTypes.GUILD_CATEGORY:
if(guild === undefined) throw new Error("No Guild was provided to construct Channel")
if (guild === undefined) throw new Error("No Guild was provided to construct Channel")
return new CategoryChannel(client, data as GuildChannelCategoryPayload, guild)
case ChannelTypes.GUILD_NEWS:
if(guild === undefined) throw new Error("No Guild was provided to construct Channel")
if (guild === undefined) throw new Error("No Guild was provided to construct Channel")
return new NewsChannel(client, data as GuildNewsChannelPayload, guild)
case ChannelTypes.GUILD_TEXT:
if(guild === undefined) throw new Error("No Guild was provided to construct Channel")
if (guild === undefined) throw new Error("No Guild was provided to construct Channel")
return new GuildTextChannel(client, data as GuildTextChannelPayload, guild)
case ChannelTypes.GUILD_VOICE:
if(guild === undefined) throw new Error("No Guild was provided to construct Channel")
if (guild === undefined) throw new Error("No Guild was provided to construct Channel")
return new VoiceChannel(client, data as GuildVoiceChannelPayload, guild)
case ChannelTypes.DM:
return new DMChannel(client, data as DMChannelPayload)