diff --git a/src/cache.nim b/src/cache.nim index 5089814..5072885 100644 --- a/src/cache.nim +++ b/src/cache.nim @@ -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)) ]# \ No newline at end of file +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())) \ No newline at end of file diff --git a/src/eventdispatcher.nim b/src/eventdispatcher.nim index 8d6be6e..c0689af 100644 --- a/src/eventdispatcher.nim +++ b/src/eventdispatcher.nim @@ -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)