Add reconection

This commit is contained in:
Killer Boss Original 2023-08-02 12:48:55 +02:00
parent 408acaef88
commit 1214feb42a
4 changed files with 120 additions and 51 deletions

2
.gitignore vendored
View file

@ -7,4 +7,4 @@
pubspec.lock pubspec.lock
# Testing # Testing
bin/ test.dart

View file

@ -1,50 +1,112 @@
import "dart:async";
import "dart:io"; import "dart:io";
import "package:events_emitter/events_emitter.dart"; import "package:tn_discord/src/requests.dart";
import "types.dart"; import "types.dart";
import "dart:convert"; import "dart:convert";
import "package:events_emitter/events_emitter.dart";
final version = "10"; final version = "10";
final apiURL = "https://discord.com/api/v$version"; final apiURL = "https://discord.com/api/v$version";
final websocket = "wss://gateway.discord.gg/?v=6&encoding=json";
class Client extends EventEmitter { class Client extends EventEmitter {
String? token; String? token;
List<GatewayIntentBits> intents = []; List<GatewayIntentBits> intents = [];
bool logged = false; bool logged = false;
dynamic ws; dynamic ws;
String resume_gateway_url = "";
String session_id = "";
Client({List<GatewayIntentBits> intents = const []}); Client({List<GatewayIntentBits> intents = const []});
login(String token) async { login(String token) async {
final websocket = await requestWebSocketURL();
this.token = token; this.token = token;
logged = true; logged = true;
ws = await WebSocket.connect(websocket); ws = await WebSocket.connect(websocket);
var interval = 0;
var payload = { dynamic payload = {
"op": 2, "op": 2,
'd': { 'd': {
"token": token, "token": token,
"intents": 32767, "intents": 32767,
"properties": { "properties": {
"\$os": "linux", "os": "linux",
"\$browser": "chrome", "browser": "chrome",
"\$device": "chrome", "device": "chrome",
}, },
} }
}; };
print(json.encode(payload));
ws.add(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) { ws.listen((event) {
event = json.decode(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"]; var eventName = event["t"];
switch (eventName) { switch (eventName) {
case "READY": case "READY":
emit("READY", {"user": event["d"]["user"]}); emit("READY", event);
} }
}); });
} }

View file

@ -1,39 +1,46 @@
import 'main.dart'; import 'main.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'dart:convert'; import 'dart:convert';
final headerswh = { final headerswh = {
"Content-Type": "application/json" "Content-Type": "application/json"
}; };
Future<http.Response> sendWH(Map content, String token, String id) { Future<http.Response> sendWH(Map content, String token, String id) {
final url = Uri.parse("$apiURL/webhooks/$id/$token"); final url = Uri.parse("$apiURL/webhooks/$id/$token");
return http.post(url, body: json.encode(content), headers: headerswh); return http.post(url, body: json.encode(content), headers: headerswh);
} }
Future<http.Response> editWH(Map content, String token, String id, String mid) { Future<http.Response> editWH(Map content, String token, String id, String mid) {
final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid");
return http.patch(url, body: json.encode(content), headers: headerswh); return http.patch(url, body: json.encode(content), headers: headerswh);
} }
Future<http.Response> getWH(String token, String id, String mid) { Future<http.Response> getWH(String token, String id, String mid) {
final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid");
return http.get(url); return http.get(url);
} }
Future<http.Response> deleteWH(String token, String id, String mid) { Future<http.Response> deleteWH(String token, String id, String mid) {
final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid");
return http.delete(url); return http.delete(url);
} }
class Sender { Future<String> requestWebSocketURL() async {
String? token; final url = Uri.parse("$apiURL/gateway");
Map<String, String> headers = {}; dynamic res = await http.get(url);
res = json.decode(res.body);
Sender(token) { return res["url"];
headers = { }
"Content-Type": "application/json",
"Authorization": "Bot $token" class Sender {
}; String? token;
} Map<String, String> headers = {};
Sender(token) {
headers = {
"Content-Type": "application/json",
"Authorization": "Bot $token"
};
}
} }

View file

@ -2,7 +2,7 @@ import "dart:convert";
import "package:http/http.dart"; import "package:http/http.dart";
import "error_handler.dart"; import "error_handler.dart";
import "message.dart"; import 'requests.dart';
import "types.dart"; import "types.dart";
import "util.dart"; import "util.dart";