From 4f2da1691039eddf82802ce482af21df96e645e1 Mon Sep 17 00:00:00 2001 From: Killer Boss Original <73131550+killerbossoriginal@users.noreply.github.com> Date: Thu, 3 Aug 2023 18:11:30 +0200 Subject: [PATCH] Add managers --- lib/src/main.dart | 22 ++++++-- lib/src/requests.dart | 3 +- lib/src/types.dart | 119 ++++++++++++++++++++++++++++++++---------- 3 files changed, 112 insertions(+), 32 deletions(-) diff --git a/lib/src/main.dart b/lib/src/main.dart index 079ca22..feb4fee 100644 --- a/lib/src/main.dart +++ b/lib/src/main.dart @@ -26,6 +26,8 @@ class Client extends EventEmitter { dynamic ws; String resumeGatewayURL = ""; String sessionID = ""; + dynamic guilds; + dynamic ready; Client({this.intents = 0}); @@ -74,8 +76,17 @@ class Client extends EventEmitter { } Sender sender = Sender(token); - var i = await sender.getServer(); - GuildManager(i); + var i = await sender.getServers(); + + List gg = []; + + for (dynamic g in i) { + gg.add(Guild(g)); + } + + guilds = GuildManager(gg); + + int n = i.length; ws.listen((event) { event = json.decode(event); @@ -99,9 +110,14 @@ class Client extends EventEmitter { case "READY": resumeGatewayURL = event["d"]["resume_gateway_url"]; sessionID = event["d"]["session_id"]; - emit("READY", event["d"]); break; case "GUILD_CREATE": + guilds.cache.set(event["d"]["id"], Guild(event["d"])); + if (n > 1) { + n--; + } else { + emit("READY"); + } break; } }, onDone: () { diff --git a/lib/src/requests.dart b/lib/src/requests.dart index f5f7ae2..a45fb84 100644 --- a/lib/src/requests.dart +++ b/lib/src/requests.dart @@ -44,11 +44,10 @@ class Sender { }; } - Future getServer() async { + Future getServers() 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 1c8a370..6ebc41f 100644 --- a/lib/src/types.dart +++ b/lib/src/types.dart @@ -1,9 +1,6 @@ // ignore_for_file: constant_identifier_names - import "dart:convert"; - import "package:http/http.dart" as http; - import "main.dart"; class GatewayIntentBits { @@ -38,6 +35,9 @@ class Collection { } dynamic get(String key) { + if (_variables[key] == null) { + throw Exception("Variable not found for $key"); + } return _variables[key]; } @@ -54,19 +54,54 @@ class Collection { _variables[key] -= value; return value - _variables[key]; } + + Map getAll() { + return _variables; + } + + List keys() { + return _variables.keys.toList(); + } + + List values() { + return _variables.values.toList(); + } } class Guild { String id = ''; String name = ''; - String owner = ''; - String description = ''; + String? owner; + String? description; + ChannelManager channels = ChannelManager([]); + MemberManager members = MemberManager([], ''); + RoleManager roles = RoleManager([]); Guild(Map data) { id = data["id"]; name = data["name"]; description = data["description"]; owner = data["owner_id"]; + + if(data["channels"] != null && data["members"] != null && data["roles"] != null) { + List cc = []; + for (var c in data["channels"]) { + cc.add(Channel(c)); + } + channels = ChannelManager(cc); + + List mm = []; + for (var m in data["members"]) { + mm.add(Member(m)); + } + members = MemberManager(mm, id); + + List rr = []; + for (var r in data["roles"]) { + rr.add(Role(r)); + } + roles = RoleManager(rr); + } } } @@ -120,31 +155,58 @@ class ChannelManager { } } -class Role { - // -} - -class RoleManager { - // -} - class Member { - User? user; - RoleManager? roles; - String joinedAt = ''; - int flags = 0; - + String id = ''; + String name = ''; + User user = User({}); Member(Map data) { + id = data["user"]["id"]; + name = data["user"]["username"]; user = User(data["user"]); - joinedAt = data["joined_at"]; - int flags = 0; } } - class MemberManager { - // + final Collection cache = Collection(); + String id; + + MemberManager(List members, this.id) { + for (var member in members) { + cache.set(member.id, member); + } + } + + fetch(String id) async { + dynamic res = await http.get(Uri.parse("$apiURL/guilds/${this.id}/members/$id")); + if (res.statusCode != 200) { + throw Exception("Error ${res.statusCode} receiving the member"); + } + + res = json.decode(res.body); + cache.set(id, res); + return res; + } +} + +class Role { + String id = ''; + String name = ''; + + Role(Map data) { + id = data["id"]; + name = data["name"]; + } +} + +class RoleManager { + final Collection cache = Collection(); + + RoleManager(List roles) { + for (var role in roles) { + cache.set(role.id, role); + } + } } class User { @@ -157,13 +219,16 @@ class User { String avatar = ''; User(Map data) { + if (data["id"] == null) { + return; + } id = data["id"]; - bot = data["bot"]; + // 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"; + // globalName = data["global_name"]; + // displayName = data["display_name"]; + // discriminator = data["discriminator"]; + // avatar = "https://cdn.discordapp.com/avatars/$id/${data["avatar"]}.webp"; } }