Some fixes. Working on Child Managers (not added, will continue tomorrow)

This commit is contained in:
DjDeveloperr 2020-11-01 19:12:00 +05:30
parent 79a06f43d7
commit 99630d74cf
17 changed files with 158 additions and 37 deletions

View file

@ -1,4 +1,5 @@
import { Channel } from '../../structures/channel.ts'
import { Guild } from "../../structures/guild.ts"
import getChannelByType from '../../utils/getChannelByType.ts'
import { Gateway, GatewayEventHandler } from '../index.ts'
@ -10,8 +11,12 @@ export const channelUpdate: GatewayEventHandler = async (
if (oldChannel !== undefined) {
await gateway.client.channels.set(d.id, d)
let guild: undefined | Guild;
if(d.guild_id) {
guild = await gateway.client.guilds.get(d.guild_id) || undefined
}
if (oldChannel.type !== d.type) {
const channel: Channel = getChannelByType(gateway.client, d) ?? oldChannel
const channel: Channel = getChannelByType(gateway.client, d, guild) ?? oldChannel
gateway.client.emit('channelUpdate', oldChannel, channel)
} else {
const before = oldChannel.refreshFromData(d)

View file

@ -2,7 +2,7 @@ import { Gateway, GatewayEventHandler } from '../index.ts'
import { Guild } from '../../structures/guild.ts'
import { GuildPayload } from "../../types/guild.ts"
export const guildCreate: GatewayEventHandler = async(gateway: Gateway, d: any) => {
export const guildCreate: GatewayEventHandler = async(gateway: Gateway, d: GuildPayload) => {
let guild: Guild | void = await gateway.client.guilds.get(d.id)
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
@ -11,6 +11,7 @@ export const guildCreate: GatewayEventHandler = async(gateway: Gateway, d: any)
} else {
await gateway.client.guilds.set(d.id, d)
guild = new Guild(gateway.client, d as GuildPayload)
await guild.roles.fromPayload(d.roles)
gateway.client.emit('guildCreate', guild)
}
}

View file

@ -1,7 +1,10 @@
import { User } from "../../structures/user.ts"
import { CLIENT_USER } from "../../types/endpoint.ts"
import { Gateway, GatewayEventHandler } from '../index.ts'
export const resume: GatewayEventHandler = (gateway: Gateway, d: any) => {
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)
gateway.client.emit('ready')
}

View file

@ -89,7 +89,7 @@ class Gateway {
}, this.heartbeatInterval)
if (!this.initialized) {
this.sendIdentify()
this.sendIdentify(this.client.forceNewSession)
this.initialized = true
} else {
console.log("Calling Resume")

View file

@ -0,0 +1,24 @@
import { Client } from "../models/client.ts";
import { BaseManager } from "./BaseManager.ts";
export class BaseChildManager<T, T2> {
client: Client
parent: BaseManager<T, T2>
constructor(client: Client, parent: BaseManager<T, T2>) {
this.client = client
this.parent = parent
}
async get(key: string): Promise<T2 | void> {
return await this.parent.get(key)
}
async set(key: string, value: T) {
return await this.parent.set(key, value)
}
async delete(key: string): Promise<any> {
return false
}
}

View file

@ -1,5 +1,5 @@
import { Client } from "../models/client.ts";
import { Base } from "../structures/base.ts";
import { Collection } from "../utils/collection.ts";
export class BaseManager<T, T2> {
client: Client
@ -22,11 +22,26 @@ export class BaseManager<T, T2> {
return new this.dataType(this.client, raw) as any
}
async set(key: string, value: T) {
set(key: string, value: T) {
return this.client.cache.set(this.cacheName, key, value)
}
async delete(key: string) {
delete(key: string) {
return this.client.cache.delete(this.cacheName, key)
}
array(): Promise<void | T2[]> {
return (this.client.cache.array(this.cacheName) as T[]).map(e => new this.dataType(this.client, e)) as any
}
async collection(): Promise<Collection<string, T2>> {
const arr = await this.array() as void | T2[]
if(arr === undefined) return new Collection()
let collection = new Collection()
for (const elem of arr) {
// @ts-ignore
collection.set(elem.id, elem)
}
return collection
}
}

View file

@ -0,0 +1,31 @@
import { Client } from "../models/client.ts";
import { Guild } from "../structures/guild.ts";
import { CategoryChannel } from "../structures/guildCategoryChannel.ts";
import { GuildTextChannel } from "../structures/guildTextChannel.ts";
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";
export type GuildChannelPayload = GuildTextChannelPayload | GuildVoiceChannelPayload | GuildChannelCategoryPayload
export type GuildChannel = GuildTextChannel | VoiceChannel | CategoryChannel
export class GuildChannelsManager extends BaseChildManager<GuildChannelPayload, GuildChannel> {
guild: Guild
constructor(client: Client, parent: BaseManager<GuildChannelPayload, GuildChannel>, guild: Guild) {
super(client, parent)
this.guild = guild
}
async get(id: string): Promise<GuildChannel | void> {
const res = await this.parent.get(id)
if(res && res.guild.id == this.guild.id) return res
else return
}
delete(id: string) {
return this.client.rest.delete(CHANNEL(id))
}
}

View file

@ -12,6 +12,17 @@ export class MessagesManager extends BaseManager<MessagePayload, Message> {
super(client, "messages", Message)
}
async get(key: string): Promise<Message | void> {
const raw = await this._get(key)
if(!raw) 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()
return new this.dataType(this.client, raw, channel, author, mentions) as any
}
fetch(channelID: string, id: string) {
return new Promise((res, rej) => {
this.client.rest.get(CHANNEL_MESSAGE(channelID, id)).then(async data => {

View file

@ -1,7 +1,6 @@
import { Client } from "../models/client.ts";
import { Guild } from "../structures/guild.ts";
import { Role } from "../structures/role.ts";
import { User } from "../structures/user.ts";
import { GUILD_ROLE } from "../types/endpoint.ts";
import { RolePayload } from "../types/role.ts";
import { BaseManager } from "./BaseManager.ts";
@ -22,4 +21,10 @@ export class RolesManager extends BaseManager<RolePayload, Role> {
}).catch(e => rej(e))
})
}
async fromPayload(roles: RolePayload[]) {
for(const role of roles) {
await this.guild.roles.set(role.id, role)
}
}
}

View file

@ -14,7 +14,8 @@ import { MessagesManager } from "../managers/MessagesManager.ts"
export interface ClientOptions {
token?: string
intents?: GatewayIntents[]
cache?: ICacheAdapter
cache?: ICacheAdapter,
forceNewSession?: boolean
}
/**
@ -28,6 +29,7 @@ export class Client extends EventEmitter {
token?: string
cache: ICacheAdapter = new DefaultCacheAdapter(this)
intents?: GatewayIntents[]
forceNewSession?: boolean
users: UserManager = new UserManager(this)
guilds: GuildManager = new GuildManager(this)
@ -39,6 +41,7 @@ export class Client extends EventEmitter {
super()
this.token = options.token
this.intents = options.intents
this.forceNewSession = options.forceNewSession
if(options.cache) this.cache = options.cache
}

View file

@ -168,22 +168,22 @@ export class Guild extends Base {
this.joinedAt = data.joined_at ?? this.joinedAt
this.large = data.large ?? this.large
this.memberCount = data.member_count ?? this.memberCount
this.voiceStates =
data.voice_states?.map(
v =>
cache.get('voiceState', `${v.guild_id}:${v.user_id}`) ??
new VoiceState(this.client, v)
) ?? this.voiceStates
this.members =
data.members?.map(
v =>
cache.get('member', `${this.id}:${v.user.id}`) ??
new Member(this.client, v)
) ?? this.members
this.channels =
data.channels?.map(
v => cache.get('channel', v.id) ?? getChannelByType(this.client, v)
) ?? this.members
// this.voiceStates =
// data.voice_states?.map(
// v =>
// cache.get('voiceState', `${v.guild_id}:${v.user_id}`) ??
// new VoiceState(this.client, v)
// ) ?? this.voiceStates
// this.members =
// data.members?.map(
// v =>
// cache.get('member', `${this.id}:${v.user.id}`) ??
// new Member(this.client, v)
// ) ?? this.members
// this.channels =
// data.channels?.map(
// v => cache.get('channel', v.id) ?? getChannelByType(this.client, v, this)
// ) ?? this.members
this.presences = data.presences ?? this.presences
this.maxPresences = data.max_presences ?? this.maxPresences
this.maxMembers = data.max_members ?? this.maxMembers

View file

@ -4,7 +4,7 @@ import {
GuildChannelCategoryPayload,
Overwrite
} from '../types/channel.ts'
import cache from '../models/cache.ts'
import { Guild } from "./guild.ts"
export class CategoryChannel extends Channel {
guildID: string
@ -12,12 +12,14 @@ export class CategoryChannel extends Channel {
position: number
permissionOverwrites: Overwrite[]
nsfw: boolean
guild: Guild
parentID?: string
constructor (client: Client, data: GuildChannelCategoryPayload) {
constructor (client: Client, data: GuildChannelCategoryPayload, guild: Guild) {
super(client, data)
this.guildID = data.guild_id
this.name = data.name
this.guild = guild
this.position = data.position
this.permissionOverwrites = data.permission_overwrites
this.nsfw = data.nsfw

View file

@ -1,7 +1,7 @@
import { Client } from '../models/client.ts'
import { GuildTextChannelPayload, Overwrite } from '../types/channel.ts'
import cache from '../models/cache.ts'
import { TextChannel } from './textChannel.ts'
import { Guild } from "./guild.ts"
export class GuildTextChannel extends TextChannel {
guildID: string
@ -12,15 +12,17 @@ export class GuildTextChannel extends TextChannel {
parentID?: string
rateLimit: number
topic?: string
guild: Guild
get mention (): string {
return `<#${this.id}>`
}
constructor (client: Client, data: GuildTextChannelPayload) {
constructor (client: Client, data: GuildTextChannelPayload, guild: Guild) {
super(client, data)
this.guildID = data.guild_id
this.name = data.name
this.guild = guild
this.position = data.position
this.permissionOverwrites = data.permission_overwrites
this.nsfw = data.nsfw

View file

@ -2,24 +2,27 @@ import cache from '../models/cache.ts'
import { Client } from '../models/client.ts'
import { GuildVoiceChannelPayload, Overwrite } from '../types/channel.ts'
import { Channel } from './channel.ts'
import { Guild } from "./guild.ts"
export class VoiceChannel extends Channel {
bitrate: string
userLimit: number
guildID: string
name: string
guild: Guild
position: number
permissionOverwrites: Overwrite[]
nsfw: boolean
parentID?: string
constructor (client: Client, data: GuildVoiceChannelPayload) {
constructor (client: Client, data: GuildVoiceChannelPayload, guild: Guild) {
super(client, data)
this.bitrate = data.bitrate
this.userLimit = data.user_limit
this.guildID = data.guild_id
this.name = data.name
this.position = data.position
this.guild = guild
this.permissionOverwrites = data.permission_overwrites
this.nsfw = data.nsfw
this.parentID = data.parent_id

View file

@ -1,9 +1,11 @@
import { Client } from '../models/client.ts'
import { GuildNewsChannelPayload, Overwrite } from '../types/channel.ts'
import { Guild } from "./guild.ts"
import { TextChannel } from './textChannel.ts'
export class NewsChannel extends TextChannel {
guildID: string
guild: Guild
name: string
position: number
permissionOverwrites: Overwrite[]
@ -11,10 +13,11 @@ export class NewsChannel extends TextChannel {
parentID?: string
topic?: string
constructor (client: Client, data: GuildNewsChannelPayload) {
constructor (client: Client, data: GuildNewsChannelPayload, guild: Guild) {
super(client, data)
this.guildID = data.guild_id
this.name = data.name
this.guild = guild
this.position = data.position
this.permissionOverwrites = data.permission_overwrites
this.nsfw = data.nsfw

View file

@ -187,6 +187,10 @@ const INVITE = (inviteCODE: string): string =>
const VOICE_REGIONS = (guildID: string): string =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/regions`
// Client User Endpoint
const CLIENT_USER = (): string =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/users/@me`
export default [
GUILDS,
GUILD,
@ -204,6 +208,7 @@ export default [
GUILD_CHANNEL,
GUILD_CHANNELS,
GUILD_MEMBER,
CLIENT_USER,
GUILD_MEMBERS,
GUILD_MEMBER_ROLE,
GUILD_INVITES,
@ -319,6 +324,7 @@ export {
CUSTOM_EMOJI,
GUILD_ICON,
GUILD_SPLASH,
CLIENT_USER,
GUILD_DISCOVERY_SPLASH,
GUILD_BANNER,
DEFAULT_USER_AVATAR,

View file

@ -14,7 +14,9 @@ import { GroupDMChannel } from '../structures/groupChannel.ts'
import { CategoryChannel } from '../structures/guildCategoryChannel.ts'
import { NewsChannel } from '../structures/guildNewsChannel.ts'
import { VoiceChannel } from '../structures/guildVoiceChannel.ts'
import { TextChannel } from '../structures/textChannel.ts'
import { Guild } from "../structures/guild.ts"
import { GuildTextChannel } from "../structures/guildTextChannel.ts"
import { TextChannel } from "../structures/textChannel.ts"
const getChannelByType = (
client: Client,
@ -25,7 +27,8 @@ const getChannelByType = (
| GuildVoiceChannelPayload
| DMChannelPayload
| GroupDMChannelPayload
| ChannelPayload
| ChannelPayload,
guild?: Guild
):
| CategoryChannel
| NewsChannel
@ -36,13 +39,17 @@ const getChannelByType = (
| undefined => {
switch (data.type) {
case ChannelTypes.GUILD_CATEGORY:
return new CategoryChannel(client, data as GuildChannelCategoryPayload)
if(!guild) throw new Error("No Guild was provided to construct Channel")
return new CategoryChannel(client, data as GuildChannelCategoryPayload, guild)
case ChannelTypes.GUILD_NEWS:
return new NewsChannel(client, data as GuildNewsChannelPayload)
if(!guild) throw new Error("No Guild was provided to construct Channel")
return new NewsChannel(client, data as GuildNewsChannelPayload, guild)
case ChannelTypes.GUILD_TEXT:
return new TextChannel(client, data as GuildTextChannelPayload)
if(!guild) throw new Error("No Guild was provided to construct Channel")
return new GuildTextChannel(client, data as GuildTextChannelPayload, guild)
case ChannelTypes.GUILD_VOICE:
return new VoiceChannel(client, data as GuildVoiceChannelPayload)
if(!guild) 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)
case ChannelTypes.GROUP_DM: