Add managers

This commit is contained in:
Killer Boss Original 2023-08-03 18:11:30 +02:00
parent 64ebee326b
commit 4f2da16910
3 changed files with 112 additions and 32 deletions

View file

@ -26,6 +26,8 @@ class Client extends EventEmitter {
dynamic ws; dynamic ws;
String resumeGatewayURL = ""; String resumeGatewayURL = "";
String sessionID = ""; String sessionID = "";
dynamic guilds;
dynamic ready;
Client({this.intents = 0}); Client({this.intents = 0});
@ -74,8 +76,17 @@ class Client extends EventEmitter {
} }
Sender sender = Sender(token); Sender sender = Sender(token);
var i = await sender.getServer(); var i = await sender.getServers();
GuildManager(i);
List<Guild> gg = [];
for (dynamic g in i) {
gg.add(Guild(g));
}
guilds = GuildManager(gg);
int n = i.length;
ws.listen((event) { ws.listen((event) {
event = json.decode(event); event = json.decode(event);
@ -99,9 +110,14 @@ class Client extends EventEmitter {
case "READY": case "READY":
resumeGatewayURL = event["d"]["resume_gateway_url"]; resumeGatewayURL = event["d"]["resume_gateway_url"];
sessionID = event["d"]["session_id"]; sessionID = event["d"]["session_id"];
emit("READY", event["d"]);
break; break;
case "GUILD_CREATE": case "GUILD_CREATE":
guilds.cache.set(event["d"]["id"], Guild(event["d"]));
if (n > 1) {
n--;
} else {
emit("READY");
}
break; break;
} }
}, onDone: () { }, onDone: () {

View file

@ -44,11 +44,10 @@ class Sender {
}; };
} }
Future getServer() async { Future getServers() async {
final url = Uri.parse("$apiURL/users/@me/guilds"); final url = Uri.parse("$apiURL/users/@me/guilds");
dynamic res = await http.get(url, headers: headers); dynamic res = await http.get(url, headers: headers);
res = json.decode(res.body); res = json.decode(res.body);
return res; return res;
} }
} }

View file

@ -1,9 +1,6 @@
// ignore_for_file: constant_identifier_names // ignore_for_file: constant_identifier_names
import "dart:convert"; import "dart:convert";
import "package:http/http.dart" as http; import "package:http/http.dart" as http;
import "main.dart"; import "main.dart";
class GatewayIntentBits { class GatewayIntentBits {
@ -38,6 +35,9 @@ class Collection {
} }
dynamic get(String key) { dynamic get(String key) {
if (_variables[key] == null) {
throw Exception("Variable not found for $key");
}
return _variables[key]; return _variables[key];
} }
@ -54,19 +54,54 @@ class Collection {
_variables[key] -= value; _variables[key] -= value;
return value - _variables[key]; return value - _variables[key];
} }
Map<String, dynamic> getAll() {
return _variables;
}
List<String> keys() {
return _variables.keys.toList();
}
List<dynamic> values() {
return _variables.values.toList();
}
} }
class Guild { class Guild {
String id = ''; String id = '';
String name = ''; String name = '';
String owner = ''; String? owner;
String description = ''; String? description;
ChannelManager channels = ChannelManager([]);
MemberManager members = MemberManager([], '');
RoleManager roles = RoleManager([]);
Guild(Map data) { Guild(Map data) {
id = data["id"]; id = data["id"];
name = data["name"]; name = data["name"];
description = data["description"]; description = data["description"];
owner = data["owner_id"]; owner = data["owner_id"];
if(data["channels"] != null && data["members"] != null && data["roles"] != null) {
List<Channel> cc = [];
for (var c in data["channels"]) {
cc.add(Channel(c));
}
channels = ChannelManager(cc);
List<Member> mm = [];
for (var m in data["members"]) {
mm.add(Member(m));
}
members = MemberManager(mm, id);
List<Role> 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 { class Member {
User? user; String id = '';
RoleManager? roles; String name = '';
String joinedAt = ''; User user = User({});
int flags = 0;
Member(Map data) { Member(Map data) {
id = data["user"]["id"];
name = data["user"]["username"];
user = User(data["user"]); user = User(data["user"]);
joinedAt = data["joined_at"];
int flags = 0;
} }
} }
class MemberManager { class MemberManager {
// final Collection cache = Collection();
String id;
MemberManager(List<Member> 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<Role> roles) {
for (var role in roles) {
cache.set(role.id, role);
}
}
} }
class User { class User {
@ -157,13 +219,16 @@ class User {
String avatar = ''; String avatar = '';
User(Map data) { User(Map data) {
if (data["id"] == null) {
return;
}
id = data["id"]; id = data["id"];
bot = data["bot"]; // bot = data["bot"];
username = data["username"]; username = data["username"];
globalName = data["global_name"]; // globalName = data["global_name"];
displayName = data["display_name"]; // displayName = data["display_name"];
discriminator = data["discriminator"]; // discriminator = data["discriminator"];
avatar = "https://cdn.discordapp.com/avatars/$id/${data["avatar"]}.webp"; // avatar = "https://cdn.discordapp.com/avatars/$id/${data["avatar"]}.webp";
} }
} }