From b8c61487a249c934dff0e4275a658b72d4fb8c82 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Thu, 18 Jun 2020 15:31:12 -0500 Subject: [PATCH] Add emoji type --- src/emoji.nim | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/emoji.nim diff --git a/src/emoji.nim b/src/emoji.nim new file mode 100644 index 0000000..140b8dc --- /dev/null +++ b/src/emoji.nim @@ -0,0 +1,58 @@ +import json, discordobject, nimcordutils, user, httpcore, asyncdispatch, strutils, uri + +type + Emoji* = object of DiscordObject + name*: string + roles*: seq[snowflake] + user*: User + requireColons: bool + managed: bool + animated: bool + available: bool + +proc newEmoji*(json: JsonNode): Emoji = + result = Emoji( + id: getIDFromJson(json["id"].getStr()), + name: json["name"].getStr() + ) + + if (json.contains("roles")): + for role in json["roles"]: + result.roles.add(getIDFromJson(role.getStr())) + if (json.contains("user")): + result.user = newUser(json["user"]) + if (json.contains("require_colons")): + result.requireColons = json["require_colons"].getBool() + if (json.contains("managed")): + result.managed = json["managed"].getBool() + if (json.contains("animated")): + result.requireColons = json["animated"].getBool() + if (json.contains("available")): + result.requireColons = json["available"].getBool() + +proc newEmoji*(name: string, id: snowflake): Emoji = + return Emoji(name: name, id: id) + +proc newEmoji*(unicode: string): Emoji = + return Emoji(name: unicode) + +proc `$`*(emoji: Emoji): string = + ## Converts the emoji to a string to use in text. + + # Check if the emoji has a name but not id. + # If its true, this means that the emoji is just a unicode + # representation of the emoji. + if (emoji.id == 0 and not emoji.name.isEmptyOrWhitespace()): + return emoji.name + else: + result = $emoji.id & ":" & emoji.name + + # If the emoji must be wrapped in colons, wrap it! + if emoji.requireColons: + result = ":" & result & ":" + +proc toUrlEncoding*(emoji: Emoji): string = + ## Converts the emoji to be used in a url. + ## Not needed for users, only for internal + ## library use. + return encodeUrl($emoji, true) \ No newline at end of file