aboutsummaryrefslogtreecommitdiff
path: root/src/model/guild/emoji.rs
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-03-25 15:41:47 -0700
committerZeyla Hellyer <[email protected]>2017-03-25 15:41:47 -0700
commit9114963daf708cfaeaf54d8c788206ccfbae5df8 (patch)
tree08d6aba5b8ad40189b312865ac776bb6fa30c45d /src/model/guild/emoji.rs
parentAdd slightly more documentation (diff)
downloadserenity-9114963daf708cfaeaf54d8c788206ccfbae5df8.tar.xz
serenity-9114963daf708cfaeaf54d8c788206ccfbae5df8.zip
Rework the models directory
Diffstat (limited to 'src/model/guild/emoji.rs')
-rw-r--r--src/model/guild/emoji.rs100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/model/guild/emoji.rs b/src/model/guild/emoji.rs
new file mode 100644
index 0000000..43e57a2
--- /dev/null
+++ b/src/model/guild/emoji.rs
@@ -0,0 +1,100 @@
+use serde_json::builder::ObjectBuilder;
+use std::fmt::{Display, Formatter, Result as FmtResult, Write as FmtWrite};
+use std::mem;
+use ::client::{CACHE, rest};
+use ::model::{Emoji, EmojiId, GuildId};
+use ::internal::prelude::*;
+
+impl Emoji {
+ /// Deletes the emoji.
+ ///
+ /// **Note**: The [Manage Emojis] permission is required.
+ ///
+ /// **Note**: Only user accounts may use this method.
+ ///
+ /// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html
+ #[cfg(feature="cache")]
+ pub fn delete(&self) -> Result<()> {
+ match self.find_guild_id() {
+ Some(guild_id) => rest::delete_emoji(guild_id.0, self.id.0),
+ None => Err(Error::Client(ClientError::ItemMissing)),
+ }
+ }
+
+ /// Edits the emoji by updating it with a new name.
+ ///
+ /// **Note**: The [Manage Emojis] permission is required.
+ ///
+ /// **Note**: Only user accounts may use this method.
+ ///
+ /// [Manage Emojis]: permissions/constant.MANAGE_EMOJIS.html
+ #[cfg(feature="cache")]
+ pub fn edit(&mut self, name: &str) -> Result<()> {
+ match self.find_guild_id() {
+ Some(guild_id) => {
+ let map = ObjectBuilder::new()
+ .insert("name", name)
+ .build();
+
+ match rest::edit_emoji(guild_id.0, self.id.0, &map) {
+ Ok(emoji) => {
+ mem::replace(self, emoji);
+
+ Ok(())
+ },
+ Err(why) => Err(why),
+ }
+ },
+ None => Err(Error::Client(ClientError::ItemMissing)),
+ }
+ }
+
+ /// Finds the [`Guild`] that owns the emoji by looking through the Cache.
+ ///
+ /// [`Guild`]: struct.Guild.html
+ #[cfg(feature="cache")]
+ pub fn find_guild_id(&self) -> Option<GuildId> {
+ for guild in CACHE.read().unwrap().guilds.values() {
+ let guild = guild.read().unwrap();
+
+ if guild.emojis.contains_key(&self.id) {
+ return Some(guild.id);
+ }
+ }
+
+ None
+ }
+
+ /// Generates a URL to the emoji's image.
+ #[inline]
+ pub fn url(&self) -> String {
+ format!(cdn!("/emojis/{}.png"), self.id)
+ }
+}
+
+impl Display for Emoji {
+ /// Formats the emoji into a string that will cause Discord clients to
+ /// render the emoji.
+ ///
+ /// This is in the format of: `<:NAME:EMOJI_ID>`.
+ fn fmt(&self, f: &mut Formatter) -> FmtResult {
+ f.write_str("<:")?;
+ f.write_str(&self.name)?;
+ FmtWrite::write_char(f, ':')?;
+ Display::fmt(&self.id, f)?;
+ FmtWrite::write_char(f, '>')
+ }
+}
+
+impl Display for EmojiId {
+ fn fmt(&self, f: &mut Formatter) -> FmtResult {
+ Display::fmt(&self.0, f)
+ }
+}
+
+impl From<Emoji> for EmojiId {
+ /// Gets the Id of an `Emoji`.
+ fn from(emoji: Emoji) -> EmojiId {
+ emoji.id
+ }
+}