From a665d000307a90fa3e03e60921fbdffa82583775 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Thu, 18 Jun 2020 21:24:15 -0500 Subject: [PATCH] Add a getGuild proc and most some things around. --- src/cache.nim | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/cache.nim b/src/cache.nim index edb32ec..d7853c2 100644 --- a/src/cache.nim +++ b/src/cache.nim @@ -6,24 +6,28 @@ type Cache* = ref object channels*: seq[Channel] guilds*: seq[Guild] -proc getMessageChannel*(msg: Message, cache: Cache): Channel = - for index, channel in cache.channels: - if (channel.id == msg.channelID): - return channel - - return nil - -proc getChannelGuild*(channel: Channel, cache: Cache): Guild = - for index, guild in cache.guilds: - if (guild.id == channel.guildID): - return guild - - return nil - proc getChannel*(cache: Cache, id: snowflake): Channel = for index, channel in cache.channels: if (channel.id == id): return channel - return newChannel(sendRequest(endpoint("/channels/" & $id), HttpGet, - defaultHeaders(), id, RateLimitBucketType.channel)) \ No newline at end of file + return newChannel(sendRequest(endpoint("/channels/" & $id), HttpGet, defaultHeaders(), + id, RateLimitBucketType.channel)) + +proc getMessageChannel*(msg: Message, cache: 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: + if (guild.id == id): + return guild + + return newGuild(sendRequest(endpoint("/guild/" & $id), HttpGet, defaultHeaders(), + id, RateLimitBucketType.guild)) + +proc getChannelGuild*(channel: Channel, cache: Cache): Guild = + return cache.getGuild(channel.guildID) \ No newline at end of file