Merge pull request #74 from ayntee/emoji-methods

feat(structures): add Emoji methods
This commit is contained in:
Helloyunho 2020-12-29 12:46:04 +09:00 committed by GitHub
commit 492ebe91f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 44 additions and 12 deletions

View file

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

View file

@ -29,7 +29,7 @@ export const messageReactionAdd: GatewayEventHandler = async (
} else return } else return
} }
const emojiID = d.emoji.id !== null ? d.emoji.id : d.emoji.name const emojiID = (d.emoji.id !== null ? d.emoji.id : d.emoji.name) as string
let reaction = await message.reactions.get(emojiID) let reaction = await message.reactions.get(emojiID)
if (reaction === undefined) { if (reaction === undefined) {
await message.reactions.set(emojiID, { await message.reactions.set(emojiID, {

View file

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

View file

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

View file

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

View file

@ -23,7 +23,7 @@ export class MessageReactionsManager extends BaseManager<
const emojiID = raw.emoji.id !== null ? raw.emoji.id : raw.emoji.name const emojiID = raw.emoji.id !== null ? raw.emoji.id : raw.emoji.name
let emoji = await this.client.emojis.get(emojiID) 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)
const reaction = new MessageReaction(this.client, raw, this.message, emoji) const reaction = new MessageReaction(this.client, raw, this.message, emoji)
@ -46,7 +46,7 @@ export class MessageReactionsManager extends BaseManager<
return await Promise.all( return await Promise.all(
arr.map(async (raw) => { arr.map(async (raw) => {
const emojiID = raw.emoji.id !== null ? raw.emoji.id : raw.emoji.name const emojiID = raw.emoji.id !== null ? raw.emoji.id : raw.emoji.name
let emoji = await this.client.emojis.get(emojiID) 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)
return new MessageReaction(this.client, raw, this.message, emoji) return new MessageReaction(this.client, raw, this.message, emoji)

View file

@ -1,12 +1,14 @@
import { Client } from '../models/client.ts' import { Client } from '../models/client.ts'
import { EmojiPayload } from '../types/emoji.ts' import { EmojiPayload } from '../types/emoji.ts'
import { EMOJI } from '../types/endpoint.ts'
import { Base } from './base.ts' import { Base } from './base.ts'
import { Guild } from './guild.ts' import { Guild } from './guild.ts'
import { Role } from './role.ts'
import { User } from './user.ts' import { User } from './user.ts'
export class Emoji extends Base { export class Emoji extends Base {
id: string | null id: string | null
name: string name: string | null
roles?: string[] roles?: string[]
user?: User user?: User
guild?: Guild guild?: Guild
@ -17,7 +19,7 @@ export class Emoji extends Base {
get getEmojiString(): string { get getEmojiString(): string {
if (this.id === null) { if (this.id === null) {
return this.name return this.name as string
} else { } else {
if (this.animated === false) { if (this.animated === false) {
return `<:${this.name}:${this.id}>` return `<:${this.name}:${this.id}>`
@ -41,6 +43,28 @@ export class Emoji extends Base {
this.available = data.available this.available = data.available
} }
/** Modify the given emoji. Requires the MANAGE_EMOJIS permission. Returns the updated emoji object on success. Fires a Guild Emojis Update Gateway event. */
async edit(data: ModifyGuildEmojiParams): Promise<Emoji> {
if (this.id === null) throw new Error('Emoji ID is not valid.')
if (this.guild === undefined) throw new Error('Guild is undefined')
const roles = Array.isArray(data.roles)
? data.roles.map(role => (role instanceof Role ? role.id : role))
: [data.roles instanceof Role ? data.roles.id : data.roles]
const res = await this.client.rest.patch(EMOJI(this.guild.id, this.id), {
...data,
roles
})
return new Emoji(this.client, res)
}
/** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns `true` on success. Fires a Guild Emojis Update Gateway event. */
async delete(): Promise<boolean> {
if (this.id === null) return false
if (this.guild === undefined) return false
await this.client.rest.delete(EMOJI(this.guild.id, this.id))
return true
}
readFromData(data: EmojiPayload): void { readFromData(data: EmojiPayload): void {
this.id = data.id ?? this.id this.id = data.id ?? this.id
this.name = data.name ?? this.name this.name = data.name ?? this.name
@ -52,3 +76,11 @@ export class Emoji extends Base {
if (data.user !== undefined) this.user = new User(this.client, data.user) if (data.user !== undefined) this.user = new User(this.client, data.user)
} }
} }
/** https://discord.com/developers/docs/resources/emoji#modify-guild-emoji-json-params */
export interface ModifyGuildEmojiParams {
/** Name of the emoji */
name?: string
/** Roles to which this emoji will be whitelisted */
roles?: string | Role | Array<string | Role>;
}

View file

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