2020-06-18 03:18:54 +00:00
|
|
|
import websocket, asyncnet, asyncdispatch, json, httpClient, eventdispatcher, strformat
|
|
|
|
import eventhandler, streams, nimcordutils, discordobject, user, cache, clientobjects
|
|
|
|
import strutils, channel, options
|
|
|
|
|
|
|
|
const
|
|
|
|
nimcordMajor = 0
|
|
|
|
nimcordMinor = 0
|
|
|
|
nimcordMicro = 0
|
2020-05-28 05:50:53 +00:00
|
|
|
|
|
|
|
type
|
2020-05-29 06:20:39 +00:00
|
|
|
DiscordOpCode = enum
|
|
|
|
opDispatch = 0,
|
|
|
|
opHeartbeat = 1,
|
|
|
|
opIdentify = 2,
|
|
|
|
opPresenceUpdate = 3,
|
|
|
|
opVoiceStateUpdate = 4,
|
|
|
|
opResume = 6,
|
|
|
|
opReconnect = 7,
|
|
|
|
opRequestGuildMembers = 8,
|
|
|
|
opInvalidSession = 9,
|
|
|
|
opHello = 10,
|
|
|
|
opHeartbeatAck = 11
|
|
|
|
|
2020-06-17 05:34:23 +00:00
|
|
|
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;
|
|
|
|
|
2020-05-29 06:20:39 +00:00
|
|
|
proc sendGatewayRequest*(client: DiscordClient, request: JsonNode, msg: string = "") {.async.} =
|
|
|
|
if (msg.len == 0):
|
|
|
|
echo "Sending gateway payload: ", request
|
|
|
|
else:
|
|
|
|
echo msg
|
|
|
|
|
|
|
|
discard client.ws.sendText($request)
|
2020-05-28 05:50:53 +00:00
|
|
|
|
2020-05-29 06:20:39 +00:00
|
|
|
proc handleHeartbeat(client: DiscordClient) {.async.} =
|
2020-05-28 05:50:53 +00:00
|
|
|
while true:
|
2020-05-29 06:20:39 +00:00
|
|
|
var heartbeatPayload: JsonNode
|
|
|
|
if (client.lastSequence == 0):
|
|
|
|
heartbeatPayload = %* { "d": nil, "op": ord(DiscordOpCode.opHeartbeat) }
|
|
|
|
else:
|
|
|
|
heartbeatPayload = %* { "d": client.lastSequence, "op": ord(DiscordOpCode.opHeartbeat) }
|
2020-05-28 05:50:53 +00:00
|
|
|
|
2020-05-29 06:20:39 +00:00
|
|
|
await client.sendGatewayRequest(heartbeatPayload, fmt("Sending heartbeat payload: {$heartbeatPayload}"))
|
|
|
|
client.heartbeatAcked = true
|
|
|
|
|
|
|
|
echo "Waiting ", client.heartbeatInterval, " ms until next heartbeat..."
|
|
|
|
await sleepAsync(client.heartbeatInterval)
|
|
|
|
|
|
|
|
proc getIdentifyPacket(client: DiscordClient): JsonNode =
|
|
|
|
return %* { "op": ord(DiscordOpCode.opIdentify), "d": { "token": client.token, "properties": { "$os": system.hostOS, "$browser": "NimCord", "$device": "NimCord" } } }
|
|
|
|
|
|
|
|
proc handleWebsocketPacket(client: DiscordClient) {.async.} =
|
2020-05-28 05:50:53 +00:00
|
|
|
while true:
|
|
|
|
var packet: tuple[opcode: Opcode, data: string]
|
|
|
|
|
|
|
|
packet = await client.ws.readData();
|
2020-05-29 06:20:39 +00:00
|
|
|
echo "Received gateway payload: ", packet.data
|
2020-05-28 05:50:53 +00:00
|
|
|
|
|
|
|
var json: JsonNode = parseJson(packet.data);
|
|
|
|
|
2020-05-29 06:20:39 +00:00
|
|
|
if (json.contains("s")):
|
|
|
|
client.lastSequence = json["s"].getInt()
|
|
|
|
|
|
|
|
case json["op"].getInt()
|
|
|
|
of ord(DiscordOpCode.opHello):
|
|
|
|
client.heartbeatInterval = json["d"]["heartbeat_interval"].getInt()
|
|
|
|
discard client.sendGatewayRequest(client.getIdentifyPacket())
|
|
|
|
|
|
|
|
asyncCheck client.handleHeartbeat()
|
|
|
|
client.heartbeatAcked = true
|
|
|
|
of ord(DiscordOpCode.opHeartbeatAck):
|
|
|
|
client.heartbeatAcked = true
|
2020-05-30 04:59:23 +00:00
|
|
|
of ord(DiscordOpCode.opDispatch):
|
2020-06-18 03:18:54 +00:00
|
|
|
discard handleDiscordEvent(client, json["d"], json["t"].getStr())
|
2020-05-28 05:50:53 +00:00
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
2020-05-29 06:20:39 +00:00
|
|
|
proc startConnection*(client: DiscordClient) {.async.} =
|
2020-06-17 05:34:23 +00:00
|
|
|
let urlResult = sendRequest(endpoint("/gateway/bot"), HttpMethod.HttpGet, client.defaultHeaders())
|
2020-05-29 06:20:39 +00:00
|
|
|
if (urlResult.contains("url")):
|
|
|
|
let url = urlResult["url"].getStr()
|
2020-05-28 05:50:53 +00:00
|
|
|
|
2020-06-17 05:34:23 +00:00
|
|
|
client.ws = await newAsyncWebsocketClient(url[6..url.high], Port 443,
|
2020-05-28 05:50:53 +00:00
|
|
|
path = "/v=6&encoding=json", true)
|
|
|
|
echo "Connected!"
|
|
|
|
|
2020-05-29 06:20:39 +00:00
|
|
|
asyncCheck client.handleWebsocketPacket()
|
2020-05-28 05:50:53 +00:00
|
|
|
runForever()
|
|
|
|
else:
|
2020-06-18 04:50:17 +00:00
|
|
|
raise newException(IOError, "Failed to get gateway url, token may of been incorrect!")
|
2020-05-28 05:50:53 +00:00
|
|
|
|
2020-06-18 03:18:54 +00:00
|
|
|
proc newDiscordClient(tkn: string): DiscordClient =
|
|
|
|
globalToken = tkn
|
|
|
|
|
|
|
|
var cac: Cache
|
|
|
|
new(cac)
|
|
|
|
|
|
|
|
result = DiscordClient(token: tkn, cache: cac)
|
|
|
|
|
2020-05-31 06:14:56 +00:00
|
|
|
var tokenStream = newFileStream("token.txt", fmRead)
|
2020-05-31 06:13:57 +00:00
|
|
|
var tkn: string
|
|
|
|
if (not isNil(tokenStream)):
|
|
|
|
discard tokenStream.readLine(tkn)
|
2020-05-31 06:14:56 +00:00
|
|
|
echo "Read token from the file: ", tkn
|
|
|
|
|
2020-05-31 06:13:57 +00:00
|
|
|
tokenStream.close()
|
|
|
|
|
2020-06-18 03:18:54 +00:00
|
|
|
var bot = newDiscordClient(tkn)
|
2020-05-31 06:13:57 +00:00
|
|
|
|
|
|
|
registerEventListener(EventType.evtReady, proc(bEvt: BaseEvent) =
|
|
|
|
let event = ReadyEvent(bEvt)
|
2020-06-18 03:18:54 +00:00
|
|
|
bot.clientUser = event.clientUser
|
2020-05-30 04:59:23 +00:00
|
|
|
|
2020-06-18 03:18:54 +00:00
|
|
|
echo "Ready! (v", nimcordMajor, ".", nimcordMinor, ".", nimcordMicro, ")"
|
|
|
|
echo "Logged in as: ", bot.clientUser.username, "#", bot.clientUser.discriminator
|
|
|
|
echo "ID: ", bot.clientUser.id
|
|
|
|
echo "--------------------"
|
2020-05-31 06:13:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
registerEventListener(EventType.evtMessageCreate, proc(bEvt: BaseEvent) =
|
|
|
|
let event = MessageCreateEvent(bEvt)
|
2020-06-18 03:18:54 +00:00
|
|
|
|
|
|
|
if (event.message.content == "?ping"):
|
|
|
|
var channel: Channel = event.message.getMessageChannel(event.client.cache)
|
|
|
|
if (channel != nil):
|
|
|
|
discard channel.sendMessage("PONG")
|
|
|
|
elif (event.message.content.startsWith("?modifyChannelTopic")):
|
|
|
|
let modifyTopic = event.message.content.substr(20)
|
2020-06-18 04:51:38 +00:00
|
|
|
|
2020-06-18 03:18:54 +00:00
|
|
|
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)
|
2020-06-18 04:51:38 +00:00
|
|
|
|
2020-06-18 03:18:54 +00:00
|
|
|
if (channel != nil):
|
|
|
|
discard channel.sendMessage("Deleting Channel!")
|
|
|
|
discard channel.deleteChannel()
|
|
|
|
discard channel.sendMessage("Deleted Channel!")
|
2020-06-18 04:51:38 +00:00
|
|
|
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!"
|
2020-05-30 04:59:23 +00:00
|
|
|
)
|
|
|
|
|
2020-05-28 05:50:53 +00:00
|
|
|
waitFor bot.startConnection()
|