From 0e1cb7280aa212691a0803275a5d24d6b89ec47a Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Wed, 17 Jun 2020 23:51:38 -0500 Subject: [PATCH] Add Channel.getMessages method --- src/channel.nim | 48 +++++++++++++++++++++++++++++++++++++++++++++++- src/client.nim | 15 ++++++++------- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/channel.nim b/src/channel.nim index e87c188..b5dd7c8 100644 --- a/src/channel.nim +++ b/src/channel.nim @@ -142,4 +142,50 @@ proc modifyChannel*(channel: Channel, modify: ChannelModify): Future[Channel] {. proc deleteChannel*(channel: Channel) {.async.} = ## Delete the channel. discard sendRequest(endpoint("/channels/" & $channel.id), HttpDelete, - defaultHeaders(), channel.id, RateLimitBucketType.channel) \ No newline at end of file + defaultHeaders(), channel.id, RateLimitBucketType.channel) + +type MessagesGetRequest* = object + ## Use this type to get a channel's messages by setting some of the fields. + ## You can only set one of `around`, `before`, or `after`. + around*: Option[snowflake] + before*: Option[snowflake] + after*: Option[snowflake] + limit*: Option[int] + +proc getMessages*(channel: Channel, request: MessagesGetRequest): seq[Message] = + ## Gets messages in the channel. + ## + ## Examples: + ## .. code-block:: nim + ## var chan = getChannel(703084913510973472) + ## channel.getMessages(MessagesGetRequest(limit: some(15), before: some(723030179760570428))) + + var url: string = endpoint("/channels/" & $channel.id & "/messages?") + + if (request.around.isSome): + url = url & "around=" & $request.around.get() + + # Raise some exceptions to make sure the user doesn't + # try to set more than one of these fields + if (request.before.isSome): + if (request.around.isSome): + raise newException(Defect, "You cannot get around and before a message! Choose one...") + url = url & "before=" & $request.before.get() + + if (request.after.isSome): + if (request.around.isSome or request.before.isSome): + raise newException(Defect, "You cannot get around/before and after a message! Choose one...") + url = url & "after=" & $request.after.get() + + if (request.limit.isSome): + # Add the `&` for the url if something else is set. + if (request.around.isSome or request.before.isSome or request.after.isSome): + url = url & "&" + + url = url & "limit=" & $request.limit.get() + + let response = sendRequest(url, HttpGet, defaultHeaders(newHttpHeaders()), + channel.id, RateLimitBucketType.channel) + + for message in response: + result.add(newMessage(message)) \ No newline at end of file diff --git a/src/client.nim b/src/client.nim index 549f263..aeb7295 100644 --- a/src/client.nim +++ b/src/client.nim @@ -129,24 +129,25 @@ registerEventListener(EventType.evtMessageCreate, proc(bEvt: BaseEvent) = discard channel.sendMessage("PONG") elif (event.message.content.startsWith("?modifyChannelTopic")): let modifyTopic = event.message.content.substr(20) + var channel: Channel = event.message.getMessageChannel(event.client.cache) if (channel != nil): discard channel.sendMessage("Modifing Channel!") discard channel.modifyChannel(ChannelModify(topic: some(modifyTopic))) elif (event.message.content.startsWith("?deleteChannel")): let channelID = getIDFromJson(event.message.content.substr(15)) - var channel: Channel = event.client.cache.getChannel(channelID) + if (channel != nil): discard channel.sendMessage("Deleting Channel!") discard channel.deleteChannel() discard channel.sendMessage("Deleted Channel!") + elif (event.message.content.startsWith("?getMessages")): + var channel: Channel = event.message.getMessageChannel(event.client.cache) + if (channel != nil): + discard channel.getMessages(MessagesGetRequest(limit: some(15), before: some(event.message.id))) + else: + echo "Failed to get channel!" ) -#[ registerEventListener(EventType.evtGuildCreate, proc(bEvt: BaseEvent) = - let event = GuildCreateEvent(bEvt) - - echo "Guild has ", event.guild.members.len, " members and ", event.guild.channels.len, " channels..." -) ]# - waitFor bot.startConnection() \ No newline at end of file