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 { Channel } from '../../structures/channel.ts'
import { Guild } from "../../structures/guild.ts"
import getChannelByType from '../../utils/getChannelByType.ts' import getChannelByType from '../../utils/getChannelByType.ts'
import { Gateway, GatewayEventHandler } from '../index.ts' import { Gateway, GatewayEventHandler } from '../index.ts'
@ -10,8 +11,12 @@ export const channelUpdate: GatewayEventHandler = async (
if (oldChannel !== undefined) { if (oldChannel !== undefined) {
await gateway.client.channels.set(d.id, d) 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) { 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) gateway.client.emit('channelUpdate', oldChannel, channel)
} else { } else {
const before = oldChannel.refreshFromData(d) const before = oldChannel.refreshFromData(d)

View file

@ -2,7 +2,7 @@ import { Gateway, GatewayEventHandler } from '../index.ts'
import { Guild } from '../../structures/guild.ts' import { Guild } from '../../structures/guild.ts'
import { GuildPayload } from "../../types/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) let guild: Guild | void = await gateway.client.guilds.get(d.id)
if (guild !== undefined) { 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 // 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 { } else {
await gateway.client.guilds.set(d.id, d) await gateway.client.guilds.set(d.id, d)
guild = new Guild(gateway.client, d as GuildPayload) guild = new Guild(gateway.client, d as GuildPayload)
await guild.roles.fromPayload(d.roles)
gateway.client.emit('guildCreate', guild) 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' 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.debug(`Session Resumed!`)
gateway.client.emit('resume') 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') gateway.client.emit('ready')
} }

View file

@ -89,7 +89,7 @@ class Gateway {
}, this.heartbeatInterval) }, this.heartbeatInterval)
if (!this.initialized) { if (!this.initialized) {
this.sendIdentify() this.sendIdentify(this.client.forceNewSession)
this.initialized = true this.initialized = true
} else { } else {
console.log("Calling Resume") 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 { Client } from "../models/client.ts";
import { Base } from "../structures/base.ts"; import { Collection } from "../utils/collection.ts";
export class BaseManager<T, T2> { export class BaseManager<T, T2> {
client: Client client: Client
@ -22,11 +22,26 @@ export class BaseManager<T, T2> {
return new this.dataType(this.client, raw) as any 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) return this.client.cache.set(this.cacheName, key, value)
} }
async delete(key: string) { delete(key: string) {
return this.client.cache.delete(this.cacheName, key) 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) 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) { fetch(channelID: string, id: string) {
return new Promise((res, rej) => { return new Promise((res, rej) => {
this.client.rest.get(CHANNEL_MESSAGE(channelID, id)).then(async data => { this.client.rest.get(CHANNEL_MESSAGE(channelID, id)).then(async data => {

View file

@ -1,7 +1,6 @@
import { Client } from "../models/client.ts"; import { Client } from "../models/client.ts";
import { Guild } from "../structures/guild.ts"; import { Guild } from "../structures/guild.ts";
import { Role } from "../structures/role.ts"; import { Role } from "../structures/role.ts";
import { User } from "../structures/user.ts";
import { GUILD_ROLE } from "../types/endpoint.ts"; import { GUILD_ROLE } from "../types/endpoint.ts";
import { RolePayload } from "../types/role.ts"; import { RolePayload } from "../types/role.ts";
import { BaseManager } from "./BaseManager.ts"; import { BaseManager } from "./BaseManager.ts";
@ -22,4 +21,10 @@ export class RolesManager extends BaseManager<RolePayload, Role> {
}).catch(e => rej(e)) }).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 { export interface ClientOptions {
token?: string token?: string
intents?: GatewayIntents[] intents?: GatewayIntents[]
cache?: ICacheAdapter cache?: ICacheAdapter,
forceNewSession?: boolean
} }
/** /**
@ -28,6 +29,7 @@ export class Client extends EventEmitter {
token?: string token?: string
cache: ICacheAdapter = new DefaultCacheAdapter(this) cache: ICacheAdapter = new DefaultCacheAdapter(this)
intents?: GatewayIntents[] intents?: GatewayIntents[]
forceNewSession?: boolean
users: UserManager = new UserManager(this) users: UserManager = new UserManager(this)
guilds: GuildManager = new GuildManager(this) guilds: GuildManager = new GuildManager(this)
@ -39,6 +41,7 @@ export class Client extends EventEmitter {
super() super()
this.token = options.token this.token = options.token
this.intents = options.intents this.intents = options.intents
this.forceNewSession = options.forceNewSession
if(options.cache) this.cache = options.cache 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.joinedAt = data.joined_at ?? this.joinedAt
this.large = data.large ?? this.large this.large = data.large ?? this.large
this.memberCount = data.member_count ?? this.memberCount this.memberCount = data.member_count ?? this.memberCount
this.voiceStates = // this.voiceStates =
data.voice_states?.map( // data.voice_states?.map(
v => // v =>
cache.get('voiceState', `${v.guild_id}:${v.user_id}`) ?? // cache.get('voiceState', `${v.guild_id}:${v.user_id}`) ??
new VoiceState(this.client, v) // new VoiceState(this.client, v)
) ?? this.voiceStates // ) ?? this.voiceStates
this.members = // this.members =
data.members?.map( // data.members?.map(
v => // v =>
cache.get('member', `${this.id}:${v.user.id}`) ?? // cache.get('member', `${this.id}:${v.user.id}`) ??
new Member(this.client, v) // new Member(this.client, v)
) ?? this.members // ) ?? this.members
this.channels = // this.channels =
data.channels?.map( // data.channels?.map(
v => cache.get('channel', v.id) ?? getChannelByType(this.client, v) // v => cache.get('channel', v.id) ?? getChannelByType(this.client, v, this)
) ?? this.members // ) ?? this.members
this.presences = data.presences ?? this.presences this.presences = data.presences ?? this.presences
this.maxPresences = data.max_presences ?? this.maxPresences this.maxPresences = data.max_presences ?? this.maxPresences
this.maxMembers = data.max_members ?? this.maxMembers this.maxMembers = data.max_members ?? this.maxMembers

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -14,7 +14,9 @@ import { GroupDMChannel } from '../structures/groupChannel.ts'
import { CategoryChannel } from '../structures/guildCategoryChannel.ts' import { CategoryChannel } from '../structures/guildCategoryChannel.ts'
import { NewsChannel } from '../structures/guildNewsChannel.ts' import { NewsChannel } from '../structures/guildNewsChannel.ts'
import { VoiceChannel } from '../structures/guildVoiceChannel.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 = ( const getChannelByType = (
client: Client, client: Client,
@ -25,7 +27,8 @@ const getChannelByType = (
| GuildVoiceChannelPayload | GuildVoiceChannelPayload
| DMChannelPayload | DMChannelPayload
| GroupDMChannelPayload | GroupDMChannelPayload
| ChannelPayload | ChannelPayload,
guild?: Guild
): ):
| CategoryChannel | CategoryChannel
| NewsChannel | NewsChannel
@ -36,13 +39,17 @@ const getChannelByType = (
| undefined => { | undefined => {
switch (data.type) { switch (data.type) {
case ChannelTypes.GUILD_CATEGORY: 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: 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: 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: 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: case ChannelTypes.DM:
return new DMChannel(client, data as DMChannelPayload) return new DMChannel(client, data as DMChannelPayload)
case ChannelTypes.GROUP_DM: case ChannelTypes.GROUP_DM: