diff --git a/.gitignore b/.gitignore index eb78b6c..292edb8 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ pubspec.lock # Testing -bin/ \ No newline at end of file +test.dart \ No newline at end of file diff --git a/lib/src/main.dart b/lib/src/main.dart index 18144d1..5b5a341 100644 --- a/lib/src/main.dart +++ b/lib/src/main.dart @@ -1,50 +1,112 @@ +import "dart:async"; import "dart:io"; -import "package:events_emitter/events_emitter.dart"; +import "package:tn_discord/src/requests.dart"; + import "types.dart"; import "dart:convert"; +import "package:events_emitter/events_emitter.dart"; final version = "10"; final apiURL = "https://discord.com/api/v$version"; -final websocket = "wss://gateway.discord.gg/?v=6&encoding=json"; class Client extends EventEmitter { String? token; List intents = []; bool logged = false; dynamic ws; + String resume_gateway_url = ""; + String session_id = ""; Client({List intents = const []}); login(String token) async { + final websocket = await requestWebSocketURL(); + this.token = token; logged = true; ws = await WebSocket.connect(websocket); - var interval = 0; - var payload = { + + dynamic payload = { "op": 2, 'd': { "token": token, "intents": 32767, "properties": { - "\$os": "linux", - "\$browser": "chrome", - "\$device": "chrome", + "os": "linux", + "browser": "chrome", + "device": "chrome", }, } }; - - print(json.encode(payload)); - ws.add(json.encode(payload)); + reconnect() { + payload = { + "op": 6, + "d": {"token": token, "session_id": session_id, "seq": 1337} + }; + + ws = WebSocket.connect(resume_gateway_url); + ws.add(json.encode(payload)); + } + + sendHeartBeat() { + payload = {"op": 1, "d": null}; + ws.add(json.encode(payload)); + print("Sent Heartbeat"); + } + + heartbeatsender(int ms) { + double jitter = 0.8; // number from 0 to 1 + Timer.periodic(Duration(milliseconds: (ms * jitter).round()), (timer) { + sendHeartBeat(); + }); + } + ws.listen((event) { event = json.decode(event); + + print(event); + + switch (event["op"]) { + case 1: + sendHeartBeat(); + break; + case 10: + heartbeatsender(event["d"]["heartbeat_interval"]); + break; + case 7: + case 9: + case 4000: + case 4001: + case 4002: + case 4003: + case 4005: + case 4007: + case 4008: + case 4009: + reconnect(); + break; + case 4004: + throw Exception("[4014] Disallowed Intents"); + case 4010: + throw Exception("[4010] Invalid Shard"); + case 4011: + throw Exception("[4011] Sharding Required"); + case 4012: + throw Exception("[4012] Invalid API Version"); + case 4013: + throw Exception("[4013] Invalid Intents"); + case 4014: + throw Exception("[4014] Disallowed Intents"); + } + var eventName = event["t"]; switch (eventName) { case "READY": - emit("READY", {"user": event["d"]["user"]}); + emit("READY", event); } }); } diff --git a/lib/src/message.dart b/lib/src/requests.dart similarity index 85% rename from lib/src/message.dart rename to lib/src/requests.dart index 7fd0243..5990942 100644 --- a/lib/src/message.dart +++ b/lib/src/requests.dart @@ -1,39 +1,46 @@ -import 'main.dart'; -import 'package:http/http.dart' as http; -import 'dart:convert'; - -final headerswh = { - "Content-Type": "application/json" -}; - -Future sendWH(Map content, String token, String id) { - final url = Uri.parse("$apiURL/webhooks/$id/$token"); - return http.post(url, body: json.encode(content), headers: headerswh); -} - -Future editWH(Map content, String token, String id, String mid) { - final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); - return http.patch(url, body: json.encode(content), headers: headerswh); -} - -Future getWH(String token, String id, String mid) { - final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); - return http.get(url); -} - -Future deleteWH(String token, String id, String mid) { - final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); - return http.delete(url); -} - -class Sender { - String? token; - Map headers = {}; - - Sender(token) { - headers = { - "Content-Type": "application/json", - "Authorization": "Bot $token" - }; - } +import 'main.dart'; +import 'package:http/http.dart' as http; +import 'dart:convert'; + +final headerswh = { + "Content-Type": "application/json" +}; + +Future sendWH(Map content, String token, String id) { + final url = Uri.parse("$apiURL/webhooks/$id/$token"); + return http.post(url, body: json.encode(content), headers: headerswh); +} + +Future editWH(Map content, String token, String id, String mid) { + final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); + return http.patch(url, body: json.encode(content), headers: headerswh); +} + +Future getWH(String token, String id, String mid) { + final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); + return http.get(url); +} + +Future deleteWH(String token, String id, String mid) { + final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); + return http.delete(url); +} + +Future requestWebSocketURL() async { + final url = Uri.parse("$apiURL/gateway"); + dynamic res = await http.get(url); + res = json.decode(res.body); + return res["url"]; +} + +class Sender { + String? token; + Map headers = {}; + + Sender(token) { + headers = { + "Content-Type": "application/json", + "Authorization": "Bot $token" + }; + } } \ No newline at end of file diff --git a/lib/src/webhook.dart b/lib/src/webhook.dart index 61cb13f..d3422f9 100644 --- a/lib/src/webhook.dart +++ b/lib/src/webhook.dart @@ -2,7 +2,7 @@ import "dart:convert"; import "package:http/http.dart"; import "error_handler.dart"; -import "message.dart"; +import 'requests.dart'; import "types.dart"; import "util.dart";