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

View File

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