From 64ebee326b5a659e4de90fd9bfe0aeaa299d4b88 Mon Sep 17 00:00:00 2001 From: Killer Boss Original <73131550+killerbossoriginal@users.noreply.github.com> Date: Wed, 2 Aug 2023 17:44:35 +0200 Subject: [PATCH] Adding *managers Co-authored-by: GabrieleYT9768 --- lib/src/errors.dart | 0 lib/src/main.dart | 71 +++++++++++------- lib/src/requests.dart | 8 ++ lib/src/types.dart | 169 +++++++++++++++++++++++++++++++++++++++++- lib/src/webhook.dart | 6 +- 5 files changed, 223 insertions(+), 31 deletions(-) create mode 100644 lib/src/errors.dart diff --git a/lib/src/errors.dart b/lib/src/errors.dart new file mode 100644 index 0000000..e69de29 diff --git a/lib/src/main.dart b/lib/src/main.dart index 5b5a341..079ca22 100644 --- a/lib/src/main.dart +++ b/lib/src/main.dart @@ -1,29 +1,38 @@ import "dart:async"; import "dart:io"; -import "package:tn_discord/src/requests.dart"; +import "package:tn_discord/src/types.dart"; + +import "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"; +int calculateIntents(List intents) { + int intentsNumber = 0; + + for (var element in intents) { + intentsNumber += element; + } + + return intentsNumber; +} + class Client extends EventEmitter { String? token; - List intents = []; - bool logged = false; + int intents; dynamic ws; - String resume_gateway_url = ""; - String session_id = ""; + String resumeGatewayURL = ""; + String sessionID = ""; - Client({List intents = const []}); + Client({this.intents = 0}); login(String token) async { final websocket = await requestWebSocketURL(); this.token = token; - logged = true; ws = await WebSocket.connect(websocket); @@ -31,30 +40,30 @@ class Client extends EventEmitter { "op": 2, 'd': { "token": token, - "intents": 32767, + "intents": intents, "properties": { "os": "linux", - "browser": "chrome", - "device": "chrome", + "browser": "tn_discord", + "device": "tn_discord", }, } }; + ws.add(json.encode(payload)); - reconnect() { + reconnect() async { payload = { "op": 6, - "d": {"token": token, "session_id": session_id, "seq": 1337} + "d": {"token": token, "session_id": sessionID, "seq": 1337} }; - ws = WebSocket.connect(resume_gateway_url); + ws = await WebSocket.connect(resumeGatewayURL); ws.add(json.encode(payload)); } sendHeartBeat() { payload = {"op": 1, "d": null}; ws.add(json.encode(payload)); - print("Sent Heartbeat"); } heartbeatsender(int ms) { @@ -64,11 +73,13 @@ class Client extends EventEmitter { }); } + Sender sender = Sender(token); + var i = await sender.getServer(); + GuildManager(i); + ws.listen((event) { event = json.decode(event); - print(event); - switch (event["op"]) { case 1: sendHeartBeat(); @@ -78,6 +89,23 @@ class Client extends EventEmitter { break; case 7: case 9: + reconnect(); + break; + } + + var eventName = event["t"]; + + switch (eventName) { + case "READY": + resumeGatewayURL = event["d"]["resume_gateway_url"]; + sessionID = event["d"]["session_id"]; + emit("READY", event["d"]); + break; + case "GUILD_CREATE": + break; + } + }, onDone: () { + switch (ws.closeCode) { case 4000: case 4001: case 4002: @@ -89,7 +117,7 @@ class Client extends EventEmitter { reconnect(); break; case 4004: - throw Exception("[4014] Disallowed Intents"); + throw Exception("[4004] Disallowed Intents"); case 4010: throw Exception("[4010] Invalid Shard"); case 4011: @@ -101,13 +129,6 @@ class Client extends EventEmitter { case 4014: throw Exception("[4014] Disallowed Intents"); } - - var eventName = event["t"]; - - switch (eventName) { - case "READY": - emit("READY", event); - } }); } } diff --git a/lib/src/requests.dart b/lib/src/requests.dart index 5990942..f5f7ae2 100644 --- a/lib/src/requests.dart +++ b/lib/src/requests.dart @@ -43,4 +43,12 @@ class Sender { "Authorization": "Bot $token" }; } + + Future getServer() async { + final url = Uri.parse("$apiURL/users/@me/guilds"); + dynamic res = await http.get(url, headers: headers); + res = json.decode(res.body); + return res; + + } } \ No newline at end of file diff --git a/lib/src/types.dart b/lib/src/types.dart index c3be6da..1c8a370 100644 --- a/lib/src/types.dart +++ b/lib/src/types.dart @@ -1,4 +1,10 @@ -// ignore_for_file: non_constant_identifier_names, constant_identifier_names +// ignore_for_file: constant_identifier_names + +import "dart:convert"; + +import "package:http/http.dart" as http; + +import "main.dart"; class GatewayIntentBits { static const Guilds = 1; @@ -22,5 +28,164 @@ class GatewayIntentBits { static const AutoModerationExecution = 2097152; } +class Collection { + final Map _variables = {}; + Collection(); + + dynamic set(String key, dynamic value) { + _variables[key] = value; + return value; + } + + dynamic get(String key) { + return _variables[key]; + } + + void remove(String key) { + _variables.remove(key); + } + + dynamic add(String key, num value) { + _variables[key] += value; + return value + _variables[key]; + } + + dynamic subtract(String key, num value) { + _variables[key] -= value; + return value - _variables[key]; + } +} + +class Guild { + String id = ''; + String name = ''; + String owner = ''; + String description = ''; + + Guild(Map data) { + id = data["id"]; + name = data["name"]; + description = data["description"]; + owner = data["owner_id"]; + } +} + +class GuildManager { + final Collection cache = Collection(); + + GuildManager(List guilds) { + for (var guild in guilds) { + cache.set(guild.id, guild); + } + } + + Future fetch(String id) async { + var res = await http.get(Uri.parse("$apiURL/guilds/$id")); + if (res.statusCode != 200) { + throw Exception("Error ${res.statusCode} receiving the guild"); + } + final guild = Guild(json.decode(res.body)); + cache.set(guild.id, guild); + return guild; + } +} + +class Channel { + String id = ''; + String name = ''; + + Channel(Map data) { + id = data["id"]; + name = data["name"]; + } +} + +class ChannelManager { + final Collection cache = Collection(); + + ChannelManager(List channels) { + for (var channel in channels) { + cache.set(channel.id, channel); + } + } + + Future fetch(String id) async { + var res = await http.get(Uri.parse("$apiURL/channels/$id")); + if (res.statusCode != 200) { + throw Exception("Error ${res.statusCode} receiving the channel"); + } + dynamic channel = Channel(json.decode(res.body)); + cache.set(channel.id, channel); + return channel; + } +} + +class Role { + // +} + +class RoleManager { + // +} + +class Member { + User? user; + RoleManager? roles; + String joinedAt = ''; + int flags = 0; + + + Member(Map data) { + user = User(data["user"]); + joinedAt = data["joined_at"]; + int flags = 0; + } +} + + +class MemberManager { + // +} + +class User { + String id = ''; + bool bot = false; + String username = ''; + String globalName = ''; + String displayName = ''; + int discriminator = 0; + String avatar = ''; + + User(Map data) { + id = data["id"]; + bot = data["bot"]; + username = data["username"]; + globalName = data["global_name"]; + displayName = data["display_name"]; + discriminator = data["discriminator"]; + avatar = "https://cdn.discordapp.com/avatars/$id/${data["avatar"]}.webp"; + } +} + +class UserManager { + final Collection cache = Collection(); + + UserManager(List users) { + for (var user in users) { + cache.set(user.id, user); + } + } + + Future fetch(String id) async { + var res = await http.get(Uri.parse("$apiURL/users/$id")); + if (res.statusCode != 200) { + throw Exception("Error ${res.statusCode} receiving the user"); + } + dynamic user = User(json.decode(res.body)); + cache.set(user.id, user); + return user; + } +} + typedef Embed = Map; -typedef Message = Map; \ No newline at end of file +typedef Message = Map; diff --git a/lib/src/webhook.dart b/lib/src/webhook.dart index d3422f9..d93eeb4 100644 --- a/lib/src/webhook.dart +++ b/lib/src/webhook.dart @@ -29,8 +29,7 @@ class WebhookClient { return json.decode(res.body); } - Future> send( - {String? content, List? embeds}) async { + Future> send({String? content, List? embeds}) async { Map body = Utils().createMessage(text: content, embeds: embeds); @@ -48,8 +47,7 @@ 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);