Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Sticker): updates, sticker packs, and guild stickers (#5867)
Co-authored-by: SpaceEEC <spaceeec@yahoo.com> Co-authored-by: Antonio Román <kyradiscord@gmail.com> Co-authored-by: Tiemen <ThaTiemsz@users.noreply.github.com> Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com> Co-authored-by: BannerBomb <BannerBomb55@gmail.com> Co-authored-by: Noel <icrawltogo@gmail.com> Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
- Loading branch information
1 parent
76888e6
commit 54d6a3a
Showing
23 changed files
with
771 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
const Action = require('./Action'); | ||
const { Events } = require('../../util/Constants'); | ||
|
||
class GuildStickerCreateAction extends Action { | ||
handle(guild, createdSticker) { | ||
const already = guild.stickers.cache.has(createdSticker.id); | ||
const sticker = guild.stickers._add(createdSticker); | ||
/** | ||
* Emitted whenever a custom sticker is created in a guild. | ||
* @event Client#stickerCreate | ||
* @param {Sticker} sticker The sticker that was created | ||
*/ | ||
if (!already) this.client.emit(Events.GUILD_STICKER_CREATE, sticker); | ||
return { sticker }; | ||
} | ||
} | ||
|
||
module.exports = GuildStickerCreateAction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
const Action = require('./Action'); | ||
const { Events } = require('../../util/Constants'); | ||
|
||
class GuildStickerDeleteAction extends Action { | ||
handle(sticker) { | ||
sticker.guild.stickers.cache.delete(sticker.id); | ||
sticker.deleted = true; | ||
/** | ||
* Emitted whenever a custom sticker is deleted in a guild. | ||
* @event Client#stickerDelete | ||
* @param {Sticker} sticker The sticker that was deleted | ||
*/ | ||
this.client.emit(Events.GUILD_STICKER_DELETE, sticker); | ||
return { sticker }; | ||
} | ||
} | ||
|
||
module.exports = GuildStickerDeleteAction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
const Action = require('./Action'); | ||
const { Events } = require('../../util/Constants'); | ||
|
||
class GuildStickerUpdateAction extends Action { | ||
handle(current, data) { | ||
const old = current._update(data); | ||
/** | ||
* Emitted whenever a custom sticker is updated in a guild. | ||
* @event Client#stickerUpdate | ||
* @param {Sticker} oldSticker The old sticker | ||
* @param {Sticker} newSticker The new sticker | ||
*/ | ||
this.client.emit(Events.GUILD_STICKER_UPDATE, old, current); | ||
return { sticker: current }; | ||
} | ||
} | ||
|
||
module.exports = GuildStickerUpdateAction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
const Action = require('./Action'); | ||
|
||
class GuildStickersUpdateAction extends Action { | ||
handle(data) { | ||
const guild = this.client.guilds.cache.get(data.guild_id); | ||
if (!guild?.stickers) return; | ||
|
||
const deletions = new Map(guild.stickers.cache); | ||
|
||
for (const sticker of data.stickers) { | ||
// Determine type of sticker event | ||
const cachedSticker = guild.stickers.cache.get(sticker.id); | ||
if (cachedSticker) { | ||
deletions.delete(sticker.id); | ||
if (!cachedSticker.equals(sticker)) { | ||
// Sticker updated | ||
this.client.actions.GuildStickerUpdate.handle(cachedSticker, sticker); | ||
} | ||
} else { | ||
// Sticker added | ||
this.client.actions.GuildStickerCreate.handle(guild, sticker); | ||
} | ||
} | ||
|
||
for (const sticker of deletions.values()) { | ||
// Sticker deleted | ||
this.client.actions.GuildStickerDelete.handle(sticker); | ||
} | ||
} | ||
} | ||
|
||
module.exports = GuildStickersUpdateAction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = (client, packet) => { | ||
client.actions.GuildStickersUpdate.handle(packet.d); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
'use strict'; | ||
|
||
const CachedManager = require('./CachedManager'); | ||
const { TypeError } = require('../errors'); | ||
const MessagePayload = require('../structures/MessagePayload'); | ||
const Sticker = require('../structures/Sticker'); | ||
const Collection = require('../util/Collection'); | ||
|
||
/** | ||
* Manages API methods for Guild Stickers and stores their cache. | ||
* @extends {CachedManager} | ||
*/ | ||
class GuildStickerManager extends CachedManager { | ||
constructor(guild, iterable) { | ||
super(guild.client, Sticker, iterable); | ||
|
||
/** | ||
* The guild this manager belongs to | ||
* @type {Guild} | ||
*/ | ||
this.guild = guild; | ||
} | ||
|
||
/** | ||
* The cache of Guild Stickers | ||
* @type {Collection<Snowflake, Sticker>} | ||
* @name GuildStickerManager#cache | ||
*/ | ||
|
||
_add(data, cache) { | ||
return super._add(data, cache, { extras: [this.guild] }); | ||
} | ||
|
||
/** | ||
* Creates a new custom sticker in the guild. | ||
* @param {BufferResolvable|Stream|FileOptions|MessageAttachment} file The file for the sticker | ||
* @param {string} name The name for the sticker | ||
* @param {string} tags The Discord name of a unicode emoji representing the sticker's expression | ||
* @param {Object} [options] Options | ||
* @param {?string} [options.description] The description for the sticker | ||
* @param {string} [options.reason] Reason for creating the sticker | ||
* @returns {Promise<Sticker>} The created sticker | ||
* @example | ||
* // Create a new sticker from a url | ||
* guild.stickers.create('https://i.imgur.com/w3duR07.png', 'rip') | ||
* .then(sticker => console.log(`Created new sticker with name ${sticker.name}!`)) | ||
* .catch(console.error); | ||
* @example | ||
* // Create a new sticker from a file on your computer | ||
* guild.stickers.create('./memes/banana.png', 'banana') | ||
* .then(sticker => console.log(`Created new sticker with name ${sticker.name}!`)) | ||
* .catch(console.error); | ||
*/ | ||
async create(file, name, tags, { description, reason } = {}) { | ||
file = { ...(await MessagePayload.resolveFile(file)), key: 'file' }; | ||
if (!file) throw new TypeError('REQ_RESOURCE_TYPE'); | ||
|
||
const data = { name, tags, description: description ?? '' }; | ||
|
||
return this.client.api | ||
.guilds(this.guild.id) | ||
.stickers.post({ data, files: [file], reason, dontUsePayloadJSON: true }) | ||
.then(sticker => this.client.actions.GuildStickerCreate.handle(this.guild, sticker).sticker); | ||
} | ||
|
||
/** | ||
* Data that resolves to give a Sticker object. This can be: | ||
* * An Sticker object | ||
* * A Snowflake | ||
* @typedef {Sticker|Snowflake} StickerResolvable | ||
*/ | ||
|
||
/** | ||
* Resolves an StickerResolvable to a Sticker object. | ||
* @method resolve | ||
* @memberof GuildStickerManager | ||
* @instance | ||
* @param {StickerResolvable} sticker The Sticker resolvable to identify | ||
* @returns {?Sticker} | ||
*/ | ||
|
||
/** | ||
* Resolves an StickerResolvable to an Sticker id string. | ||
* @method resolveId | ||
* @memberof GuildStickerManager | ||
* @instance | ||
* @param {StickerResolvable} sticker The Sticker resolvable to identify | ||
* @returns {?Snowflake} | ||
*/ | ||
|
||
/** | ||
* Edits a sticker. | ||
* @param {StickerResolvable} sticker The sticker to edit | ||
* @param {GuildStickerEditData} [data] The new data for the sticker | ||
* @param {string} [reason] Reason for editing this sticker | ||
* @returns {Promise<Sticker>} | ||
*/ | ||
async edit(sticker, data, reason) { | ||
const stickerId = this.resolveId(sticker); | ||
if (!stickerId) throw new TypeError('INVALID_TYPE', 'sticker', 'StickerResolvable'); | ||
|
||
const d = await this.client.api.guilds(this.guild.id).stickers(stickerId).patch({ | ||
data, | ||
reason, | ||
}); | ||
|
||
const existing = this.cache.get(stickerId); | ||
if (existing) { | ||
const clone = existing._clone(); | ||
clone._patch(d); | ||
return clone; | ||
} | ||
return this._add(d); | ||
} | ||
|
||
/** | ||
* Deletes a sticker. | ||
* @param {StickerResolvable} sticker The sticker to delete | ||
* @param {string} [reason] Reason for deleting this sticker | ||
* @returns {Promise<void>} | ||
*/ | ||
async delete(sticker, reason) { | ||
sticker = this.resolveId(sticker); | ||
if (!sticker) throw new TypeError('INVALID_TYPE', 'sticker', 'StickerResolvable'); | ||
|
||
await this.client.api.guilds(this.guild.id).stickers(sticker).delete({ reason }); | ||
} | ||
|
||
/** | ||
* Obtains one or more stickers from Discord, or the sticker cache if they're already available. | ||
* @param {Snowflake} [id] The Sticker's id | ||
* @param {BaseFetchOptions} [options] Additional options for this fetch | ||
* @returns {Promise<Sticker|Collection<Snowflake, Sticker>>} | ||
* @example | ||
* // Fetch all stickers from the guild | ||
* message.guild.stickers.fetch() | ||
* .then(stickers => console.log(`There are ${stickers.size} stickers.`)) | ||
* .catch(console.error); | ||
* @example | ||
* // Fetch a single sticker | ||
* message.guild.stickers.fetch('222078108977594368') | ||
* .then(sticker => console.log(`The sticker name is: ${sticker.name}`)) | ||
* .catch(console.error); | ||
*/ | ||
async fetch(id, { cache = true, force = false } = {}) { | ||
if (id) { | ||
if (!force) { | ||
const existing = this.cache.get(id); | ||
if (existing) return existing; | ||
} | ||
const sticker = await this.client.api.guilds(this.guild.id).stickers(id).get(); | ||
return this._add(sticker, cache); | ||
} | ||
|
||
const data = await this.client.api.guilds(this.guild.id).stickers.get(); | ||
return new Collection(data.map(sticker => [sticker.id, this._add(sticker, cache)])); | ||
} | ||
} | ||
|
||
module.exports = GuildStickerManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.