This repository has been archived on 2023-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
nimcord/src/cache.nim

33 lines
1.2 KiB
Nim
Raw Normal View History

2020-06-18 03:37:21 +00:00
import sequtils, message, member, channel, guild, discordobject, nimcordutils, httpcore
type Cache* = ref object
members*: seq[GuildMember]
messages*: seq[Message]
channels*: seq[Channel]
guilds*: seq[Guild]
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))
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)