diff --git a/lib/src/main.dart b/lib/src/main.dart new file mode 100644 index 0000000..18144d1 --- /dev/null +++ b/lib/src/main.dart @@ -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 intents = []; + bool logged = false; + dynamic ws; + + Client({List 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"]}); + } + }); + } +} diff --git a/lib/src/message.dart b/lib/src/message.dart index 0ca2cb4..7fd0243 100644 --- a/lib/src/message.dart +++ b/lib/src/message.dart @@ -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 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 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 getWH(String token, String id, String mid) { @@ -23,3 +25,15 @@ 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" + }; + } +} \ No newline at end of file diff --git a/lib/src/util.dart b/lib/src/util.dart index fd2b78a..23f3ad4 100644 --- a/lib/src/util.dart +++ b/lib/src/util.dart @@ -1,4 +1,4 @@ -import 'package:tn_discord/src/types.dart'; +import 'types.dart'; class Utils { static Map parseWebhookURL(String url) { diff --git a/lib/src/index.dart b/lib/src/webhook.dart similarity index 72% rename from lib/src/index.dart rename to lib/src/webhook.dart index 5b67b57..61cb13f 100644 --- a/lib/src/index.dart +++ b/lib/src/webhook.dart @@ -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 intents = const []}); -} - class WebhookClient { String token = ""; String id = ""; @@ -37,8 +29,10 @@ class WebhookClient { return json.decode(res.body); } - Future> send({String? content, List? embeds}) async { - Map body = Utils().createMessage(text: content, embeds: embeds); + Future> send( + {String? content, List? embeds}) async { + Map 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> edit(String id, {String content = "", List embeds = const []}) async { + Future> edit(String id, + {String content = "", List embeds = const []}) async { Map body = {"content": content, "embeds": embeds}; Response res = await editWH(body, token, this.id, id); diff --git a/lib/tn_discord.dart b/lib/tn_discord.dart index cb3f894..da3922d 100644 --- a/lib/tn_discord.dart +++ b/lib/tn_discord.dart @@ -1,9 +1,5 @@ -/// Support for doing something awesome. -/// -/// More dartdocs go here. library; -export "src/index.dart"; -export "src/types.dart"; - -// TODO: Export any libraries intended for clients of this package. +export "src/webhook.dart"; +export "src/main.dart"; +export "src/types.dart"; \ No newline at end of file diff --git a/test.dart b/test.dart new file mode 100644 index 0000000..14cd397 --- /dev/null +++ b/test.dart @@ -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"); + }); +}