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:
commit
cc7a587040
6 changed files with 63 additions and 43 deletions
|
@ -30,7 +30,7 @@ export class GuildManager extends BaseManager<GuildPayload, Guild> {
|
||||||
this.client.rest
|
this.client.rest
|
||||||
.get(GUILD(id))
|
.get(GUILD(id))
|
||||||
.then(async (data: any) => {
|
.then(async (data: any) => {
|
||||||
this.set(id, data)
|
await this.set(id, data)
|
||||||
|
|
||||||
const guild = new Guild(this.client, data)
|
const guild = new Guild(this.client, data)
|
||||||
|
|
||||||
|
@ -145,6 +145,16 @@ export class GuildManager extends BaseManager<GuildPayload, Guild> {
|
||||||
return result
|
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.
|
* Edits a guild. Returns edited guild.
|
||||||
* @param guild Guild or guild id
|
* @param guild Guild or guild id
|
||||||
|
|
|
@ -23,6 +23,14 @@ export class MessageReactionsManager extends BaseManager<
|
||||||
this.message = message
|
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> {
|
async get(id: string): Promise<MessageReaction | undefined> {
|
||||||
const raw = await this._get(id)
|
const raw = await this._get(id)
|
||||||
if (raw === undefined) return
|
if (raw === undefined) return
|
||||||
|
@ -32,6 +40,7 @@ export class MessageReactionsManager extends BaseManager<
|
||||||
let emoji = await this.client.emojis.get(emojiID as string)
|
let emoji = await this.client.emojis.get(emojiID as string)
|
||||||
if (emoji === undefined) emoji = new Emoji(this.client, raw.emoji)
|
if (emoji === undefined) emoji = new Emoji(this.client, raw.emoji)
|
||||||
|
|
||||||
|
await this.updateRefs()
|
||||||
const reaction = new MessageReaction(this.client, raw, this.message, emoji)
|
const reaction = new MessageReaction(this.client, raw, this.message, emoji)
|
||||||
return reaction
|
return reaction
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,18 +93,12 @@ export class MessagesManager extends BaseManager<MessagePayload, Message> {
|
||||||
if (channel === undefined)
|
if (channel === undefined)
|
||||||
channel = await this.client.channels.fetch(this.channel.id)
|
channel = await this.client.channels.fetch(this.channel.id)
|
||||||
|
|
||||||
const author = new User(this.client, (data as MessagePayload).author)
|
|
||||||
await this.client.users.set(
|
await this.client.users.set(
|
||||||
author.id,
|
data.author.id,
|
||||||
(data as MessagePayload).author
|
(data as MessagePayload).author
|
||||||
)
|
)
|
||||||
|
|
||||||
const res = new Message(
|
const res = (await this.get(data.id)) as Message
|
||||||
this.client,
|
|
||||||
data as MessagePayload,
|
|
||||||
channel as TextChannel,
|
|
||||||
author
|
|
||||||
)
|
|
||||||
|
|
||||||
await res.mentions.fromPayload(data)
|
await res.mentions.fromPayload(data)
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -12,6 +12,10 @@ import { Channel } from './channel.ts'
|
||||||
import { Guild } from './guild.ts'
|
import { Guild } from './guild.ts'
|
||||||
import { VoiceState } from './voiceState.ts'
|
import { VoiceState } from './voiceState.ts'
|
||||||
|
|
||||||
|
export interface VoiceServerData extends VoiceServerUpdateData {
|
||||||
|
sessionID: string
|
||||||
|
}
|
||||||
|
|
||||||
export class VoiceChannel extends Channel {
|
export class VoiceChannel extends Channel {
|
||||||
bitrate: string
|
bitrate: string
|
||||||
userLimit: number
|
userLimit: number
|
||||||
|
@ -32,13 +36,15 @@ export class VoiceChannel extends Channel {
|
||||||
this.guild = guild
|
this.guild = guild
|
||||||
this.permissionOverwrites = data.permission_overwrites
|
this.permissionOverwrites = data.permission_overwrites
|
||||||
this.parentID = data.parent_id
|
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) => {
|
return await new Promise((resolve, reject) => {
|
||||||
let vcdata: VoiceServerUpdateData | undefined
|
let vcdata: VoiceServerData
|
||||||
|
let sessionID: string
|
||||||
let done = 0
|
let done = 0
|
||||||
|
|
||||||
const onVoiceStateAdd = (state: VoiceState): void => {
|
const onVoiceStateAdd = (state: VoiceState): void => {
|
||||||
|
@ -46,15 +52,24 @@ export class VoiceChannel extends Channel {
|
||||||
if (state.channel?.id !== this.id) return
|
if (state.channel?.id !== this.id) return
|
||||||
this.client.off('voiceStateAdd', onVoiceStateAdd)
|
this.client.off('voiceStateAdd', onVoiceStateAdd)
|
||||||
done++
|
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 => {
|
const onVoiceServerUpdate = (data: VoiceServerUpdateData): void => {
|
||||||
if (data.guild.id !== this.guild.id) return
|
if (data.guild.id !== this.guild.id) return
|
||||||
vcdata = data
|
vcdata = (data as unknown) as VoiceServerData
|
||||||
this.client.off('voiceServerUpdate', onVoiceServerUpdate)
|
this.client.off('voiceServerUpdate', onVoiceServerUpdate)
|
||||||
done++
|
done++
|
||||||
if (done >= 2) resolve(vcdata)
|
if (done >= 2) {
|
||||||
|
vcdata.sessionID = sessionID
|
||||||
|
resolve(vcdata)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.client.shards
|
this.client.shards
|
||||||
|
@ -78,6 +93,7 @@ export class VoiceChannel extends Channel {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Leave the Voice Channel */
|
||||||
leave(): void {
|
leave(): void {
|
||||||
this.client.shards
|
this.client.shards
|
||||||
.get(this.guild.shardID)
|
.get(this.guild.shardID)
|
||||||
|
|
|
@ -13,7 +13,7 @@ import { Member } from './member.ts'
|
||||||
import { Embed } from './embed.ts'
|
import { Embed } from './embed.ts'
|
||||||
import { CHANNEL_MESSAGE } from '../types/endpoint.ts'
|
import { CHANNEL_MESSAGE } from '../types/endpoint.ts'
|
||||||
import { MessageMentions } from './messageMentions.ts'
|
import { MessageMentions } from './messageMentions.ts'
|
||||||
import { TextChannel } from './textChannel.ts'
|
import { GuildTextChannel, TextChannel } from './textChannel.ts'
|
||||||
import { Guild } from './guild.ts'
|
import { Guild } from './guild.ts'
|
||||||
import { MessageReactionsManager } from '../managers/messageReactions.ts'
|
import { MessageReactionsManager } from '../managers/messageReactions.ts'
|
||||||
import { MessageSticker } from './messageSticker.ts'
|
import { MessageSticker } from './messageSticker.ts'
|
||||||
|
@ -115,6 +115,23 @@ export class Message extends SnowflakeBase {
|
||||||
: this.stickers
|
: 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. */
|
/** Edits this message. */
|
||||||
async edit(
|
async edit(
|
||||||
content?: string | AllMessageOptions,
|
content?: string | AllMessageOptions,
|
||||||
|
|
Loading…
Reference in a new issue