Add Channel.getMessages method
This commit is contained in:
parent
f312b914ff
commit
0e1cb7280a
|
@ -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)
|
||||
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))
|
|
@ -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()
|
Reference in New Issue