Fix code blocks and add documentation for some client procs.

This commit is contained in:
SeanOMik 2020-06-18 21:20:09 -05:00
parent 6d7897a100
commit 9e4da6a718
No known key found for this signature in database
GPG Key ID: FA4D55AC05268A88
2 changed files with 23 additions and 9 deletions

View File

@ -144,6 +144,7 @@ proc modifyChannel*(channel: Channel, modify: ChannelModify): Future[Channel] {.
## Modifies the channel.
##
## Examples:
##
## .. code-block:: nim
## var chan = getChannel(703084913510973472)
## chan = chan.modifyChannel(ChannelModify(topic: some("This is the channel topic")))
@ -204,6 +205,7 @@ proc getMessages*(channel: Channel, request: MessagesGetRequest): seq[Message] =
## Gets messages from the channel.
##
## Examples:
##
## .. code-block:: nim
## var chan = getChannel(703084913510973472)
## channel.getMessages(MessagesGetRequest(limit: some(15), before: some(723030179760570428)))
@ -302,6 +304,7 @@ proc createChannelInvite*(channel: Channel, fields: CreateInviteFields): Invite
## Requires the CREATE_INSTANT_INVITE permission.
##
## Examples:
##
## .. code-block:: nim
## var chan = getChannel(703084913510973472)
## # Create an invite that lasts 1 day, and can only be used 10 times

View File

@ -21,13 +21,9 @@ type
opHello = 10,
opHeartbeatAck = 11
proc defaultHeaders*(client: DiscordClient, added: HttpHeaders = newHttpHeaders()): HttpHeaders =
added.add("Authorization", fmt("Bot {client.token}"))
added.add("User-Agent", "NimCord (https://github.com/SeanOMik/nimcord, v0.0.0)")
added.add("X-RateLimit-Precision", "millisecond")
return added;
proc sendGatewayRequest*(client: DiscordClient, request: JsonNode, msg: string = "") {.async.} =
## Send a gateway request.
## Don't use this unless you know what you're doing!
if (msg.len == 0):
echo "Sending gateway payload: ", request
else:
@ -79,7 +75,21 @@ proc handleWebsocketPacket(client: DiscordClient) {.async.} =
discard
proc startConnection*(client: DiscordClient) {.async.} =
let urlResult = sendRequest(endpoint("/gateway/bot"), HttpMethod.HttpGet, client.defaultHeaders())
## Start a bot connection.
##
## Examples:
##
## .. code-block:: nim
## var tokenStream = newFileStream("token.txt", fmRead)
## var tkn: string
## if (not isNil(tokenStream)):
## discard tokenStream.readLine(tkn)
## echo "Read token from the file: ", tkn
##
## tokenStream.close()
##
## var bot = newDiscordClient(tkn)
let urlResult = sendRequest(endpoint("/gateway/bot"), HttpMethod.HttpGet, defaultHeaders())
if (urlResult.contains("url")):
let url = urlResult["url"].getStr()
@ -93,6 +103,7 @@ proc startConnection*(client: DiscordClient) {.async.} =
raise newException(IOError, "Failed to get gateway url, token may of been incorrect!")
proc newDiscordClient(tkn: string): DiscordClient =
## Create a DiscordClient using a token.
globalToken = tkn
var cac: Cache