Handle null-id emojis

This commit is contained in:
Helloyunho 2020-12-24 09:59:32 +09:00
parent 315fd0d0da
commit eac5d52d83
8 changed files with 38 additions and 21 deletions

View file

@ -17,23 +17,29 @@ export const guildEmojiUpdate: GatewayEventHandler = async (
const _updated: EmojiPayload[] = []
for (const raw of d.emojis) {
const has = emojis.get(raw.id)
const emojiID = raw.id !== null ? raw.id : raw.name
const has = emojis.get(emojiID)
if (has === undefined) {
await guild.emojis.set(raw.id, raw)
const emoji = (await guild.emojis.get(raw.id)) as Emoji
await guild.emojis.set(emojiID, raw)
const emoji = (await guild.emojis.get(emojiID)) as Emoji
added.push(emoji)
} else _updated.push(raw)
}
for (const emoji of emojis.values()) {
const find = _updated.find((e) => emoji.id === e.id)
const emojiID = emoji.id !== null ? emoji.id : emoji.name
const find = _updated.find((e) => {
const eID = e.id !== null ? e.id : e.name
return emojiID === eID
})
if (find === undefined) {
await guild.emojis.delete(emoji.id)
await guild.emojis.delete(emojiID)
deleted.push(emoji)
} else {
const before = (await guild.emojis.get(find.id)) as Emoji
await guild.emojis.set(find.id, find)
const after = (await guild.emojis.get(find.id)) as Emoji
const foundID = find.id !== null ? find.id : find.name
const before = (await guild.emojis.get(foundID)) as Emoji
await guild.emojis.set(foundID, find)
const after = (await guild.emojis.get(foundID)) as Emoji
updated.push({ before, after })
}
}

View file

@ -29,15 +29,16 @@ export const messageReactionAdd: GatewayEventHandler = async (
} else return
}
let reaction = await message.reactions.get(d.emoji.id)
const emojiID = d.emoji.id !== null ? d.emoji.id : d.emoji.name
let reaction = await message.reactions.get(emojiID)
if (reaction === undefined) {
await message.reactions.set(d.emoji.id, {
await message.reactions.set(emojiID, {
count: 1,
emoji: d.emoji,
me: d.user_id === gateway.client.user?.id
})
reaction = ((await message.reactions.get(
d.emoji.id
emojiID
)) as unknown) as MessageReaction
}

View file

@ -27,7 +27,8 @@ export const messageReactionRemove: GatewayEventHandler = async (
} else return
}
const reaction = await message.reactions.get(d.emoji.id)
const emojiID = d.emoji.id !== null ? d.emoji.id : d.emoji.name
const reaction = await message.reactions.get(emojiID)
if (reaction === undefined) return
reaction.users.delete(d.user_id)

View file

@ -19,7 +19,8 @@ export const messageReactionRemoveEmoji: GatewayEventHandler = async (
} else return
}
const reaction = await message.reactions.get(d.emoji.id)
const emojiID = d.emoji.id !== null ? d.emoji.id : d.emoji.name
const reaction = await message.reactions.get(emojiID)
if (reaction === undefined) return
await reaction.users.flush()

View file

@ -87,7 +87,8 @@ export class GuildEmojisManager extends BaseChildManager<EmojiPayload, Emoji> {
async flush(): Promise<boolean> {
const arr = await this.array()
for (const elem of arr) {
this.parent.delete(elem.id)
const emojiID = elem.id !== null ? elem.id : elem.name
this.parent.delete(emojiID)
}
return true
}

View file

@ -21,7 +21,9 @@ export class MessageReactionsManager extends BaseManager<
const raw = await this._get(id)
if (raw === undefined) return
let emoji = await this.client.emojis.get(raw.emoji.id)
const emojiID = raw.emoji.id !== null ? raw.emoji.id : raw.emoji.name
let emoji = await this.client.emojis.get(emojiID)
if (emoji === undefined) emoji = new Emoji(this.client, raw.emoji)
const reaction = new MessageReaction(this.client, raw, this.message, emoji)
@ -43,7 +45,8 @@ export class MessageReactionsManager extends BaseManager<
return await Promise.all(
arr.map(async (raw) => {
let emoji = await this.client.emojis.get(raw.emoji.id)
const emojiID = raw.emoji.id !== null ? raw.emoji.id : raw.emoji.name
let emoji = await this.client.emojis.get(emojiID)
if (emoji === undefined) emoji = new Emoji(this.client, raw.emoji)
return new MessageReaction(this.client, raw, this.message, emoji)

View file

@ -5,7 +5,7 @@ import { Guild } from './guild.ts'
import { User } from './user.ts'
export class Emoji extends Base {
id: string
id: string | null
name: string
roles?: string[]
user?: User
@ -16,9 +16,13 @@ export class Emoji extends Base {
available?: boolean
get getEmojiString(): string {
if (this.animated === false) {
return `<:${this.name}:${this.id}>`
} else return `<a:${this.name}:${this.id}>`
if (this.id === null) {
return this.name
} else {
if (this.animated === false) {
return `<:${this.name}:${this.id}>`
} else return `<a:${this.name}:${this.id}>`
}
}
toString(): string {

View file

@ -1,7 +1,7 @@
import { UserPayload } from './user.ts'
export interface EmojiPayload {
id: string
id: string | null
name: string
roles?: string[]
user?: UserPayload