Updating guild class

Co-authored-by: birlino <Birlino@users.noreply.github.com>
This commit is contained in:
KillerBossOrig 2023-08-07 12:15:56 +02:00
parent ad9426a871
commit 0d7c0bbd7d
4 changed files with 68 additions and 22 deletions

View file

@ -26,10 +26,24 @@ class Guild {
Future<Member>? owner; // | | Future<Member>? owner; // | |
String? ownerId; // x | x | String? ownerId; // x | x |
String? permissions; // | | x String? permissions; // | | x
String? afkChannelId; // x | x |
int? afkTimeout; // x | x |
bool? widgetEnabled; // x | x |
String? widgetChannelId; // x | x |
int? verificationLevel; // x | x |
int? defaultMessageNotifications; // x | x |
int? explicitContentFilter; // x | x |
List? features; // | | x
int? mfaLevel; // x | x |
String? applicationId; // x | x |
String? systemChannelId; // x | x |
int? systemChannelFlags; // x | x |
String? rulesChannelId; // x | x |
String? description; String? description;
String? joinedAt; String? joinedAt; // x | |
bool? large; bool? large; // x | |
int? memberCount; int? memberCount; // x | |
Guild(this._sender, Map data) { Guild(this._sender, Map data) {
id = data["id"]; id = data["id"];
@ -60,7 +74,7 @@ class Guild {
roles = RoleManager([]); roles = RoleManager([]);
} }
unavailable = data["unavailable"]; unavailable = data["unavailable"] ?? false;
name = data["name"]; name = data["name"];
icon = data["icon"]; icon = data["icon"];
iconHash = data["icon_hash"]; iconHash = data["icon_hash"];
@ -74,7 +88,22 @@ class Guild {
ownerId = data["owner_id"]; ownerId = data["owner_id"];
owner = members?.fetch(ownerId.toString()); owner = members?.fetch(ownerId.toString());
permissions = data["permissions"]; permissions = data["permissions"];
afkChannelId = data["afk_channel_id"];
afkTimeout = data["afk_timeout"];
widgetEnabled = data["widget_enabled"];
widgetChannelId = data["widget_channel_id"];
verificationLevel = data["verification_level"];
defaultMessageNotifications = data["default_message_notifications"];
explicitContentFilter = data["explicit_content_filter"];
features = data["features"];
mfaLevel = data["mfa_level"];
applicationId = data["application_id"];
systemChannelId = data["system_channel_id"];
systemChannelFlags = data["system_channel_flags"];
rulesChannelId = data["rules_channel_id"];
description = data["description"]; description = data["description"];
// GuildCreate only
if (data["joined_at"] != null) { if (data["joined_at"] != null) {
joinedAt = data["joined_at"]; joinedAt = data["joined_at"];
} }

View file

@ -15,7 +15,13 @@ class GuildManager {
Future<Guild> fetch(String id) async { Future<Guild> fetch(String id) async {
var res = await _sender.fetchGuild(id); var res = await _sender.fetchGuild(id);
final guild = Guild(_sender, res); final guild = Guild(_sender, res);
final oldGuild = cache.get(guild.id); Guild oldGuild = cache.get(guild.id);
oldGuild.members?.cache.values().forEach((member) {
guild.members?.cache.set(member.id, member);
});
oldGuild.channels?.cache.values().forEach((channel) {
guild.channels?.cache.set(channel.id, channel);
});
guild.joinedAt = oldGuild.joinedAt; guild.joinedAt = oldGuild.joinedAt;
guild.large = oldGuild.large; guild.large = oldGuild.large;
guild.unavailable = oldGuild.unavailable; guild.unavailable = oldGuild.unavailable;

View file

@ -13,9 +13,17 @@ class MemberManager {
} }
} }
Future<Member> fetch(String id) async { Future<Member> fetch(String id, {bool forceFetch = false}) async {
if (!forceFetch) {
if (cache.has(id)) {
return cache.get(id);
} else {
return await fetch(id, forceFetch: true);
}
} else {
var res = await _sender.fetchMember(this.id, id); var res = await _sender.fetchMember(this.id, id);
cache.set(id, Member(res)); cache.set(id, Member(res));
return Member(res); return Member(res);
} }
} }
}

View file

@ -133,8 +133,11 @@ class Client extends EventEmitter {
guilds.cache.set(event['d']["id"], Guild(sender, event['d'])); guilds.cache.set(event['d']["id"], Guild(sender, event['d']));
if (n > 1) { if (n > 1) {
n--; n--;
} else { } else if (n == 1) {
n--;
emit("READY"); emit("READY");
} else {
emit("GUILD_CREATE");
} }
break; break;
} }