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[] = []
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)
if (has === undefined) {
await guild.emojis.set(emojiID, raw)
@ -27,7 +27,7 @@ export const guildEmojiUpdate: GatewayEventHandler = async (
}
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 eID = e.id !== null ? e.id : e.name
return emojiID === eID
@ -36,7 +36,7 @@ export const guildEmojiUpdate: GatewayEventHandler = async (
await guild.emojis.delete(emojiID)
deleted.push(emoji)
} 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
await guild.emojis.set(foundID, find)
const after = (await guild.emojis.get(foundID)) as Emoji

View file

@ -29,7 +29,7 @@ export const messageReactionAdd: GatewayEventHandler = async (
} 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)
if (reaction === undefined) {
await message.reactions.set(emojiID, {

View file

@ -27,7 +27,7 @@ export const messageReactionRemove: GatewayEventHandler = async (
} 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)
if (reaction === undefined) return

View file

@ -19,7 +19,7 @@ export const messageReactionRemoveEmoji: GatewayEventHandler = async (
} 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)
if (reaction === undefined) return

View file

@ -88,7 +88,7 @@ export class GuildEmojisManager extends BaseChildManager<EmojiPayload, Emoji> {
const arr = await this.array()
for (const elem of arr) {
const emojiID = elem.id !== null ? elem.id : elem.name
this.parent.delete(emojiID)
this.parent.delete(emojiID as string)
}
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
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)
const reaction = new MessageReaction(this.client, raw, this.message, emoji)
@ -46,7 +46,7 @@ export class MessageReactionsManager extends BaseManager<
return await Promise.all(
arr.map(async (raw) => {
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)
return new MessageReaction(this.client, raw, this.message, emoji)

View file

@ -1,12 +1,14 @@
import { Client } from '../models/client.ts'
import { EmojiPayload } from '../types/emoji.ts'
import { EMOJI } from '../types/endpoint.ts'
import { Base } from './base.ts'
import { Guild } from './guild.ts'
import { Role } from './role.ts'
import { User } from './user.ts'
export class Emoji extends Base {
id: string | null
name: string
name: string | null
roles?: string[]
user?: User
guild?: Guild
@ -17,7 +19,7 @@ export class Emoji extends Base {
get getEmojiString(): string {
if (this.id === null) {
return this.name
return this.name as string
} else {
if (this.animated === false) {
return `<:${this.name}:${this.id}>`
@ -41,6 +43,28 @@ export class Emoji extends Base {
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 {
this.id = data.id ?? this.id
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)
}
}
/** 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 {
id: string | null
name: string
name: string | null
roles?: string[]
user?: UserPayload
require_colons?: boolean