added changes requested by Dj

This commit is contained in:
CyberKnight007 2020-12-01 22:08:15 +05:30
parent 620299462e
commit 4fc5734ccc
4 changed files with 62 additions and 14 deletions

View file

@ -2,6 +2,7 @@ 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'
import getChannelByType from '../../utils/getChannelByType.ts'
export const inviteCreate: GatewayEventHandler = async (
gateway: Gateway,
@ -13,20 +14,27 @@ export const inviteCreate: GatewayEventHandler = async (
// Weird case, shouldn't happen
if (guild === undefined) return
const cachedChannel = await guild.channels.get(d.channel_id)
/**
* TODO(DjDeveloperr): Add _get method in BaseChildManager
*/
const cachedChannel = await gateway.client.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,
// had to use `as ChannelPayload` because the _get method returned `ChannelPayload | undefined` which errored
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)

View file

@ -15,7 +15,10 @@ export const inviteDelete: GatewayEventHandler = async (
const cachedInvite = await guild.invites.get(d.code)
// Should not happen but here we go
if (cachedInvite === undefined) return
gateway.client.emit('inviteDelete', cachedInvite)
if (cachedInvite === undefined) {
return gateway.client.emit('inviteDeleteUncached', cachedInvite)
} else {
await guild.invites.delete(d.code)
gateway.client.emit('inviteDelete', cachedInvite)
}
}

View file

@ -13,6 +13,12 @@ export class InviteManager extends BaseManager<InvitePayload, Invite> {
this.guild = guild
}
async get(key: string): Promise<Invite | undefined> {
const raw = await this._get(key)
if (raw === undefined) return
return new Invite(this.client, raw)
}
async fetch(id: string): Promise<Invite> {
return await new Promise((resolve, reject) => {
this.client.rest

View file

@ -1,10 +1,18 @@
import { Command, CommandClient, Intents, GuildChannel, CommandContext, Extension } from '../../mod.ts'
import {
Command,
CommandClient,
Intents,
GuildChannel,
CommandContext,
Extension,
} from '../../mod.ts'
import { Invite } from '../structures/invite.ts'
import { TOKEN } from './config.ts'
const client = new CommandClient({
prefix: ["pls", "!"],
prefix: ['pls', '!'],
spacesAfterPrefix: true,
mentionPrefix: true
mentionPrefix: true,
})
client.on('debug', console.log)
@ -55,10 +63,21 @@ client.on('webhooksUpdate', (guild, channel) => {
console.log(`Webhooks Updated in #${channel.name} from ${guild.name}`)
})
client.on("commandError", console.error)
client.on('inviteCreate', (invite: Invite) => {
console.log(`Invite Create: ${invite.code}`)
})
client.on('inviteDelete', (invite: Invite) => {
console.log(`Invite Delete: ${invite.code}`)
})
client.on('inviteDeleteUncached', (invite: Invite) => {
console.log(`Invite deleted uncached: ${invite}`)
})
client.on('commandError', console.error)
class ChannelLog extends Extension {
onChannelCreate(ext: Extension, channel: GuildChannel): void {
console.log(`Channel Created: ${channel.name}`)
}
@ -81,15 +100,27 @@ class ChannelLog extends Extension {
client.extensions.load(ChannelLog)
client.on('messageDeleteBulk', (channel, messages, uncached) => {
console.log(`=== Message Delete Bulk ===\nMessages: ${messages.map(m => m.id).join(', ')}\nUncached: ${[...uncached.values()].join(', ')}`)
console.log(
`=== Message Delete Bulk ===\nMessages: ${messages
.map((m) => m.id)
.join(', ')}\nUncached: ${[...uncached.values()].join(', ')}`
)
})
client.on('channelUpdate', (before, after) => {
console.log(`Channel Update: ${(before as GuildChannel).name}, ${(after as GuildChannel).name}`)
console.log(
`Channel Update: ${(before as GuildChannel).name}, ${
(after as GuildChannel).name
}`
)
})
client.on('typingStart', (user, channel, at, guildData) => {
console.log(`${user.tag} started typing in ${channel.id} at ${at}${guildData !== undefined ? `\nGuild: ${guildData.guild.name}` : ''}`)
console.log(
`${user.tag} started typing in ${channel.id} at ${at}${
guildData !== undefined ? `\nGuild: ${guildData.guild.name}` : ''
}`
)
})
// client.on('raw', (evt: string) => console.log(`EVENT: ${evt}`))