|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { Collection } = require('@discordjs/collection'); |
| 4 | +const { Routes } = require('discord-api-types/v10'); |
| 5 | +const CachedManager = require('./CachedManager'); |
| 6 | +const { DiscordjsTypeError, ErrorCodes } = require('../errors'); |
| 7 | +const ApplicationEmoji = require('../structures/ApplicationEmoji'); |
| 8 | +const { resolveImage } = require('../util/DataResolver'); |
| 9 | + |
| 10 | +/** |
| 11 | + * Manages API methods for ApplicationEmojis and stores their cache. |
| 12 | + * @extends {CachedManager} |
| 13 | + */ |
| 14 | +class ApplicationEmojiManager extends CachedManager { |
| 15 | + constructor(application, iterable) { |
| 16 | + super(application.client, ApplicationEmoji, iterable); |
| 17 | + |
| 18 | + /** |
| 19 | + * The application this manager belongs to |
| 20 | + * @type {ClientApplication} |
| 21 | + */ |
| 22 | + this.application = application; |
| 23 | + } |
| 24 | + |
| 25 | + _add(data, cache) { |
| 26 | + return super._add(data, cache, { extras: [this.application] }); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Options used for creating an emoji of the application |
| 31 | + * @typedef {Object} ApplicationEmojiCreateOptions |
| 32 | + * @property {BufferResolvable|Base64Resolvable} attachment The image for the emoji |
| 33 | + * @property {string} name The name for the emoji |
| 34 | + */ |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a new custom emoji of the application. |
| 38 | + * @param {ApplicationEmojiCreateOptions} options Options for creating the emoji |
| 39 | + * @returns {Promise<Emoji>} The created emoji |
| 40 | + * @example |
| 41 | + * // Create a new emoji from a URL |
| 42 | + * application.emojis.create({ attachment: 'https://i.imgur.com/w3duR07.png', name: 'rip' }) |
| 43 | + * .then(emoji => console.log(`Created new emoji with name ${emoji.name}!`)) |
| 44 | + * .catch(console.error); |
| 45 | + * @example |
| 46 | + * // Create a new emoji from a file on your computer |
| 47 | + * application.emojis.create({ attachment: './memes/banana.png', name: 'banana' }) |
| 48 | + * .then(emoji => console.log(`Created new emoji with name ${emoji.name}!`)) |
| 49 | + * .catch(console.error); |
| 50 | + */ |
| 51 | + async create({ attachment, name }) { |
| 52 | + attachment = await resolveImage(attachment); |
| 53 | + if (!attachment) throw new DiscordjsTypeError(ErrorCodes.ReqResourceType); |
| 54 | + |
| 55 | + const body = { image: attachment, name }; |
| 56 | + |
| 57 | + const emoji = await this.client.rest.post(Routes.applicationEmojis(this.application.id), { body }); |
| 58 | + return this._add(emoji); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Obtains one or more emojis from Discord, or the emoji cache if they're already available. |
| 63 | + * @param {Snowflake} [id] The emoji's id |
| 64 | + * @param {BaseFetchOptions} [options] Additional options for this fetch |
| 65 | + * @returns {Promise<ApplicationEmoji|Collection<Snowflake, ApplicationEmoji>>} |
| 66 | + * @example |
| 67 | + * // Fetch all emojis from the application |
| 68 | + * message.application.emojis.fetch() |
| 69 | + * .then(emojis => console.log(`There are ${emojis.size} emojis.`)) |
| 70 | + * .catch(console.error); |
| 71 | + * @example |
| 72 | + * // Fetch a single emoji |
| 73 | + * message.application.emojis.fetch('222078108977594368') |
| 74 | + * .then(emoji => console.log(`The emoji name is: ${emoji.name}`)) |
| 75 | + * .catch(console.error); |
| 76 | + */ |
| 77 | + async fetch(id, { cache = true, force = false } = {}) { |
| 78 | + if (id) { |
| 79 | + if (!force) { |
| 80 | + const existing = this.cache.get(id); |
| 81 | + if (existing) return existing; |
| 82 | + } |
| 83 | + const emoji = await this.client.rest.get(Routes.applicationEmoji(this.application.id, id)); |
| 84 | + return this._add(emoji, cache); |
| 85 | + } |
| 86 | + |
| 87 | + const { items: data } = await this.client.rest.get(Routes.applicationEmojis(this.application.id)); |
| 88 | + const emojis = new Collection(); |
| 89 | + for (const emoji of data) emojis.set(emoji.id, this._add(emoji, cache)); |
| 90 | + return emojis; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Deletes an emoji. |
| 95 | + * @param {EmojiResolvable} emoji The Emoji resolvable to delete |
| 96 | + * @returns {Promise<void>} |
| 97 | + */ |
| 98 | + async delete(emoji) { |
| 99 | + const id = this.resolveId(emoji); |
| 100 | + if (!id) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'emoji', 'EmojiResolvable', true); |
| 101 | + await this.client.rest.delete(Routes.applicationEmoji(this.application.id, id)); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Edits an emoji. |
| 106 | + * @param {EmojiResolvable} emoji The Emoji resolvable to edit |
| 107 | + * @param {ApplicationEmojiEditOptions} options The options to provide |
| 108 | + * @returns {Promise<ApplicationEmoji>} |
| 109 | + */ |
| 110 | + async edit(emoji, options) { |
| 111 | + const id = this.resolveId(emoji); |
| 112 | + if (!id) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'emoji', 'EmojiResolvable', true); |
| 113 | + |
| 114 | + const newData = await this.client.rest.patch(Routes.applicationEmoji(this.application.id, id), { |
| 115 | + body: { |
| 116 | + name: options.name, |
| 117 | + }, |
| 118 | + }); |
| 119 | + const existing = this.cache.get(id); |
| 120 | + if (existing) { |
| 121 | + existing._patch(newData); |
| 122 | + return existing; |
| 123 | + } |
| 124 | + return this._add(newData); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Fetches the author for this emoji |
| 129 | + * @param {EmojiResolvable} emoji The emoji to fetch the author of |
| 130 | + * @returns {Promise<User>} |
| 131 | + */ |
| 132 | + async fetchAuthor(emoji) { |
| 133 | + const id = this.resolveId(emoji); |
| 134 | + if (!id) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'emoji', 'EmojiResolvable', true); |
| 135 | + |
| 136 | + const data = await this.client.rest.get(Routes.applicationEmoji(this.application.id, id)); |
| 137 | + |
| 138 | + return this._add(data).author; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +module.exports = ApplicationEmojiManager; |
0 commit comments