Add emoji type

This commit is contained in:
SeanOMik 2020-06-18 15:31:12 -05:00
parent f1ec478904
commit b8c61487a2
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
1 changed files with 58 additions and 0 deletions

58
src/emoji.nim Normal file
View File

@ -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)