Set members presence's and fix Activity constructor.

This commit is contained in:
SeanOMik 2020-06-21 00:12:12 -05:00
parent 4a1798c346
commit b40fe05df4
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
3 changed files with 32 additions and 10 deletions

View File

@ -1,6 +1,6 @@
import json, discordobject, channel, member, options, nimcordutils, emoji import json, discordobject, channel, member, options, nimcordutils, emoji
import role, permission, httpcore, strformat, image, asyncdispatch, user import role, permission, httpcore, strformat, image, asyncdispatch, user
import permission import permission, presence, tables
type type
VerificationLevel* = enum VerificationLevel* = enum
@ -244,7 +244,18 @@ proc newGuild*(json: JsonNode): Guild {.inline.} =
if (json.contains("channels")): if (json.contains("channels")):
for channel in json["channels"]: for channel in json["channels"]:
g.channels.insert(newChannel(channel)) g.channels.insert(newChannel(channel))
#TODO: presences if (json.contains("presences")):
# Parse all presences
var tmpPresences = initTable[snowflake, Presence]()
for presence in json["presences"]:
tmpPresences.add(getIDFromJson(presence["user"]["id"].getStr()), newPresence(presence))
# Check if the `tmpPresences` variable has a presence for the member,
# if it does, then update the member to include its presence.
for member in g.members:
if (tmpPresences.hasKey(member.user.id)):
member.presence = tmpPresences[member.user.id]
if (json.contains("max_presences")): if (json.contains("max_presences")):
g.maxPresences = json["max_presences"].getInt() g.maxPresences = json["max_presences"].getInt()
if (json.contains("max_members")): if (json.contains("max_members")):

View File

@ -1,4 +1,4 @@
import discordobject, user, json, role, options, asyncdispatch, nimcordutils, httpcore, strformat, strutils import discordobject, user, json, role, options, asyncdispatch, nimcordutils, httpcore, strformat, strutils, presence
type GuildMember* = ref object of DiscordObject type GuildMember* = ref object of DiscordObject
## This type is a guild member. ## This type is a guild member.
@ -10,6 +10,7 @@ type GuildMember* = ref object of DiscordObject
deaf*: bool ## Whether the user is deafened in voice channels. deaf*: bool ## Whether the user is deafened in voice channels.
mute*: bool ## Whether the user is muted in voice channels. mute*: bool ## Whether the user is muted in voice channels.
guildID*: snowflake ## The guild this member is in. guildID*: snowflake ## The guild this member is in.
presence*: Presence ## The member's presence.
proc newGuildMember*(json: JsonNode, guild: snowflake): GuildMember {.inline.} = proc newGuildMember*(json: JsonNode, guild: snowflake): GuildMember {.inline.} =
## Construct a GuildMember using json. ## Construct a GuildMember using json.

View File

@ -1,4 +1,4 @@
import json, discordobject, emoji, nimcordutils, times import json, discordobject, emoji, nimcordutils, tables, times
type type
ClientStatus* = enum ClientStatus* = enum
@ -12,7 +12,7 @@ type
activityTypeGame = 0, activityTypeGame = 0,
activityTypeStreaming = 1, activityTypeStreaming = 1,
activityTypeListening = 2, activityTypeListening = 2,
activityTypeCustom = 3 activityTypeCustom = 4
ActivityFlag* = enum ActivityFlag* = enum
activityFlagInstance = 0, activityFlagInstance = 0,
@ -59,7 +59,7 @@ type
flags*: uint flags*: uint
Presence* = ref object Presence* = ref object
status*: ClientStatus status*: string
game*: Activity game*: Activity
activities*: seq[Activity] activities*: seq[Activity]
afk*: bool afk*: bool
@ -74,9 +74,6 @@ proc newActivity*(json: JsonNode, guildID: snowflake): Activity =
applicationID: getIDFromJson(json{"application_id"}.getStr()), applicationID: getIDFromJson(json{"application_id"}.getStr()),
details: json{"details"}.getStr(), details: json{"details"}.getStr(),
state: json{"state"}.getStr(), state: json{"state"}.getStr(),
#party?
#assets?
#secrets
instance: json{"instance"}.getBool(), instance: json{"instance"}.getBool(),
flags: uint(json{"flags"}.getInt()), flags: uint(json{"flags"}.getInt()),
) )
@ -122,10 +119,23 @@ proc newActivity*(json: JsonNode, guildID: snowflake): Activity =
if (json["secrets"].contains("match")): if (json["secrets"].contains("match")):
secrets.match = json["secrets"]["match"].getStr() secrets.match = json["secrets"]["match"].getStr()
proc newPresence*(json: JsonNode): Presence =
## Parses Presence type from json.
result = Presence(
status: json["status"].getStr()
)
if (json.contains("game") and json["game"].getFields().len > 0):
result.game = newActivity(json["game"], getIDFromJson(json{"guild_id"}.getStr()))
if json.contains("activities"):
for activity in json["activities"]:
result.activities.add(newActivity(json["game"], getIDFromJson(json{"guild_id"}.getStr())))
proc newPresence*(text: string, `type`: ActivityType, status: ClientStatus, afk: bool = false): Presence = proc newPresence*(text: string, `type`: ActivityType, status: ClientStatus, afk: bool = false): Presence =
## Used to create a presence that you can use to update the presence of your bot's user. ## Used to create a presence that you can use to update the presence of your bot's user.
return Presence( return Presence(
status: status, status: $status,
afk: afk, afk: afk,
game: Activity( game: Activity(
name: text, name: text,