Add Emoji class

This commit is contained in:
Amish Shah
2016-08-29 15:18:35 +01:00
parent 144638e746
commit 5d06be6333
5 changed files with 91 additions and 3 deletions

File diff suppressed because one or more lines are too long

83
src/structures/Emoji.js Normal file
View File

@@ -0,0 +1,83 @@
const Collection = require('../util/Collection');
const Constants = require('../util/Constants');
/**
* Represents a Custom Emoji
*/
class Emoji {
constructor(guild, data) {
/**
* The Client that instantiated this object
* @type {Client}
*/
this.client = guild.client;
/**
* The Guild this emoji is part of
* @type {Guild}
*/
this.guild = guild;
this.setup(data);
}
setup(data) {
/**
* The ID of the Emoji
* @type {String}
*/
this.id = data.id;
/**
* The name of the Emoji
* @type {String}
*/
this.name = data.name;
this.roleIDS = data.roles;
/**
* Whether or not this emoji requires colons surrounding it
* @type {Boolean}
*/
this.requiresColons = data.require_colons;
/**
* Whether this emoji is managed by an external service
* @type {Boolean}
*/
this.managed = data.managed;
}
/**
* A collection of roles this emoji is active for (empty if all). Mapped by role ID.
* @type {Collection<String, Role>}
* @readonly
*/
get roles() {
const roles = new Collection();
for (const role of this.roleIDS) {
if (this.guild.roles.get(role)) {
roles.set(role, this.guild.roles.get(role));
}
}
return roles;
}
/**
* The URL to the emoji file
* @type {String}
* @readonly
*/
get url() {
return `${Constants.Endpoints.CDN}/emojis/${this.id}.png`;
}
/**
* When concatenated with a String, this automatically returns the emoji mention rather than the object.
* @returns {String}
* @example
* // send an emoji:
* const emoji = guild.emojis.array()[0];
* msg.reply(`Hello! ${emoji}`);
*/
toString() {
return this.requiresColons ? `<:${this.name}:${this.id}>` : this.name;
}
}
module.exports = Emoji;

View File

@@ -4,6 +4,7 @@ const Constants = require('../util/Constants');
const cloneObject = require('../util/CloneObject');
const Role = require('./Role');
const Collection = require('../util/Collection');
const Emoji = require('./Emoji');
function arraysEqual(a, b) {
if (a === b) return true;
@@ -278,7 +279,10 @@ class Guild {
* An array of guild emojis.
* @type {Array<Object>}
*/
this.emojis = data.emojis;
this.emojis = new Collection();
for (const emoji of data.emojis) {
this.emojis.set(emoji.id, new Emoji(this, emoji));
}
/**
* The time in seconds before a user is counted as "away from keyboard".
* @type {?Number}

View File

@@ -80,6 +80,7 @@ const Endpoints = exports.Endpoints = {
logout: `${API}/auth/logout`,
gateway: `${API}/gateway`,
invite: (id) => `${API}/invite/${id}`,
CDN: 'https://cdn.discordapp.com',
// users
user: (userID) => `${API}/users/${userID}`,

View File

@@ -4,7 +4,7 @@ const Discord = require('../');
const request = require('superagent');
const fs = require('fs');
const client = new Discord.Client();
const client = new Discord.Client({ fetch_all_members: false });
client.login(require('./auth.json').token).then(token => console.log('logged in with token ' + token)).catch(console.log);