2020-06-18 03:37:21 +00:00
|
|
|
import sequtils, message, member, channel, guild, discordobject, nimcordutils, httpcore
|
2020-06-18 03:18:54 +00:00
|
|
|
|
|
|
|
type Cache* = ref object
|
|
|
|
members*: seq[GuildMember]
|
|
|
|
messages*: seq[Message]
|
|
|
|
channels*: seq[Channel]
|
|
|
|
guilds*: seq[Guild]
|
|
|
|
|
2020-06-19 02:24:15 +00:00
|
|
|
proc getChannel*(cache: Cache, id: snowflake): Channel =
|
2020-06-18 03:18:54 +00:00
|
|
|
for index, channel in cache.channels:
|
2020-06-19 02:24:15 +00:00
|
|
|
if (channel.id == id):
|
2020-06-18 03:18:54 +00:00
|
|
|
return channel
|
2020-06-19 02:24:15 +00:00
|
|
|
|
|
|
|
return newChannel(sendRequest(endpoint("/channels/" & $id), HttpGet, defaultHeaders(),
|
|
|
|
id, RateLimitBucketType.channel))
|
2020-06-18 03:18:54 +00:00
|
|
|
|
2020-06-19 02:24:15 +00:00
|
|
|
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)
|
2020-06-18 03:18:54 +00:00
|
|
|
|
2020-06-19 02:24:15 +00:00
|
|
|
proc getGuild*(cache: Cache, id: snowflake): Guild =
|
2020-06-18 03:18:54 +00:00
|
|
|
for index, guild in cache.guilds:
|
2020-06-19 02:24:15 +00:00
|
|
|
if (guild.id == id):
|
2020-06-18 03:18:54 +00:00
|
|
|
return guild
|
|
|
|
|
2020-06-19 02:24:15 +00:00
|
|
|
return newGuild(sendRequest(endpoint("/guild/" & $id), HttpGet, defaultHeaders(),
|
|
|
|
id, RateLimitBucketType.guild))
|
|
|
|
|
|
|
|
proc getChannelGuild*(channel: Channel, cache: Cache): Guild =
|
|
|
|
return cache.getGuild(channel.guildID)
|