feat invite events

This commit is contained in:
CyberKnight007 2020-12-01 19:45:43 +05:30
parent 3ec783d7d4
commit c1ec7a7353
6 changed files with 100 additions and 2 deletions

6
.prettierrc Normal file
View file

@ -0,0 +1,6 @@
{
"tabWidth": 2,
"useTabs": false,
"semi": false,
"singleQuote": true
}

View file

@ -36,6 +36,8 @@ import { Member } from "../../structures/member.ts"
import { Role } from "../../structures/role.ts"
import { Message } from "../../structures/message.ts"
import { Collection } from "../../utils/collection.ts"
import { inviteCreate } from './inviteCreate'
import { inviteDelete } from './inviteDelete'
export const gatewayHandlers: {
[eventCode in GatewayEvents]: GatewayEventHandler | undefined
@ -61,8 +63,8 @@ export const gatewayHandlers: {
GUILD_ROLE_CREATE: guildRoleCreate,
GUILD_ROLE_UPDATE: guildRoleUpdate,
GUILD_ROLE_DELETE: guildRoleDelete,
INVITE_CREATE: undefined,
INVITE_DELETE: undefined,
INVITE_CREATE: inviteCreate,
INVITE_DELETE: inviteDelete,
MESSAGE_CREATE: messageCreate,
MESSAGE_UPDATE: messageUpdate,
MESSAGE_DELETE: messageDelete,

View file

@ -0,0 +1,34 @@
import { Gateway, GatewayEventHandler } from '../index.ts'
import { Guild } from '../../structures/guild.ts'
import { InviteCreatePayload } from '../../types/gateway.ts'
import { ChannelPayload, GuildPayload, InvitePayload } from '../../../mod.ts'
export const inviteCreate: GatewayEventHandler = async (
gateway: Gateway,
d: InviteCreatePayload
) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id!)
// Weird case, shouldn't happen
if (guild === undefined) return
const cachedChannel = await guild.channels.get(d.channel_id)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const cachedGuild: GuildPayload | undefined =
d.guild_id === undefined
? undefined
: await guild.client.guilds._get(d.guild_id)
const dataConverted: InvitePayload = {
code: d.code,
guild: cachedGuild,
channel: cachedChannel as ChannelPayload,
inviter: d.inviter,
target_user: d.target_user,
target_user_type: d.target_user_type,
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
await guild.invites.set(d.code, dataConverted)
const invite = await guild.invites.get(d.code)
gateway.client.emit('inviteCreate', invite)
}

View file

@ -0,0 +1,21 @@
import { Gateway, GatewayEventHandler } from '../index.ts'
import { Guild } from '../../structures/guild.ts'
import { InviteDeletePayload } from '../../types/gateway.ts'
export const inviteDelete: GatewayEventHandler = async (
gateway: Gateway,
d: InviteDeletePayload
) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id!)
// Weird case, shouldn't happen
if (guild === undefined) return
const cachedInvite = await guild.invites.get(d.code)
// Should not happen but here we go
if (cachedInvite === undefined) return
gateway.client.emit('inviteDelete', cachedInvite)
}

34
src/managers/invites.ts Normal file
View file

@ -0,0 +1,34 @@
import { Client } from '../models/client.ts'
import { Guild } from '../structures/guild.ts'
import { Invite } from '../structures/invite.ts'
import { GUILD_INVITES } from '../types/endpoint.ts'
import { InvitePayload } from '../types/invite.ts'
import { BaseManager } from './base.ts'
export class InviteManager extends BaseManager<InvitePayload, Invite> {
guild: Guild
constructor(client: Client, guild: Guild) {
super(client, `invites:${guild.id}`, Invite)
this.guild = guild
}
async fetch(id: string): Promise<Invite> {
return await new Promise((resolve, reject) => {
this.client.rest
.get(GUILD_INVITES(this.guild.id))
.then((data) => {
this.set(id, data as InvitePayload)
resolve(new Invite(this.client, data as InvitePayload))
})
.catch((e) => reject(e))
})
}
async fromPayload(invites: InvitePayload[]): Promise<boolean> {
for (const invite of invites) {
await this.set(invite.code, invite)
}
return true
}
}

View file

@ -78,6 +78,7 @@ export class Guild extends Base {
)
this.roles = new RolesManager(this.client, this)
this.emojis = new GuildEmojisManager(this.client, this.client.emojis, this)
this.invites = new InviteManager(this.client, this)
if (!this.unavailable) {
this.name = data.name