Add Cache.getUser proc and add some documentation.

This commit is contained in:
SeanOMik 2020-06-20 00:34:43 -05:00
parent d88e31e3c3
commit 24fea025a1
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
2 changed files with 34 additions and 16 deletions

View File

@ -1,4 +1,4 @@
import sequtils, message, member, channel, guild, discordobject, nimcordutils, httpcore
import message, member, channel, guild, discordobject, nimcordutils, httpcore, user
type Cache* = ref object
members*: seq[GuildMember]
@ -6,36 +6,53 @@ type Cache* = ref object
channels*: seq[Channel]
guilds*: seq[Guild]
proc getChannel*(cache: Cache, id: snowflake): Channel =
for index, channel in cache.channels:
proc getChannel*(cache: var Cache, id: snowflake): Channel =
## Get a channel object from the id.
##
## If for some reason the channel is not in cache, it gets requested via the
## Discord REST API.
for channel in cache.channels:
if (channel.id == id):
return channel
return newChannel(sendRequest(endpoint("/channels/" & $id), HttpGet, defaultHeaders(),
result = newChannel(sendRequest(endpoint("/channels/" & $id), HttpGet, defaultHeaders(),
id, RateLimitBucketType.channel))
cache.channels.add(result)
proc getMessageChannel*(msg: Message, cache: Cache): Channel =
proc getMessageChannel*(msg: Message, cache: var Cache): Channel =
## Get a message's channel object.
##
## If for some reason the channel is not in cache, it gets requested via the
## Discord REST API.
return cache.getChannel(msg.channelID)
proc getGuild*(cache: Cache, id: snowflake): Guild =
for index, guild in cache.guilds:
proc getGuild*(cache: var Cache, id: snowflake): Guild =
## Get a guild object from it's id.
##
## If for some reason the guild is not in cache, it gets requested via the
## Discord REST API.
for guild in cache.guilds:
if (guild.id == id):
return guild
return newGuild(sendRequest(endpoint("/guilds/" & $id), HttpGet, defaultHeaders(),
result = newGuild(sendRequest(endpoint("/guilds/" & $id), HttpGet, defaultHeaders(),
id, RateLimitBucketType.guild))
cache.guilds.add(result)
proc getChannelGuild*(channel: Channel, cache: Cache): Guild =
proc getChannelGuild*(channel: Channel, cache: var Cache): Guild =
## Get a channels's guild object.
##
## If for some reason the guild is not in cache, it gets requested via the
## Discord REST API.
return cache.getGuild(channel.guildID)
#[ proc getGuildMember*(guild: Guild, memberID: snowflake, cache: Cache): GuildMember =
for index, members in cache.members:
if (members.id == id):
return guild
return newGuildMember(sendRequest(endpoint("/guilds/" & $guild.id & "/members/" & $memberID),
HttpGet, defaultHeaders(), id, RateLimitBucketType.guild)) ]#
proc getUser*(cache: Cache, id: snowflake): User =
## Get a user object from it's id.
##
## If for some reason the user is not in cache, it gets requested via the
## Discord REST API.
for member in cache.members:
if (member.user.id == id):
return member.user
return newUser(sendRequest(endpoint("/users/" & $id), HttpGet, defaultHeaders()))

View File

@ -40,6 +40,7 @@ let internalEventTable: Table[string, proc(discordClient: DiscordClient, json: J
}.toTable
proc handleDiscordEvent*(discordClient: DiscordClient, json: JsonNode, eventName: string) {.async.} =
## Handles, and dispatches, a gateway event. Only used internally.
if (internalEventTable.hasKey(eventName)):
let eventProc: proc(discordClient: DiscordClient, json: JsonNode) = internalEventTable[eventName]
eventProc(discordClient, json)