Merge pull request #104 from DjDeveloperr/slash

Add updateRefs to Message and MessageReactionsManager, don't double save Guild-related cache
This commit is contained in:
DjDeveloper 2021-02-06 19:48:36 +05:30 committed by GitHub
commit cc7a587040
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 63 additions and 43 deletions

View File

@ -30,7 +30,7 @@ export class GuildManager extends BaseManager<GuildPayload, Guild> {
this.client.rest
.get(GUILD(id))
.then(async (data: any) => {
this.set(id, data)
await this.set(id, data)
const guild = new Guild(this.client, data)
@ -145,6 +145,16 @@ export class GuildManager extends BaseManager<GuildPayload, Guild> {
return result
}
/** Sets a value to Cache */
async set(key: string, value: GuildPayload): Promise<any> {
if ('roles' in value) value.roles = []
if ('emojis' in value) value.emojis = []
if ('members' in value) value.members = []
if ('presences' in value) value.presences = []
if ('voice_states' in value) value.voice_states = []
return this.client.cache.set(this.cacheName, key, value)
}
/**
* Edits a guild. Returns edited guild.
* @param guild Guild or guild id

View File

@ -23,6 +23,14 @@ export class MessageReactionsManager extends BaseManager<
this.message = message
}
async updateRefs(): Promise<void> {
const newVal = await this.message.channel.messages.get(this.message.id)
if (newVal !== undefined) {
this.message = newVal
}
await this.message.updateRefs()
}
async get(id: string): Promise<MessageReaction | undefined> {
const raw = await this._get(id)
if (raw === undefined) return
@ -32,6 +40,7 @@ export class MessageReactionsManager extends BaseManager<
let emoji = await this.client.emojis.get(emojiID as string)
if (emoji === undefined) emoji = new Emoji(this.client, raw.emoji)
await this.updateRefs()
const reaction = new MessageReaction(this.client, raw, this.message, emoji)
return reaction
}

View File

@ -93,18 +93,12 @@ export class MessagesManager extends BaseManager<MessagePayload, Message> {
if (channel === undefined)
channel = await this.client.channels.fetch(this.channel.id)
const author = new User(this.client, (data as MessagePayload).author)
await this.client.users.set(
author.id,
data.author.id,
(data as MessagePayload).author
)
const res = new Message(
this.client,
data as MessagePayload,
channel as TextChannel,
author
)
const res = (await this.get(data.id)) as Message
await res.mentions.fromPayload(data)

View File

@ -1,26 +0,0 @@
import { Guild } from '../structures/guild.ts'
import { VoiceChannel } from '../structures/guildVoiceChannel.ts'
import { Client } from './client.ts'
export interface VoiceOptions {
guild: Guild
channel: VoiceChannel
}
export class VoiceClient {
client: Client
ws?: WebSocket
guild: Guild
channel: VoiceChannel
constructor(client: Client, options: VoiceOptions) {
this.client = client
this.guild = options.guild
this.channel = options.channel
}
async connect(): Promise<VoiceClient> {
// TODO(DjDeveloperr): understand docs
return this
}
}

View File

@ -12,6 +12,10 @@ import { Channel } from './channel.ts'
import { Guild } from './guild.ts'
import { VoiceState } from './voiceState.ts'
export interface VoiceServerData extends VoiceServerUpdateData {
sessionID: string
}
export class VoiceChannel extends Channel {
bitrate: string
userLimit: number
@ -32,13 +36,15 @@ export class VoiceChannel extends Channel {
this.guild = guild
this.permissionOverwrites = data.permission_overwrites
this.parentID = data.parent_id
// TODO: Cache in Gateway Event Code
// cache.set('guildvoicechannel', this.id, this)
}
async join(options?: VoiceStateOptions): Promise<VoiceServerUpdateData> {
/** Join the Voice Channel */
async join(
options?: VoiceStateOptions & { onlyJoin?: boolean }
): Promise<VoiceServerUpdateData> {
return await new Promise((resolve, reject) => {
let vcdata: VoiceServerUpdateData | undefined
let vcdata: VoiceServerData
let sessionID: string
let done = 0
const onVoiceStateAdd = (state: VoiceState): void => {
@ -46,15 +52,24 @@ export class VoiceChannel extends Channel {
if (state.channel?.id !== this.id) return
this.client.off('voiceStateAdd', onVoiceStateAdd)
done++
if (done >= 2) resolve((vcdata as unknown) as VoiceServerUpdateData)
sessionID = state.sessionID
if (done >= 2) {
vcdata.sessionID = sessionID
if (options?.onlyJoin !== true) {
}
resolve(vcdata)
}
}
const onVoiceServerUpdate = (data: VoiceServerUpdateData): void => {
if (data.guild.id !== this.guild.id) return
vcdata = data
vcdata = (data as unknown) as VoiceServerData
this.client.off('voiceServerUpdate', onVoiceServerUpdate)
done++
if (done >= 2) resolve(vcdata)
if (done >= 2) {
vcdata.sessionID = sessionID
resolve(vcdata)
}
}
this.client.shards
@ -78,6 +93,7 @@ export class VoiceChannel extends Channel {
})
}
/** Leave the Voice Channel */
leave(): void {
this.client.shards
.get(this.guild.shardID)

View File

@ -13,7 +13,7 @@ import { Member } from './member.ts'
import { Embed } from './embed.ts'
import { CHANNEL_MESSAGE } from '../types/endpoint.ts'
import { MessageMentions } from './messageMentions.ts'
import { TextChannel } from './textChannel.ts'
import { GuildTextChannel, TextChannel } from './textChannel.ts'
import { Guild } from './guild.ts'
import { MessageReactionsManager } from '../managers/messageReactions.ts'
import { MessageSticker } from './messageSticker.ts'
@ -115,6 +115,23 @@ export class Message extends SnowflakeBase {
: this.stickers
}
async updateRefs(): Promise<void> {
if (this.guildID !== undefined)
this.guild = await this.client.guilds.get(this.guildID)
const newVal = await this.client.channels.get<TextChannel>(this.channelID)
if (newVal !== undefined) this.channel = newVal
const newUser = await this.client.users.get(this.author.id)
if (newUser !== undefined) this.author = newUser
if (this.member !== undefined) {
const newMember = await this.guild?.members.get(this.member?.id)
if (newMember !== undefined) this.member = newMember
}
if (((this.channel as unknown) as GuildTextChannel).guild !== undefined)
this.guild = ((this.channel as unknown) as GuildTextChannel).guild
if (this.guild !== undefined && this.guildID === undefined)
this.guildID = this.guild.id
}
/** Edits this message. */
async edit(
content?: string | AllMessageOptions,