mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 20:13:30 +01:00
Add full guild emoji functionality (#749)
* all the emoji stuff
* fix things for hydra 😘
* feck i need to stop committing on github
* update docs again
* Butts
This commit is contained in:
committed by
Schuyler Cebulskie
parent
682e33cad9
commit
aed75e1f9a
File diff suppressed because one or more lines are too long
@@ -3,6 +3,7 @@ const cloneObject = require('../util/CloneObject');
|
|||||||
const Guild = require('../structures/Guild');
|
const Guild = require('../structures/Guild');
|
||||||
const User = require('../structures/User');
|
const User = require('../structures/User');
|
||||||
const DMChannel = require('../structures/DMChannel');
|
const DMChannel = require('../structures/DMChannel');
|
||||||
|
const Emoji = require('../structures/Emoji');
|
||||||
const TextChannel = require('../structures/TextChannel');
|
const TextChannel = require('../structures/TextChannel');
|
||||||
const VoiceChannel = require('../structures/VoiceChannel');
|
const VoiceChannel = require('../structures/VoiceChannel');
|
||||||
const GuildChannel = require('../structures/GuildChannel');
|
const GuildChannel = require('../structures/GuildChannel');
|
||||||
@@ -73,6 +74,26 @@ class ClientDataManager {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newEmoji(data, guild) {
|
||||||
|
const already = guild.emojis.has(data.id);
|
||||||
|
if (data && !already) {
|
||||||
|
let emoji = new Emoji(guild, data);
|
||||||
|
this.client.emit(Constants.Events.EMOJI_CREATE, emoji);
|
||||||
|
guild.emojis.set(emoji.id, emoji);
|
||||||
|
return emoji;
|
||||||
|
} else if (already) {
|
||||||
|
return guild.emojis.get(data.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
killEmoji(emoji) {
|
||||||
|
if (!(emoji instanceof Emoji && emoji.guild)) return;
|
||||||
|
this.client.emit(Constants.Events.EMOJI_DELETE, emoji);
|
||||||
|
emoji.guild.emojis.delete(emoji.id);
|
||||||
|
}
|
||||||
|
|
||||||
killGuild(guild) {
|
killGuild(guild) {
|
||||||
const already = this.client.guilds.has(guild.id);
|
const already = this.client.guilds.has(guild.id);
|
||||||
this.client.guilds.delete(guild.id);
|
this.client.guilds.delete(guild.id);
|
||||||
|
|||||||
@@ -20,6 +20,9 @@ class ActionsManager {
|
|||||||
this.register('UserGet');
|
this.register('UserGet');
|
||||||
this.register('UserUpdate');
|
this.register('UserUpdate');
|
||||||
this.register('GuildSync');
|
this.register('GuildSync');
|
||||||
|
this.register('GuildEmojiCreate');
|
||||||
|
this.register('GuildEmojiDelete');
|
||||||
|
this.register('GuildEmojiUpdate');
|
||||||
}
|
}
|
||||||
|
|
||||||
register(name) {
|
register(name) {
|
||||||
|
|||||||
18
src/client/actions/GuildEmojiCreate.js
Normal file
18
src/client/actions/GuildEmojiCreate.js
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
const Action = require('./Action');
|
||||||
|
|
||||||
|
class EmojiCreateAction extends Action {
|
||||||
|
handle(data, guild) {
|
||||||
|
const client = this.client;
|
||||||
|
const emoji = client.dataManager.newEmoji(data, guild);
|
||||||
|
return {
|
||||||
|
emoji,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emitted whenever an emoji is created
|
||||||
|
* @event Client#guildEmojiCreate
|
||||||
|
* @param {Emoji} emoji The emoji that was created.
|
||||||
|
*/
|
||||||
|
module.exports = EmojiCreateAction;
|
||||||
18
src/client/actions/GuildEmojiDelete.js
Normal file
18
src/client/actions/GuildEmojiDelete.js
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
const Action = require('./Action');
|
||||||
|
|
||||||
|
class EmojiDeleteAction extends Action {
|
||||||
|
handle(data) {
|
||||||
|
const client = this.client;
|
||||||
|
client.dataManager.killEmoji(data);
|
||||||
|
return {
|
||||||
|
data,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emitted whenever an emoji is deleted
|
||||||
|
* @event Client#guildEmojiDelete
|
||||||
|
* @param {Emoji} emoji The emoji that was deleted.
|
||||||
|
*/
|
||||||
|
module.exports = EmojiDeleteAction;
|
||||||
27
src/client/actions/GuildEmojiUpdate.js
Normal file
27
src/client/actions/GuildEmojiUpdate.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
const Action = require('./Action');
|
||||||
|
const Constants = require('../../util/Constants');
|
||||||
|
|
||||||
|
class GuildEmojiUpdateAction extends Action {
|
||||||
|
handle(data, guild) {
|
||||||
|
const client = this.client;
|
||||||
|
for (let emoji of data.emojis) {
|
||||||
|
const already = guild.emojis.has(emoji.id);
|
||||||
|
emoji = client.dataManager.newEmoji(emoji, guild);
|
||||||
|
if (already) client.emit(Constants.Events.GUILD_EMOJI_UPDATE, guild, emoji);
|
||||||
|
}
|
||||||
|
for (let emoji of guild.emojis) {
|
||||||
|
if (!data.emoijs.has(emoji.id)) client.dataManager.killEmoji(emoji);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
emojis: data.emojis,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emitted whenever an emoji is updated
|
||||||
|
* @event Client#guildEmojiUpdate
|
||||||
|
* @param {Guild} guild The guild that the emoji was updated in.
|
||||||
|
* @param {Emoji} emoji The emoji that was updated.
|
||||||
|
*/
|
||||||
|
module.exports = GuildEmojiUpdateAction;
|
||||||
@@ -7,7 +7,6 @@ const User = requireStructure('User');
|
|||||||
const GuildMember = requireStructure('GuildMember');
|
const GuildMember = requireStructure('GuildMember');
|
||||||
const Role = requireStructure('Role');
|
const Role = requireStructure('Role');
|
||||||
const Invite = requireStructure('Invite');
|
const Invite = requireStructure('Invite');
|
||||||
const Emoji = requireStructure('Emoji');
|
|
||||||
|
|
||||||
class RESTMethods {
|
class RESTMethods {
|
||||||
constructor(restManager) {
|
constructor(restManager) {
|
||||||
@@ -522,15 +521,17 @@ class RESTMethods {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.rest.makeRequest('post', `${Constants.Endpoints.guildEmojis(guild.id)}`, true, { name: name, image: image })
|
this.rest.makeRequest('post', `${Constants.Endpoints.guildEmojis(guild.id)}`, true, { name: name, image: image })
|
||||||
.then(data => {
|
.then(data => {
|
||||||
resolve(new Emoji(guild, data));
|
resolve(this.rest.client.actions.EmojiCreate.handle(data, guild).emoji);
|
||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteEmoji(guild, id) {
|
deleteEmoji(emoji) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.rest.makeRequest('delete', `${Constants.Endpoints.guildEmojis(guild.id)}/${id}`, true)
|
this.rest.makeRequest('delete', `${Constants.Endpoints.guildEmojis(emoji.guild.id)}/${emoji.id}`, true)
|
||||||
.then(resolve).catch(reject);
|
.then(() => {
|
||||||
|
resolve(this.rest.client.actions.EmojiDelete.handle(emoji).data);
|
||||||
|
}).catch(reject);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
13
src/client/websocket/packets/handlers/GuildEmojiUpdate.js
Normal file
13
src/client/websocket/packets/handlers/GuildEmojiUpdate.js
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
const AbstractHandler = require('./AbstractHandler');
|
||||||
|
|
||||||
|
class GuildEmojiUpdate extends AbstractHandler {
|
||||||
|
handle(packet) {
|
||||||
|
const client = this.packetManager.client;
|
||||||
|
const data = packet.d;
|
||||||
|
const guild = client.guilds.get(data.guild_id);
|
||||||
|
if (!guild) return;
|
||||||
|
client.actions.EmojiUpdate.handle(data, guild);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = GuildEmojiUpdate;
|
||||||
@@ -592,8 +592,8 @@ class Guild {
|
|||||||
* @returns {Promise}
|
* @returns {Promise}
|
||||||
*/
|
*/
|
||||||
deleteEmoji(emoji) {
|
deleteEmoji(emoji) {
|
||||||
if (emoji instanceof Emoji) emoji = emoji.id;
|
if (!(emoji instanceof Emoji)) emoji = this.emojis.get(emoji);
|
||||||
return this.client.rest.methods.deleteEmoji(this, emoji);
|
return this.client.rest.methods.deleteEmoji(emoji);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -158,6 +158,9 @@ exports.Events = {
|
|||||||
GUILD_ROLE_DELETE: 'guildRoleDelete',
|
GUILD_ROLE_DELETE: 'guildRoleDelete',
|
||||||
GUILD_ROLE_UPDATE: 'guildRoleUpdate',
|
GUILD_ROLE_UPDATE: 'guildRoleUpdate',
|
||||||
GUILD_MEMBER_AVAILABLE: 'guildMemberAvailable',
|
GUILD_MEMBER_AVAILABLE: 'guildMemberAvailable',
|
||||||
|
GUILD_EMOJI_CREATE: 'guildEmojiCreate',
|
||||||
|
GUILD_EMOJI_DELETE: 'guildEmojiDelete',
|
||||||
|
GUILD_EMOJI_UPDATE: 'guildEmojiUpdate',
|
||||||
CHANNEL_CREATE: 'channelCreate',
|
CHANNEL_CREATE: 'channelCreate',
|
||||||
CHANNEL_DELETE: 'channelDelete',
|
CHANNEL_DELETE: 'channelDelete',
|
||||||
CHANNEL_UPDATE: 'channelUpdate',
|
CHANNEL_UPDATE: 'channelUpdate',
|
||||||
|
|||||||
Reference in New Issue
Block a user