add websocket connection

Co-authored-by: GabrieleYT9768 <GabrieleYT9768@users.noreply.github.com>
This commit is contained in:
Killer Boss Original 2023-08-01 21:20:43 +02:00
parent a63ca6e2b0
commit b7c12bae1f
6 changed files with 93 additions and 25 deletions

51
lib/src/main.dart Normal file
View file

@ -0,0 +1,51 @@
import "dart:io";
import "package:events_emitter/events_emitter.dart";
import "types.dart";
import "dart:convert";
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<GatewayIntentBits> intents = [];
bool logged = false;
dynamic ws;
Client({List<GatewayIntentBits> intents = const []});
login(String token) async {
this.token = token;
logged = true;
ws = await WebSocket.connect(websocket);
var interval = 0;
var payload = {
"op": 2,
'd': {
"token": token,
"intents": 32767,
"properties": {
"\$os": "linux",
"\$browser": "chrome",
"\$device": "chrome",
},
}
};
print(json.encode(payload));
ws.add(json.encode(payload));
ws.listen((event) {
event = json.decode(event);
var eventName = event["t"];
switch (eventName) {
case "READY":
emit("READY", {"user": event["d"]["user"]});
}
});
}
}

View file

@ -1,17 +1,19 @@
import 'package:tn_discord/src/index.dart';
import 'main.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
final headers = {'Content-Type': 'application/json'};
final headerswh = {
"Content-Type": "application/json"
};
Future<http.Response> sendWH(Map content, String token, String id) {
final url = Uri.parse("$apiURL/webhooks/$id/$token");
return http.post(url, body: json.encode(content), headers: headers);
return http.post(url, body: json.encode(content), headers: headerswh);
}
Future<http.Response> 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: headers);
return http.patch(url, body: json.encode(content), headers: headerswh);
}
Future<http.Response> getWH(String token, String id, String mid) {
@ -23,3 +25,15 @@ Future<http.Response> 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<String, String> headers = {};
Sender(token) {
headers = {
"Content-Type": "application/json",
"Authorization": "Bot $token"
};
}
}

View file

@ -1,4 +1,4 @@
import 'package:tn_discord/src/types.dart';
import 'types.dart';
class Utils {
static Map<String, String> parseWebhookURL(String url) {

View file

@ -1,19 +1,11 @@
import "dart:convert";
import "package:events_emitter/events_emitter.dart";
import "package:http/http.dart";
import "package:tn_discord/src/error_handler.dart";
import "package:tn_discord/src/message.dart";
import "error_handler.dart";
import "message.dart";
import "types.dart";
import "util.dart";
final version = "10";
final apiURL = "https://discord.com/api/v$version";
class Client extends EventEmitter {
Client({List<GatewayIntentBits> intents = const []});
}
class WebhookClient {
String token = "";
String id = "";
@ -37,8 +29,10 @@ class WebhookClient {
return json.decode(res.body);
}
Future<Map<String, dynamic>> send({String? content, List<Embed>? embeds}) async {
Map<String, dynamic> body = Utils().createMessage(text: content, embeds: embeds);
Future<Map<String, dynamic>> send(
{String? content, List<Embed>? embeds}) async {
Map<String, dynamic> body =
Utils().createMessage(text: content, embeds: embeds);
Response res = await sendWH(body, token, id);
@ -54,7 +48,8 @@ class WebhookClient {
return json.decode(res.body);
}
Future<Map<String, dynamic>> edit(String id, {String content = "", List<Embed> embeds = const []}) async {
Future<Map<String, dynamic>> edit(String id,
{String content = "", List<Embed> embeds = const []}) async {
Map body = {"content": content, "embeds": embeds};
Response res = await editWH(body, token, this.id, id);

View file

@ -1,9 +1,5 @@
/// Support for doing something awesome.
///
/// More dartdocs go here.
library;
export "src/index.dart";
export "src/webhook.dart";
export "src/main.dart";
export "src/types.dart";
// TODO: Export any libraries intended for clients of this package.

12
test.dart Normal file
View file

@ -0,0 +1,12 @@
import 'package:tn_discord/tn_discord.dart';
main() {
var client = Client();
client.login(
"OTU2NTczNDc3NzEwNzUzODMy.GmvBek.GEaV7uk-XPcpZ7Xbiohgp_0mm_5NVs5SeAAh7M");
client.on("READY", (data) {
print("Hi $data");
});
}