From f52d08d79681e29fb2f723dcfa5779b4700c269f Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 00:25:43 +0200 Subject: [PATCH 01/41] refactor: Guild --- lib/src/api/guilds/guild.dart | 382 +++++++++++++++++++--------------- 1 file changed, 216 insertions(+), 166 deletions(-) diff --git a/lib/src/api/guilds/guild.dart b/lib/src/api/guilds/guild.dart index e1c42c55e..955ded0e2 100644 --- a/lib/src/api/guilds/guild.dart +++ b/lib/src/api/guilds/guild.dart @@ -30,103 +30,153 @@ enum VerificationLevel { } class Guild { - Snowflake id; - String name; - String? icon; - String? iconHash; - String? splash; - String? discoverySplash; - GuildMember owner; - Snowflake ownerId; - int? permissions; - Snowflake? afkChannelId; - late VoiceChannel? afkChannel; - int afkTimeout; - bool widgetEnabled; - Snowflake? widgetChannelId; - VerificationLevel verificationLevel; - int defaultMessageNotifications; - int explicitContentFilter; - GuildRoleManager roles; - List features; - int mfaLevel; - Snowflake? applicationId; - Snowflake? systemChannelId; - late TextChannel? systemChannel; - int systemChannelFlags; - Snowflake? rulesChannelId; - late TextChannel? rulesChannel; - int? maxPresences; - int maxMembers; - String? vanityUrlCode; - String? description; - String? banner; - int premiumTier; - int premiumSubscriptionCount; - String preferredLocale; - Snowflake? publicUpdatesChannelId; - late TextChannel? publicUpdatesChannel; - int maxVideoChannelUsers; - int? approximateMemberCount; - int? approximatePresenceCount; - WelcomeScreen? welcomeScreen; - int nsfwLevel; - StickerManager stickers; - bool premiumProgressBarEnabled; - MemberManager members; - ChannelManager channels; - EmojiManager emojis; - ModerationRuleManager moderationRules; - GuildWebhookManager webhooks; - GuildScheduledEventManager scheduledEvents; - - Guild({ - required this.id, - required this.name, - required this.icon, - required this.iconHash, - required this.splash, - required this.discoverySplash, - required this.owner, - required this.ownerId, - required this.permissions, - required this.afkChannelId, - required this.afkTimeout, - required this.widgetEnabled, - required this.widgetChannelId, - required this.verificationLevel, - required this.defaultMessageNotifications, - required this.explicitContentFilter, - required this.roles, - required this.mfaLevel, - required this.applicationId, - required this.systemChannelId, - required this.systemChannelFlags, - required this.rulesChannelId, - required this.maxPresences, - required this.maxMembers, - required this.vanityUrlCode, - required this.description, - required this.banner, - required this.premiumTier, - required this.premiumSubscriptionCount, - required this.preferredLocale, - required this.publicUpdatesChannelId, - required this.maxVideoChannelUsers, - required this.approximateMemberCount, - required this.approximatePresenceCount, - required this.welcomeScreen, - required this.nsfwLevel, - required this.stickers, - required this.premiumProgressBarEnabled, - required this.members, - required this.channels, - required this.emojis, - required this.features, - required this.moderationRules, - required this.webhooks, - required this.scheduledEvents, - }); + Snowflake _id; + String _name; + String? _icon; + String? _iconHash; + String? _splash; + String? _discoverySplash; + GuildMember _owner; + Snowflake _ownerId; + int? _permissions; + Snowflake? _afkChannelId; + late VoiceChannel? _afkChannel; + int _afkTimeout; + bool _widgetEnabled; + Snowflake? _widgetChannelId; + VerificationLevel _verificationLevel; + int _defaultMessageNotifications; + int _explicitContentFilter; + GuildRoleManager _roles; + List _features; + int _mfaLevel; + Snowflake? _applicationId; + Snowflake? _systemChannelId; + late TextChannel? _systemChannel; + int _systemChannelFlags; + Snowflake? _rulesChannelId; + late TextChannel? _rulesChannel; + int? _maxPresences; + int _maxMembers; + String? _vanityUrlCode; + String? _description; + String? _banner; + int _premiumTier; + int _premiumSubscriptionCount; + String _preferredLocale; + Snowflake? _publicUpdatesChannelId; + late TextChannel? _publicUpdatesChannel; + int _maxVideoChannelUsers; + int? _approximateMemberCount; + int? _approximatePresenceCount; + WelcomeScreen? _welcomeScreen; + int _nsfwLevel; + StickerManager _stickers; + bool _premiumProgressBarEnabled; + MemberManager _members; + ChannelManager _channels; + EmojiManager _emojis; + ModerationRuleManager _moderationRules; + GuildWebhookManager _webhooks; + GuildScheduledEventManager _scheduledEvents; + + Guild( + this._id, + this._name, + this._icon, + this._iconHash, + this._splash, + this._discoverySplash, + this._owner, + this._ownerId, + this._permissions, + this._afkChannelId, + this._afkTimeout, + this._widgetEnabled, + this._widgetChannelId, + this._verificationLevel, + this._defaultMessageNotifications, + this._explicitContentFilter, + this._roles, + this._mfaLevel, + this._applicationId, + this._systemChannelId, + this._systemChannelFlags, + this._rulesChannelId, + this._maxPresences, + this._maxMembers, + this._vanityUrlCode, + this._description, + this._banner, + this._premiumTier, + this._premiumSubscriptionCount, + this._preferredLocale, + this._publicUpdatesChannelId, + this._maxVideoChannelUsers, + this._approximateMemberCount, + this._approximatePresenceCount, + this._welcomeScreen, + this._nsfwLevel, + this._stickers, + this._premiumProgressBarEnabled, + this._members, + this._channels, + this._emojis, + this._features, + this._moderationRules, + this._webhooks, + this._scheduledEvents, + ); + + Snowflake get id => _id; + String get name => _name; + String? get icon => _icon; + String? get iconHash => _iconHash; + String? get splash => _splash; + String? get discoverySplash => _discoverySplash; + GuildMember get owner => _owner; + Snowflake get ownerId => _ownerId; + int? get permissions => _permissions; + Snowflake? get afkChannelId => _afkChannelId; + VoiceChannel? get afkChannel => _afkChannel; + int get afkTimeout => _afkTimeout; + bool get widgetEnabled => _widgetEnabled; + Snowflake? get widgetChannelId => _widgetChannelId; + VerificationLevel get verificationLevel => _verificationLevel; + int get defaultMessageNotifications => _defaultMessageNotifications; + int get explicitContentFilter => _explicitContentFilter; + GuildRoleManager get roles => _roles; + List get features => _features; + int get mfaLevel => _mfaLevel; + Snowflake? get applicationId => _applicationId; + Snowflake? get systemChannelId => _systemChannelId; + TextChannel? get systemChannel => _systemChannel; + int get systemChannelFlags => _systemChannelFlags; + Snowflake? get rulesChannelId => _rulesChannelId; + TextChannel? get rulesChannel => _rulesChannel; + int? get maxPresences => _maxPresences; + int get maxMembers => _maxMembers; + String? get vanityUrlCode => _vanityUrlCode; + String? get description => _description; + String? get banner => _banner; + int get premiumTier => _premiumTier; + int get premiumSubscriptionCount => _premiumSubscriptionCount; + String get preferredLocale => _preferredLocale; + Snowflake? get publicUpdatesChannelId => _publicUpdatesChannelId; + TextChannel? get publicUpdatesChannel => _publicUpdatesChannel; + int get maxVideoChannelUsers => _maxVideoChannelUsers; + int? get approximateMemberCount => _approximateMemberCount; + int? get approximatePresenceCount => _approximatePresenceCount; + WelcomeScreen? get welcomeScreen => _welcomeScreen; + int get nsfwLevel => _nsfwLevel; + StickerManager get stickers => _stickers; + bool get premiumProgressBarEnabled => _premiumProgressBarEnabled; + MemberManager get members => _members; + ChannelManager get channels => _channels; + EmojiManager get emojis => _emojis; + ModerationRuleManager get moderationRules => _moderationRules; + GuildWebhookManager get webhooks => _webhooks; + GuildScheduledEventManager get scheduledEvents => _scheduledEvents; /// ### Modifies the [name] of this. /// @@ -139,7 +189,7 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'name': name }); if (response.statusCode == 200) { - this.name = name; + _name = name; } } @@ -156,7 +206,7 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'verification_level': level.value }); if (response.statusCode == 200) { - verificationLevel = level; + _verificationLevel = level; } } @@ -173,7 +223,7 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'default_message_notifications': level }); if (response.statusCode == 200) { - defaultMessageNotifications = level; + _defaultMessageNotifications = level; } } @@ -192,7 +242,7 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'explicit_content_filter': level }); if (response.statusCode == 200) { - explicitContentFilter = level; + _explicitContentFilter = level; } } @@ -211,8 +261,8 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'afk_channel_id': channel.id }); if (response.statusCode == 200) { - afkChannel = channel; - afkChannelId = channel.id; + _afkChannel = channel; + _afkChannelId = channel.id; } } @@ -242,8 +292,8 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'owner_id': guildMember.user.id }); if (response.statusCode == 200) { - owner = guildMember; - ownerId = guildMember.user.id; + _owner = guildMember; + _ownerId = guildMember.user.id; } } @@ -266,7 +316,7 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'splash': file }); if (response.statusCode == 200) { - splash = file; + _splash = file; } } @@ -287,7 +337,7 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'splash': null }); if (response.statusCode == 200) { - splash = null; + _splash = null; } } @@ -310,7 +360,7 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'discovery_splash': file }); if (response.statusCode == 200) { - discoverySplash = file; + _discoverySplash = file; } } @@ -331,7 +381,7 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'discovery_splash': null }); if (response.statusCode == 200) { - discoverySplash = null; + _discoverySplash = null; } } @@ -354,7 +404,7 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'banner': file }); if (response.statusCode == 200) { - banner = file; + _banner = file; } } @@ -375,7 +425,7 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'banner': null }); if (response.statusCode == 200) { - banner = null; + _banner = null; } } @@ -392,7 +442,7 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'icon': file }); if (response.statusCode == 200) { - icon = file; + _icon = file; } } @@ -407,7 +457,7 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'icon': null }); if (response.statusCode == 200) { - icon = null; + _icon = null; } } @@ -426,8 +476,8 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'system_channel_id': channel.id }); if (response.statusCode == 200) { - systemChannelId = channel.id; - systemChannel = channel; + _systemChannelId = channel.id; + _systemChannel = channel; } } @@ -446,8 +496,8 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'rules_channel_id': channel.id }); if (response.statusCode == 200) { - rulesChannelId = channel.id; - rulesChannel = channel; + _rulesChannelId = channel.id; + _rulesChannel = channel; } } @@ -466,8 +516,8 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'public_updates_channel_id': channel.id }); if (response.statusCode == 200) { - publicUpdatesChannelId = channel.id; - publicUpdatesChannel = channel; + _publicUpdatesChannelId = channel.id; + _publicUpdatesChannel = channel; } } @@ -484,7 +534,7 @@ class Guild { Response response = await http.patch(url: "/guilds/$id", payload: { 'public_updates_channel_id': locale }); if (response.statusCode == 200) { - preferredLocale = locale as String; + _preferredLocale = locale as String; } } @@ -526,56 +576,56 @@ class Guild { if(feature == null) { Console.warn(message: 'Guild feature $element don\'t exist! Please report this to our team.'); } else { - features.add(feature); + features.add(feature); } } return Guild( - id: payload['id'], - name: payload['name'], - icon: payload['icon'], - iconHash: payload['icon_hash'], - splash: payload['splash'], - discoverySplash: payload['discovery_splash'], - owner: memberManager.cache.get(payload['owner_id'])!, - ownerId: payload['owner_id'], - permissions: payload['permissions'], - afkChannelId: payload['afk_channel_id'], - afkTimeout: payload['afk_timeout'], - widgetEnabled: payload['widget_enabled'] ?? false, - widgetChannelId: payload['widget_channel_id'], - verificationLevel: VerificationLevel.values.firstWhere((level) => level.value == payload['verification_level']), - defaultMessageNotifications: payload['default_message_notifications'], - explicitContentFilter: payload['explicit_content_filter'], - roles: roleManager, - features: features, - mfaLevel: payload['mfa_level'], - applicationId: payload['application_id'], - systemChannelId: payload['system_channel_id'], - systemChannelFlags: payload['system_channel_flags'], - rulesChannelId: payload['rules_channel_id'], - maxPresences: payload['max_presences'], - maxMembers: payload['max_members'], - vanityUrlCode: payload['vanity_url_code'], - description: payload['description'], - banner: payload['banner'], - premiumTier: payload['premium_tier'], - premiumSubscriptionCount: payload['premium_subscription_count'], - preferredLocale: payload['preferred_locale'], - publicUpdatesChannelId: payload['public_updates_channel_id'], - maxVideoChannelUsers: payload['max_video_channel_users'], - approximateMemberCount: payload['approximate_member_count'], - approximatePresenceCount: payload['approximate_presence_count'], - nsfwLevel: payload['nsfw_level'], - stickers: stickerManager, - premiumProgressBarEnabled: payload['premium_progress_bar_enabled'], - members: memberManager, - channels: channelManager, - emojis: emojiManager, - welcomeScreen: payload['welcome_screen'] != null ? WelcomeScreen.from(payload['welcome_screen']) : null, - moderationRules: moderationRuleManager, - webhooks: GuildWebhookManager.fromManager(webhookManager: webhookManager), - scheduledEvents: guildScheduledEventManager + payload['id'], + payload['name'], + payload['icon'], + payload['icon_hash'], + payload['splash'], + payload['discovery_splash'], + memberManager.cache.get(payload['owner_id'])!, + payload['owner_id'], + payload['permissions'], + payload['afk_channel_id'], + payload['afk_timeout'], + payload['widget_enabled'] ?? false, + payload['widget_channel_id'], + VerificationLevel.values.firstWhere((level) => level.value == payload['verification_level']), + payload['default_message_notifications'], + payload['explicit_content_filter'], + roleManager, + payload['mfa_level'], + payload['application_id'], + payload['system_channel_id'], + payload['system_channel_flags'], + payload['rules_channel_id'], + payload['max_presences'], + payload['max_members'], + payload['vanity_url_code'], + payload['description'], + payload['banner'], + payload['premium_tier'], + payload['premium_subscription_count'], + payload['preferred_locale'], + payload['public_updates_channel_id'], + payload['max_video_channel_users'], + payload['approximate_member_count'], + payload['approximate_presence_count'], + payload['welcome_screen'] != null ? WelcomeScreen.from(payload['welcome_screen']) : null, + payload['nsfw_level'], + stickerManager, + payload['premium_progress_bar_enabled'], + memberManager, + channelManager, + emojiManager, + features, + moderationRuleManager, + GuildWebhookManager.fromManager(webhookManager: webhookManager), + guildScheduledEventManager ); } } From 5639b09c6f24e28a81a34bed852960b7a493b703 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 00:30:52 +0200 Subject: [PATCH 02/41] refactor: GuildMember --- lib/src/api/guilds/guild_member.dart | 108 +++++++++++++-------------- 1 file changed, 53 insertions(+), 55 deletions(-) diff --git a/lib/src/api/guilds/guild_member.dart b/lib/src/api/guilds/guild_member.dart index 63372f616..b7cabfa7a 100644 --- a/lib/src/api/guilds/guild_member.dart +++ b/lib/src/api/guilds/guild_member.dart @@ -2,33 +2,45 @@ import 'package:http/http.dart'; import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; import 'package:mineral/src/api/managers/guild_role_manager.dart'; -import 'package:mineral/src/api/managers/voice_manager.dart'; class GuildMember { - User user; - String? nickname; - String? avatar; - DateTime joinedAt; - DateTime? premiumSince; - String? permissions; - bool pending; - DateTime? timeoutDuration; - MemberRoleManager roles; - late VoiceManager voice; - late Guild guild; - - GuildMember({ - required this.user, - required this.nickname, - required this.avatar, - required this.joinedAt, - required this.premiumSince, - required this.permissions, - required this.pending, - required this.timeoutDuration, - required this.roles, - required this.voice, - }); + User _user; + String? _nickname; + String? _avatar; + DateTime _joinedAt; + DateTime? _premiumSince; + String? _permissions; + bool _pending; + DateTime? _timeoutDuration; + MemberRoleManager _roles; + VoiceManager _voice; + Guild _guild; + + GuildMember( + this._user, + this._nickname, + this._avatar, + this._joinedAt, + this._premiumSince, + this._permissions, + this._pending, + this._timeoutDuration, + this._roles, + this._voice, + this._guild, + ); + + User get user => _user; + String? get nickname => _nickname; + String? get avatar => _avatar; + DateTime get joinedAt => _joinedAt; + DateTime? get premiumSince => _premiumSince; + String? get permissions => _permissions; + bool get pending => _pending; + DateTime? get timeoutDuration => _timeoutDuration; + MemberRoleManager get roles => _roles; + VoiceManager get voice => _voice; + Guild get guild => _guild; /// ### Update the username of this /// @@ -41,7 +53,7 @@ class GuildMember { Response response = await http.patch(url: "/guilds/${guild.id}/members/${user.id}", payload: { 'nick': name }); if (response.statusCode == 200) { - nickname = name; + _nickname = name; } } @@ -62,7 +74,7 @@ class GuildMember { Response response = await http.patch(url: '/guilds/${guild.id}/members/${user.id}', payload: { 'communication_disabled_until': expiration.toIso8601String() }); if (response.statusCode == 200 || response.statusCode == 204) { - timeoutDuration = expiration; + _timeoutDuration = expiration; } } @@ -77,7 +89,7 @@ class GuildMember { Response response = await http.patch(url: '/guilds/${guild.id}/members/${user.id}', payload: { 'communication_disabled_until': null }); if (response.statusCode == 200 || response.statusCode == 204) { - timeoutDuration = null; + _timeoutDuration = null; } } @@ -102,7 +114,7 @@ class GuildMember { }); if (response.statusCode == 200) { - timeoutDuration = null; + _timeoutDuration = null; } } @@ -138,22 +150,7 @@ class GuildMember { return "<@${nickname != null ? '!' : ''}${user.id}>"; } - clone () { - return GuildMember( - user: user, - nickname: nickname, - avatar: avatar, - joinedAt: joinedAt, - premiumSince: premiumSince, - permissions: permissions, - pending: pending, - timeoutDuration: timeoutDuration, - roles: roles, - voice: voice - ) - ..guild = guild - ..roles = roles; - } + GuildMember clone () => GuildMember(user, nickname, avatar, joinedAt, premiumSince, permissions, pending, timeoutDuration, roles, voice, guild); factory GuildMember.from({ required user, required GuildRoleManager roles, dynamic member, required Snowflake guildId, required VoiceManager voice }) { MemberRoleManager memberRoleManager = MemberRoleManager(manager: roles, memberId: user.id); @@ -165,16 +162,17 @@ class GuildMember { } return GuildMember( - user: user, - nickname: member['nick'], - avatar: member['avatar'], - joinedAt: DateTime.parse(member['joined_at']), - premiumSince: member['premium_since'] != null ? DateTime.parse(member['premium_since']) : null, - permissions: member['permissions'], - pending: member['pending'] == true, - timeoutDuration: member['communication_disabled_until'] != null ? DateTime.parse(member['communication_disabled_until']) : null, - roles: memberRoleManager, - voice: voice, + user, + member['nick'], + member['avatar'], + DateTime.parse(member['joined_at']), + member['premium_since'] != null ? DateTime.parse(member['premium_since']) : null, + member['permissions'], + member['pending'] == true, + member['communication_disabled_until'] != null ? DateTime.parse(member['communication_disabled_until']) : null, + memberRoleManager, + voice, + roles.guild ); } } From e90e4110d1b8c44e9bdf37d33279e537d249fe95 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 00:58:20 +0200 Subject: [PATCH 03/41] refactor: Interactions --- .../api/interactions/button_interaction.dart | 50 +++++++++------- .../api/interactions/command_interaction.dart | 49 ++++++++------- .../context_message_interaction.dart | 47 +++++++++------ .../context_user_interaction.dart | 47 +++++++++------ lib/src/api/interactions/interaction.dart | 43 +++++++++----- .../api/interactions/modal_interaction.dart | 49 ++++++++------- .../interactions/select_menu_interaction.dart | 59 +++++++++++-------- 7 files changed, 200 insertions(+), 144 deletions(-) diff --git a/lib/src/api/interactions/button_interaction.dart b/lib/src/api/interactions/button_interaction.dart index 33b97cdae..71c624c9f 100644 --- a/lib/src/api/interactions/button_interaction.dart +++ b/lib/src/api/interactions/button_interaction.dart @@ -1,33 +1,39 @@ import 'dart:core'; import 'package:mineral/api.dart'; -import 'package:mineral/src/api/interactions/interaction.dart'; class ButtonInteraction extends Interaction { - Message? message; - Snowflake customId; + Message? _message; + Snowflake _customId; - ButtonInteraction({ - required this.message, - required this.customId, - required InteractionType type, - required Snowflake applicationId, - required Snowflake id, - required int version, - required String token, - required User user - }) : super(id: id, version: version, token: token, type: type, user: user, applicationId: applicationId); + ButtonInteraction( + super.id, + super.applicationId, + super.version, + super.type, + super.token, + super.user, + super.guild, + super.member, + this._message, + this._customId, + ); - factory ButtonInteraction.from({ required User user, required Message? message, required dynamic payload }) { + Message? get message => _message; + Snowflake get customId => _customId; + + factory ButtonInteraction.from({ required User user, required Guild guild, required Message message, required dynamic payload }) { return ButtonInteraction( - id: payload['id'], - applicationId: payload['application_id'], - type: InteractionType.messageComponent, - version: payload['version'], - token: payload['token'], - user: user, - message: message, - customId: payload['data']['custom_id'] + payload['id'], + payload['application_id'], + payload['version'], + InteractionType.messageComponent, + payload['token'], + user, + guild, + guild.members.cache.getOrFail(user.id), + message, + payload['data']['custom_id'] ); } } diff --git a/lib/src/api/interactions/command_interaction.dart b/lib/src/api/interactions/command_interaction.dart index 21b4bccef..a3ec164db 100644 --- a/lib/src/api/interactions/command_interaction.dart +++ b/lib/src/api/interactions/command_interaction.dart @@ -2,22 +2,28 @@ import 'dart:core'; import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; -import 'package:mineral/src/api/interactions/interaction.dart'; class CommandInteraction extends Interaction { - String identifier; - TextBasedChannel? channel; + String _identifier; + TextBasedChannel? _channel; + Map data = {}; - CommandInteraction({ - required this.identifier, - required InteractionType type, - required Snowflake applicationId, - required Snowflake id, - required int version, - required String token, - required User user, - }) : super(id: id, version: version, token: token, type: type, user: user, applicationId: applicationId); + CommandInteraction( + super._id, + super._applicationId, + super._version, + super._type, + super._token, + super._user, + super._guild, + super._member, + this._identifier, + this._channel, + ); + + String get identifier => _identifier; + TextBasedChannel? get channel => _channel; /// ### Returns an instance of [Channel] if the command has the designed option /// @@ -111,15 +117,18 @@ class CommandInteraction extends Interaction { return data[optionName]?['value']; } - factory CommandInteraction.from({ required User user, required dynamic payload }) { + factory CommandInteraction.from({ required User user, required Guild? guild, required dynamic payload }) { return CommandInteraction( - id: payload['id'], - applicationId: payload['application_id'], - type: InteractionType.applicationCommand, - identifier: payload['data']['name'], - version: payload['version'], - token: payload['token'], - user: user, + payload['id'], + payload['application_id'], + payload['version'], + InteractionType.applicationCommand, + payload['token'], + user, + guild, + guild?.members.cache.get(user.id), + payload['data']['name'], + guild?.channels.cache.get(payload['channel_id']), ); } } diff --git a/lib/src/api/interactions/context_message_interaction.dart b/lib/src/api/interactions/context_message_interaction.dart index b630d1e44..8a193b362 100644 --- a/lib/src/api/interactions/context_message_interaction.dart +++ b/lib/src/api/interactions/context_message_interaction.dart @@ -1,28 +1,37 @@ import 'package:mineral/api.dart'; class ContextMessageInteraction extends Interaction { - Message message; - late Channel channel; + Message _message; + Channel _channel; - ContextMessageInteraction({ - required this.message, - required super.id, - required super.applicationId, - required super.version, - required super.type, - required super.token, - required super.user - }); + ContextMessageInteraction( + super._id, + super._applicationId, + super._version, + super._type, + super._token, + super._user, + super._guild, + super._member, + this._message, + this._channel, + ); - factory ContextMessageInteraction.from({ required User user, required Message message, required dynamic payload }) { + Message get message => _message; + Channel get channel => _channel; + + factory ContextMessageInteraction.from({ required User user, required Guild guild, required Message message, required dynamic payload }) { return ContextMessageInteraction( - message: message, - id: payload['id'], - applicationId: payload['application_id'], - version: payload['version'], - type: InteractionType.values.firstWhere((element) => element.value == payload['type']), - token: payload['token'], - user: user + payload['id'], + payload['application_id'], + payload['version'], + InteractionType.values.firstWhere((element) => element.value == payload['type']), + payload['token'], + user, + guild, + guild.members.cache.get(user.id), + message, + message.channel, ); } } diff --git a/lib/src/api/interactions/context_user_interaction.dart b/lib/src/api/interactions/context_user_interaction.dart index 107b30ef6..10944f80c 100644 --- a/lib/src/api/interactions/context_user_interaction.dart +++ b/lib/src/api/interactions/context_user_interaction.dart @@ -1,28 +1,37 @@ import 'package:mineral/api.dart'; class ContextUserInteraction extends Interaction { - GuildMember? target; - Channel? channel; + GuildMember? _target; + Channel? _channel; - ContextUserInteraction({ - required this.target, - required super.id, - required super.applicationId, - required super.version, - required super.type, - required super.token, - required super.user - }); + ContextUserInteraction( + super._id, + super._applicationId, + super._version, + super._type, + super._token, + super._user, + super._guild, + super._member, + this._target, + this._channel, + ); - factory ContextUserInteraction.from({ required GuildMember? target, required User user, required dynamic payload }) { + GuildMember? get target => _target; + Channel? get channel => _channel; + + factory ContextUserInteraction.from({ required GuildMember? target, required Guild guild, required User user, required dynamic payload }) { return ContextUserInteraction( - target: target, - id: payload['id'], - applicationId: payload['application_id'], - version: payload['version'], - type: InteractionType.values.firstWhere((element) => element.value == payload['type']), - token: payload['token'], - user: user + payload['id'], + payload['application_id'], + payload['version'], + InteractionType.values.firstWhere((element) => element.value == payload['type']), + payload['token'], + user, + guild, + guild.members.cache.get(user.id), + target, + guild.channels.cache.getOrFail('channel_id') ); } } diff --git a/lib/src/api/interactions/interaction.dart b/lib/src/api/interactions/interaction.dart index 0000dbc9a..9bf3818c2 100644 --- a/lib/src/api/interactions/interaction.dart +++ b/lib/src/api/interactions/interaction.dart @@ -15,16 +15,25 @@ enum InteractionCallbackType { } class Interaction { - Snowflake id; - Snowflake applicationId; - int version; - InteractionType type; - String token; - User user; - Guild? guild; - late GuildMember? member; + Snowflake _id; + Snowflake _applicationId; + int _version; + InteractionType _type; + String _token; + User _user; + Guild? _guild; + GuildMember? _member; - Interaction({ required this.id, required this.applicationId, required this.version, required this.type, required this.token, required this.user }); + Interaction(this._id, this._applicationId, this._version, this._type, this._token, this._user, this._guild, this._member); + + Snowflake get id => _id; + Snowflake get applicationId => _applicationId; + int get version => _version; + InteractionType get type => _type; + String get token => _token; + User get user => _user; + Guild? get guild => _guild; + GuildMember? get member => _member; /// ### Responds to this by an [Message] /// @@ -80,14 +89,16 @@ class Interaction { }); } - factory Interaction.from({ required User user, required dynamic payload }) { + factory Interaction.from({ required User user, required Guild guild, required dynamic payload }) { return Interaction( - id: payload['id'], - applicationId: payload['application_id'], - version: payload['version'], - type: InteractionType.values.firstWhere((element) => element.value == payload['type']), - token: payload['token'], - user: user + payload['id'], + payload['application_id'], + payload['version'], + InteractionType.values.firstWhere((element) => element.value == payload['type']), + payload['token'], + user, + guild, + guild.members.cache.getOrFail(user.id) ); } } diff --git a/lib/src/api/interactions/modal_interaction.dart b/lib/src/api/interactions/modal_interaction.dart index 099b9934a..fceb4b891 100644 --- a/lib/src/api/interactions/modal_interaction.dart +++ b/lib/src/api/interactions/modal_interaction.dart @@ -1,23 +1,27 @@ import 'dart:core'; import 'package:mineral/api.dart'; -import 'package:mineral/src/api/interactions/interaction.dart'; class ModalInteraction extends Interaction { - Message? message; - Snowflake customId; + Message? _message; + Snowflake _customId; + Map data = {}; - ModalInteraction({ - required this.message, - required this.customId, - required InteractionType type, - required Snowflake applicationId, - required Snowflake id, - required int version, - required String token, - required User user - }) : super(id: id, version: version, token: token, type: type, user: user, applicationId: applicationId); + ModalInteraction( + super._id, + super._applicationId, + super._version, + super._type, + super._token, + super._user, + super._guild, + super._member, + this._customId, + ); + + Message? get message => _message; + Snowflake get customId => _customId; /// ### Return an [String] if the modal has the designed field /// @@ -27,16 +31,17 @@ class ModalInteraction extends Interaction { /// ``` String? getText(String customId) => data.get(customId); - factory ModalInteraction.from({ required User user, required Message? message, required dynamic payload }) { + factory ModalInteraction.from({ required User user, required Message? message, required Guild guild, required dynamic payload }) { return ModalInteraction( - id: payload['id'], - applicationId: payload['application_id'], - type: InteractionType.modalSubmit, - version: payload['version'], - token: payload['token'], - user: user, - message: message, - customId: payload['data']['custom_id'] + payload['id'], + payload['application_id'], + payload['version'], + InteractionType.modalSubmit, + payload['token'], + user, + guild, + guild.members.cache.getOrFail(user.id), + payload['data']['custom_id'] ); } } diff --git a/lib/src/api/interactions/select_menu_interaction.dart b/lib/src/api/interactions/select_menu_interaction.dart index 89752b52f..0cb6cdaa4 100644 --- a/lib/src/api/interactions/select_menu_interaction.dart +++ b/lib/src/api/interactions/select_menu_interaction.dart @@ -1,23 +1,28 @@ import 'dart:core'; import 'package:mineral/api.dart'; -import 'package:mineral/src/api/interactions/interaction.dart'; class SelectMenuInteraction extends Interaction { - Message? message; - Snowflake customId; - List data = []; - - SelectMenuInteraction({ - required this.message, - required this.customId, - required InteractionType type, - required Snowflake applicationId, - required Snowflake id, - required int version, - required String token, - required User user - }) : super(id: id, version: version, token: token, type: type, user: user, applicationId: applicationId); + Message? _message; + Snowflake _customId; + + final List _data = []; + + SelectMenuInteraction( + super._id, + super._applicationId, + super._version, + super._type, + super._token, + super._user, + super._guild, + super._member, + this._message, + this._customId, + ); + + Message? get message => _message; + Snowflake get customId => _customId; /// ### Return an [List] of [T] if this has the designed field /// @@ -26,7 +31,7 @@ class SelectMenuInteraction extends Interaction { /// List? fields = interaction.getValues(); /// List? fields = interaction.getValues(); /// ``` - List getValues () => data as List; + List getValues () => _data as List; /// ### Return the first [T] if this has the designed field /// @@ -35,18 +40,20 @@ class SelectMenuInteraction extends Interaction { /// String? field = interaction.getValue(); /// int? field = interaction.getValue(); /// ``` - T getValue () => data.first as T; + T getValue () => _data.first as T; - factory SelectMenuInteraction.from({ required User user, required Message? message, required dynamic payload }) { + factory SelectMenuInteraction.from({ required User user, required Message? message, required Guild? guild, required dynamic payload }) { return SelectMenuInteraction( - id: payload['id'], - applicationId: payload['application_id'], - type: InteractionType.messageComponent, - version: payload['version'], - token: payload['token'], - user: user, - message: message, - customId: payload['data']['custom_id'] + payload['id'], + payload['application_id'], + payload['version'], + InteractionType.messageComponent, + payload['token'], + user, + guild, + guild?.members.cache.get(user.id), + message, + payload['data']['custom_id'], ); } } From 6561757afd18af6d87ad8a1000a522fcc91f2de0 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:53:39 +0200 Subject: [PATCH 04/41] refactor: API --- lib/api.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/api.dart b/lib/api.dart index 18485c62e..d8301bb96 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -25,7 +25,6 @@ export 'src/api/channels/text_based_channel.dart' show TextBasedChannel; export 'src/api/channels/text_channel.dart' show TextChannel; export 'src/api/channels/category_channel.dart' show CategoryChannel; export 'src/api/channels/permission_overwrite.dart' show PermissionOverwrite, PermissionOverwriteType; -export 'src/api/managers/permission_overwrite_manager.dart' show PermissionOverwriteManager; export 'src/api/messages/message.dart' show Message; export 'src/api/messages/message_embed.dart' show MessageEmbed, Footer, Image, Thumbnail, Author, Field; From 08c7ad94670e9e65d7975ddd5aeae7dd49c5048a Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:53:53 +0200 Subject: [PATCH 05/41] refactor: CacheManager --- lib/src/api/managers/cache_manager.dart | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/src/api/managers/cache_manager.dart b/lib/src/api/managers/cache_manager.dart index 84e7cf6ec..4f8d8fd47 100644 --- a/lib/src/api/managers/cache_manager.dart +++ b/lib/src/api/managers/cache_manager.dart @@ -1,9 +1,6 @@ import 'package:mineral/api.dart'; abstract class CacheManager { - Map cache = {}; - - Future> sync () async { - throw UnimplementedError(); - } + final Map _cache = {}; + Map get cache => _cache; } From 10ed5d9083ee5084f4b095c4274e449da4192639 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:54:07 +0200 Subject: [PATCH 06/41] refactor: EmojiManager --- lib/src/api/managers/emoji_manager.dart | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/src/api/managers/emoji_manager.dart b/lib/src/api/managers/emoji_manager.dart index 91113d4ba..f6757f6c4 100644 --- a/lib/src/api/managers/emoji_manager.dart +++ b/lib/src/api/managers/emoji_manager.dart @@ -7,26 +7,20 @@ import 'package:mineral/exception.dart'; import 'package:mineral/helper.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; -class EmojiManager implements CacheManager { - @override - Map cache = {}; +class EmojiManager extends CacheManager { + late final Guild _guild; + Guild get guild => _guild; - Snowflake? guildId; - late Guild? guild; - - EmojiManager({ required this.guildId }); - - @override Future> sync () async { Http http = ioc.singleton(ioc.services.http); cache.clear(); - Response response = await http.get(url: "/guilds/$guildId/emojis"); + Response response = await http.get(url: "/guilds/${_guild.id}/emojis"); dynamic payload = jsonDecode(response.body); for(dynamic element in payload) { Emoji emoji = Emoji.from( - memberManager: guild!.members, + memberManager: _guild.members, //roleManager: guild!.roles, emojiManager: this, payload: element @@ -45,14 +39,15 @@ class EmojiManager implements CacheManager { Http http = ioc.singleton(ioc.services.http); String image = await Helper.getPicture(path); - Response response = await http.post(url: "/guilds/$guildId/emojis", payload: { + + Response response = await http.post(url: "/guilds/${_guild.id}/emojis", payload: { 'name': label, 'image': image, 'roles': roles ?? [], }); Emoji emoji = Emoji.from( - memberManager: guild!.members, + memberManager: _guild.members, //roleManager: guild!.roles, emojiManager: this, payload: jsonDecode(response.body), From d8e73c8c73556f5efcf066f036ae7a9a5eaeddab Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:54:14 +0200 Subject: [PATCH 07/41] refactor: GuildManager --- lib/src/api/managers/guild_manager.dart | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/src/api/managers/guild_manager.dart b/lib/src/api/managers/guild_manager.dart index 5df137267..1a45986ef 100644 --- a/lib/src/api/managers/guild_manager.dart +++ b/lib/src/api/managers/guild_manager.dart @@ -1,12 +1,5 @@ import 'package:mineral/api.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; -class GuildManager implements CacheManager { - @override - Map cache = {}; - - @override - Future> sync() { - throw UnimplementedError(); - } +class GuildManager extends CacheManager { } From baa4f15b3108f2aa27c23b0e15e32b11bea99376 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:54:23 +0200 Subject: [PATCH 08/41] refactor: GuildRoleManager --- lib/src/api/managers/guild_role_manager.dart | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/src/api/managers/guild_role_manager.dart b/lib/src/api/managers/guild_role_manager.dart index ea2d60faf..9a53e84b1 100644 --- a/lib/src/api/managers/guild_role_manager.dart +++ b/lib/src/api/managers/guild_role_manager.dart @@ -7,14 +7,9 @@ import 'package:mineral/exception.dart'; import 'package:mineral/helper.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; -class GuildRoleManager implements CacheManager { - @override - Map cache = {}; - - Snowflake guildId; - late Guild guild; - - GuildRoleManager({ required this.guildId }); +class GuildRoleManager extends CacheManager { + late final Guild _guild; + Guild get guild => _guild; /// Synchronise the cache from the Discord API /// @@ -22,12 +17,11 @@ class GuildRoleManager implements CacheManager { /// ```dart /// await guild.roles.sync(); /// ``` - @override Future> sync () async { Http http = ioc.singleton(ioc.services.http); cache.clear(); - Response response = await http.get(url: "/guilds/$guildId/roles"); + Response response = await http.get(url: "/guilds/${_guild.id}/roles"); dynamic payload = jsonDecode(response.body); for(dynamic element in payload) { @@ -55,15 +49,15 @@ class GuildRoleManager implements CacheManager { /// ); /// ``` Future create ({ required String label, Color? color, bool? hoist, String? icon, String? unicode, bool? mentionable, List? permissions }) async { - if ((icon != null || unicode != null) && !guild.features.contains('ROLE_ICONS')) { - throw MissingFeatureException(cause: "Guild ${guild.name} has no 'ROLE_ICONS' feature."); + if ((icon != null || unicode != null) && !_guild.features.contains('ROLE_ICONS')) { + throw MissingFeatureException(cause: "Guild ${_guild.name} has no 'ROLE_ICONS' feature."); } String? _icon = icon != null ? await Helper.getPicture(icon) : null; int? _permissions = permissions != null ? Helper.reduceRolePermissions(permissions) : null; Http http = ioc.singleton(ioc.services.http); - Response response = await http.post(url: "/guilds/$guildId/roles", payload: { + Response response = await http.post(url: "/guilds/${_guild.id}/roles", payload: { 'name': label, 'color': color != null ? Helper.toRgbColor(color) : null, 'hoist': hoist ?? false, From 89e98496d14f17c0df8486e7a306db14bfed4e4c Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:54:35 +0200 Subject: [PATCH 09/41] refactor: GuildScheduledEventManager --- .../guild_scheduled_event_manager.dart | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/src/api/managers/guild_scheduled_event_manager.dart b/lib/src/api/managers/guild_scheduled_event_manager.dart index 943f67c5c..474707bf2 100644 --- a/lib/src/api/managers/guild_scheduled_event_manager.dart +++ b/lib/src/api/managers/guild_scheduled_event_manager.dart @@ -5,28 +5,22 @@ import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; -class GuildScheduledEventManager implements CacheManager { - @override - Map cache = {}; +class GuildScheduledEventManager extends CacheManager { + late final Guild _guild; + Guild get guild => _guild; - Snowflake? guildId; - late Guild guild; - - GuildScheduledEventManager({required this.guildId}); - - @override Future> sync() async { final Http http = ioc.singleton(ioc.services.http); - Response response = await http.get(url: "/guilds/$guildId/scheduled-events"); + Response response = await http.get(url: "/guilds/${_guild.id}/scheduled-events"); if (response.statusCode == 200) { cache.clear(); dynamic payload = jsonDecode(response.body); for (dynamic element in payload) { GuildScheduledEvent event = GuildScheduledEvent.from( - channelManager: guild.channels, - memberManager: guild.members, + channelManager: _guild.channels, + memberManager: _guild.members, payload: element ); From 7aeea46b6f2491c30b2fe2e25dc11d385452ced4 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:54:43 +0200 Subject: [PATCH 10/41] refactor: GuildWebhookManager --- .../api/managers/guild_webhook_manager.dart | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/lib/src/api/managers/guild_webhook_manager.dart b/lib/src/api/managers/guild_webhook_manager.dart index 4f803b0a1..431aa9b0d 100644 --- a/lib/src/api/managers/guild_webhook_manager.dart +++ b/lib/src/api/managers/guild_webhook_manager.dart @@ -5,21 +5,16 @@ import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; import 'package:mineral/src/api/managers/webhook_manager.dart'; -import 'package:mineral/src/api/webhook.dart'; -class GuildWebhookManager implements CacheManager { - @override - Map cache = {}; +class GuildWebhookManager extends CacheManager { + late final Guild _guild; + Guild get guild => _guild; - Snowflake guildId; - Guild? guild; + GuildWebhookManager(); - GuildWebhookManager({ required this.guildId }); - - @override Future> sync () async { Http http = ioc.singleton(ioc.services.http); - Response response = await http.get(url: "/guilds/$guildId/webhooks"); + Response response = await http.get(url: "/guilds/${_guild.id}/webhooks"); for (dynamic element in jsonDecode(response.body)) { Webhook webhook = Webhook.from(payload: element); @@ -30,8 +25,8 @@ class GuildWebhookManager implements CacheManager { } factory GuildWebhookManager.fromManager({ required WebhookManager webhookManager }) { - GuildWebhookManager guildWebhookManager = GuildWebhookManager(guildId: webhookManager.guildId!); - guildWebhookManager.cache = webhookManager.cache; + final guildWebhookManager = GuildWebhookManager(); + guildWebhookManager.cache.addAll(webhookManager.cache); return guildWebhookManager; } From 0886e65e214d16df6fc27614f97c42fc93731c2c Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:54:54 +0200 Subject: [PATCH 11/41] refactor: DmChannelManager --- lib/src/api/managers/dm_channel_manager.dart | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/lib/src/api/managers/dm_channel_manager.dart b/lib/src/api/managers/dm_channel_manager.dart index 12bbd94d5..ce52fc784 100644 --- a/lib/src/api/managers/dm_channel_manager.dart +++ b/lib/src/api/managers/dm_channel_manager.dart @@ -1,15 +1,5 @@ -import 'package:mineral/api.dart'; import 'package:mineral/src/api/channels/dm_channel.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; -class DmChannelManager implements CacheManager { - @override - Map cache = {}; - - DmChannelManager(); - - @override - Future> sync () async { - return cache; - } +class DmChannelManager extends CacheManager { } From f4842cc476382ea12cf848bedb1a40e4fe590fb6 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:55:32 +0200 Subject: [PATCH 12/41] refactor: ChannelManager --- lib/src/api/managers/channel_manager.dart | 24 ++++++++--------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/src/api/managers/channel_manager.dart b/lib/src/api/managers/channel_manager.dart index 0c11edf62..db59f5f24 100644 --- a/lib/src/api/managers/channel_manager.dart +++ b/lib/src/api/managers/channel_manager.dart @@ -4,26 +4,19 @@ import 'package:http/http.dart'; import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; import 'package:mineral/src/api/channels/channel.dart'; -import 'package:mineral/src/api/channels/permission_overwrite.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; import 'package:collection/collection.dart'; -class ChannelManager implements CacheManager { - @override - Map cache = {}; +class ChannelManager extends CacheManager { + late final Guild _guild; + Guild get guild => _guild; - Snowflake? guildId; - late Guild? guild; - - ChannelManager({ required this.guildId }); - - @override Future> sync () async { Http http = ioc.singleton(ioc.services.http); cache.clear(); - Response response = await http.get(url: "/guilds/$guildId/channels"); + Response response = await http.get(url: "/guilds/${_guild.id}/channels"); dynamic payload = jsonDecode(response.body); for(dynamic element in payload) { @@ -72,7 +65,7 @@ class ChannelManager implements CacheManager { Future _create ({ required dynamic data }) async { Http http = ioc.singleton(ioc.services.http); - Response response = await http.post(url: "/guilds/$guildId/channels", payload: data); + Response response = await http.post(url: "/guilds/${_guild.id}/channels", payload: data); dynamic payload = jsonDecode(response.body); final ChannelType? type = ChannelType.values.firstWhereOrNull((element) => element.value == payload['type']); @@ -80,10 +73,9 @@ class ChannelManager implements CacheManager { Channel Function(dynamic payload) item = channels[type] as Channel Function(dynamic payload); Channel channel = item(payload); - // Define deep properties - channel.guildId = guildId; - channel.guild = guild; - channel.parent = channel.parentId != null ? guild?.channels.cache.get(channel.parentId) : null; + channel.parent = payload['parent_id'] != null + ? _guild.channels.cache.get(payload['parent_id']) + : null; cache.putIfAbsent(channel.id, () => channel); return channel as T; From 245f2b0bf3f397176d08b23ad51e7dbe34c48699 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:55:46 +0200 Subject: [PATCH 13/41] refactor: MemberManager --- lib/src/api/managers/member_manager.dart | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/src/api/managers/member_manager.dart b/lib/src/api/managers/member_manager.dart index e013a468f..1c0524825 100644 --- a/lib/src/api/managers/member_manager.dart +++ b/lib/src/api/managers/member_manager.dart @@ -4,22 +4,15 @@ import 'package:http/http.dart'; import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; -import 'package:mineral/src/api/managers/voice_manager.dart'; -class MemberManager implements CacheManager { - @override - Map cache = {}; +class MemberManager extends CacheManager { + late final Guild _guild; + Guild get guild => _guild; - Snowflake guildId; - late Guild guild; - - MemberManager({ required this.guildId }); - - @override Future> sync () async { Http http = ioc.singleton(ioc.services.http); - Response response = await http.get(url: "/guilds/$guildId/members"); + Response response = await http.get(url: "/guilds/${_guild.id}/members"); if(response.statusCode == 200) { dynamic payload = jsonDecode(response.body); final Map voiceStateCache = cache.map((key, value) => MapEntry(key, value.voice)); @@ -28,12 +21,13 @@ class MemberManager implements CacheManager { for(dynamic element in payload) { VoiceManager? voiceManager = voiceStateCache.get(element['user']['id']); + VoiceChannel? voiceChannel = guild.channels.cache.get(payload['channel_id']); GuildMember guildMember = GuildMember.from( user: User.from(element['user']), - roles: guild.roles, - guildId: guild.id, - voice: voiceManager ?? VoiceManager(isMute: element['mute'], isDeaf: element['deaf'], isSelfMute: false, isSelfDeaf: false, hasVideo: false, hasStream: false, channel: null) + roles: _guild.roles, + guildId: _guild.id, + voice: voiceManager ?? VoiceManager.from(payload, guild, voiceChannel) ); cache.putIfAbsent(guildMember.user.id, () => guildMember); From deafd9d2e23fb6f3e6cf510c096be26a70bf57e5 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:55:52 +0200 Subject: [PATCH 14/41] refactor: MemberRoleManager --- lib/src/api/managers/member_role_manager.dart | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/src/api/managers/member_role_manager.dart b/lib/src/api/managers/member_role_manager.dart index e84c5b016..a522438c0 100644 --- a/lib/src/api/managers/member_role_manager.dart +++ b/lib/src/api/managers/member_role_manager.dart @@ -7,16 +7,15 @@ import 'package:mineral/src/api/managers/guild_role_manager.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; import 'package:mineral/src/exceptions/not_exist.dart'; -class MemberRoleManager implements CacheManager { - @override - Map cache = {}; - +class MemberRoleManager extends CacheManager { + late final Guild _guild; GuildRoleManager manager; Snowflake memberId; - //late Guild guild; MemberRoleManager({ required this.manager, required this.memberId }); + Guild get guild => _guild; + /// Add a [Role] to the [GuildMember] /// /// Example : @@ -48,7 +47,7 @@ class MemberRoleManager implements CacheManager { } Response response = await http.put( - url: '/guilds/${manager.guildId}/members/$memberId/roles/$id', + url: '/guilds/${manager.guild.id}/members/$memberId/roles/$id', payload: {}, headers: headers ); @@ -84,7 +83,7 @@ class MemberRoleManager implements CacheManager { } Response response = await http.destroy( - url: '/guilds/${manager.guildId}/members/$memberId/roles/$id', + url: '/guilds/${manager.guild.id}/members/$memberId/roles/$id', headers: headers ); @@ -116,11 +115,10 @@ class MemberRoleManager implements CacheManager { : add(id, reason: reason); } - @override Future> sync () async { Http http = ioc.singleton(ioc.services.http); - Response response = await http.get(url: "/guilds/${manager.guildId}/members/$memberId"); + Response response = await http.get(url: "/guilds/${manager.guild.id}/members/$memberId"); if(response.statusCode == 200) { cache.clear(); dynamic payload = jsonDecode(response.body)['roles']; From a372166e7ef07c68fb2946f676aac9916e6a18bb Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:55:59 +0200 Subject: [PATCH 15/41] refactor: MessageManager --- lib/src/api/managers/message_manager.dart | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/lib/src/api/managers/message_manager.dart b/lib/src/api/managers/message_manager.dart index 423e5cbad..6c8069321 100644 --- a/lib/src/api/managers/message_manager.dart +++ b/lib/src/api/managers/message_manager.dart @@ -6,29 +6,19 @@ import 'package:mineral/core.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; import 'package:mineral/src/api/messages/partial_message.dart'; -class MessageManager implements CacheManager { - @override - Map cache = {}; +class MessageManager extends CacheManager { + late final TextBasedChannel channel; - final Snowflake? _guildId; - final Snowflake _channelId; - - MessageManager(this._channelId, this._guildId); - - @override Future> sync () async { Http http = ioc.singleton(ioc.services.http); cache.clear(); - Response response = await http.get(url: "/channels/$_channelId/messages"); + Response response = await http.get(url: "/channels/${channel.id}/messages"); dynamic payload = jsonDecode(response.body); for(dynamic element in payload) { - MineralClient client = ioc.singleton(ioc.services.client); - Guild? guild = client.guilds.cache.get(_guildId); - TextBasedChannel? channel = guild?.channels.cache.get(_channelId); // @Todo add DM case (guild not exist) - Message message = Message.from(channel: channel!, payload: element); + Message message = Message.from(channel: channel, payload: element); cache.putIfAbsent(message.id, () => message); } From fd2a9685aed56d02da47f88ef61a0b151c4f995f Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:56:12 +0200 Subject: [PATCH 16/41] refactor: ModerationRuleManager --- lib/src/api/managers/moderation_rule_manager.dart | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/src/api/managers/moderation_rule_manager.dart b/lib/src/api/managers/moderation_rule_manager.dart index 94a261f92..f6dcd3076 100644 --- a/lib/src/api/managers/moderation_rule_manager.dart +++ b/lib/src/api/managers/moderation_rule_manager.dart @@ -6,13 +6,8 @@ import 'package:mineral/core.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; class ModerationRuleManager extends CacheManager { - Snowflake guildId; - late Guild guild; - - @override - Map cache = {}; - - ModerationRuleManager({ required this.guildId }); + late final Guild _guild; + Guild get guild => _guild; Future create ({ required String label, required ModerationEventType eventType, required ModerationTriggerType triggerType, ModerationTriggerMetadata? triggerMetadata, List? actions, bool? enabled, List? exemptRoles, List? exemptChannels }) async { Http http = ioc.singleton(ioc.services.http); @@ -21,7 +16,7 @@ class ModerationRuleManager extends CacheManager { * @Todo Add contraints * https://discord.com/developers/docs/resources/auto-moderation#auto-moderation-rule-object-trigger-types */ - Response response = await http.post(url: "/guilds/$guildId/auto-moderation/rules", payload: { + Response response = await http.post(url: "/guilds/${guild.id}/auto-moderation/rules", payload: { 'name': label, 'event_type': eventType.value, 'trigger_type': triggerType.value, From fa9f09ca740ded35105101d13b16c28e1c2ed9cf Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:56:28 +0200 Subject: [PATCH 17/41] refactor: PermissionOverwriteManager --- .../permission_overwrite_manager.dart | 28 +++---------------- 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/lib/src/api/managers/permission_overwrite_manager.dart b/lib/src/api/managers/permission_overwrite_manager.dart index cffd463e5..ece7bf52e 100644 --- a/lib/src/api/managers/permission_overwrite_manager.dart +++ b/lib/src/api/managers/permission_overwrite_manager.dart @@ -1,22 +1,10 @@ -import 'dart:convert'; - -import 'package:http/http.dart'; import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; -import 'package:mineral/src/api/channels/permission_overwrite.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; -class PermissionOverwriteManager implements CacheManager { - @override - Map cache = {}; - - Snowflake? guildId; - Snowflake? channelId; - - PermissionOverwriteManager({required this.guildId, required this.channelId}); +class PermissionOverwriteManager extends CacheManager { + late final Channel channel; //TODO: Need channel sync - @override Future> sync() async { throw UnimplementedError(); } @@ -43,12 +31,8 @@ class PermissionOverwriteManager implements CacheManager { /// await channel.permissionOverwrites.set(overwrites); /// ``` Future set (List permissionOverwrites) async { - final MineralClient client = ioc.singleton(ioc.services.client); - final Guild guild = client.guilds.cache.getOrFail(guildId); - final dynamic channel = guild.channels.cache.getOrFail(channelId); - if(channel is VoiceChannel || channel is TextBasedChannel || channel is CategoryChannel) { - await channel.update(permissionOverwrites: permissionOverwrites); + await (channel as dynamic).update(permissionOverwrites: permissionOverwrites); } } @@ -65,14 +49,10 @@ class PermissionOverwriteManager implements CacheManager { /// )); /// ``` Future add (PermissionOverwrite permissionOverwrite) async { - final MineralClient client = ioc.singleton(ioc.services.client); - final Guild guild = client.guilds.cache.getOrFail(guildId); - final dynamic channel = guild.channels.cache.getOrFail(channelId); - cache.putIfAbsent(permissionOverwrite.id, () => permissionOverwrite); if(channel is VoiceChannel || channel is TextBasedChannel || channel is CategoryChannel) { - await channel.update(permissionOverwrites: cache.values.toList()); + await (channel as dynamic).update(permissionOverwrites: cache.values.toList()); } } } From f887229dda8195bdbd7fadc63fd61dc9452bcb72 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:56:48 +0200 Subject: [PATCH 18/41] refactor: StickerManager --- lib/src/api/managers/sticker_manager.dart | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/src/api/managers/sticker_manager.dart b/lib/src/api/managers/sticker_manager.dart index 12863a5ae..bfb20981b 100644 --- a/lib/src/api/managers/sticker_manager.dart +++ b/lib/src/api/managers/sticker_manager.dart @@ -8,19 +8,14 @@ import 'package:mineral/helper.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; import 'package:mineral/src/api/sticker.dart'; -class StickerManager implements CacheManager { - @override - Map cache = {}; - - Snowflake? guildId; - late Guild? guild; - - StickerManager({ required this.guildId }); +class StickerManager extends CacheManager { + late final Guild _guild; + Guild get guild => _guild; Future create ({ required String name, required String description, required String tags, required String filename }) async { - if (guild!.features.contains(GuildFeature.verified) || guild!.features.contains(GuildFeature.partnered)) { + if (guild.features.contains(GuildFeature.verified) || guild.features.contains(GuildFeature.partnered)) { Http http = ioc.singleton(ioc.services.http); - Response response = await http.post(url: "/guilds/$guildId/stickers", payload: { + Response response = await http.post(url: "/guilds/${guild.id}/stickers", payload: { 'name': name, 'description': description, 'tags': tags, @@ -38,17 +33,16 @@ class StickerManager implements CacheManager { Console.warn( prefix: 'cancelled', - message: "${guild?.name} guild does not have the ${GuildFeature.verified} or ${GuildFeature.partnered} feature." + message: "${guild.name} guild does not have the ${GuildFeature.verified} or ${GuildFeature.partnered} feature." ); return null; } - @override Future> sync () async { Http http = ioc.singleton(ioc.services.http); cache.clear(); - Response response = await http.get(url: "/guilds/$guildId/stickers"); + Response response = await http.get(url: "/guilds/${guild.id}/stickers"); dynamic payload = jsonDecode(response.body); for(dynamic element in payload) { From 3d8c3ffad64bf88ccee50eafbeed404359a34234 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:56:59 +0200 Subject: [PATCH 19/41] refactor: ThreadManager --- lib/src/api/managers/thread_manager.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/api/managers/thread_manager.dart b/lib/src/api/managers/thread_manager.dart index 178640794..4f1e4e0b2 100644 --- a/lib/src/api/managers/thread_manager.dart +++ b/lib/src/api/managers/thread_manager.dart @@ -9,7 +9,7 @@ import 'package:mineral/core.dart'; import '../channels/public_thread.dart'; -class ThreadManager implements CacheManager { +class ThreadManager extends CacheManager { @override Map cache = {}; From 80bda19a8dd6a899d9c02a03590969aa3cfafa79 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:57:05 +0200 Subject: [PATCH 20/41] refactor: UserManager --- lib/src/api/managers/user_manager.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/api/managers/user_manager.dart b/lib/src/api/managers/user_manager.dart index 222ccf179..883219854 100644 --- a/lib/src/api/managers/user_manager.dart +++ b/lib/src/api/managers/user_manager.dart @@ -1,7 +1,7 @@ import 'package:mineral/api.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; -class UserManager implements CacheManager { +class UserManager extends CacheManager { @override Map cache = {}; From 25cb6da13a1206ad9d08d0f681101e9e55279496 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:57:12 +0200 Subject: [PATCH 21/41] refactor: VoiceManager --- lib/src/api/managers/voice_manager.dart | 60 ++++++++++++------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/src/api/managers/voice_manager.dart b/lib/src/api/managers/voice_manager.dart index a71ff1d78..abc603319 100644 --- a/lib/src/api/managers/voice_manager.dart +++ b/lib/src/api/managers/voice_manager.dart @@ -3,24 +3,25 @@ import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; class VoiceManager { - GuildMember? member; - bool isDeaf; - bool isMute; - bool isSelfMute; - bool isSelfDeaf; - bool hasVideo; - bool? hasStream; - VoiceChannel? channel; + bool _isDeaf; + bool _isMute; + bool _isSelfMute; + bool _isSelfDeaf; + bool _hasVideo; + bool? _hasStream; + VoiceChannel? _channel; + GuildMember? _member; - VoiceManager({ - required this.isMute, - required this.isDeaf, - required this.isSelfMute, - required this.isSelfDeaf, - required this.hasVideo, - required this.hasStream, - required this.channel - }); + VoiceManager( this._isDeaf, this._isMute, this._isSelfMute, this._isSelfDeaf, this._hasVideo, this._hasStream, this._channel, this._member); + + bool get isDeaf => _isDeaf; + bool get isMute => _isMute; + bool get isSelfMute => _isSelfMute; + bool get isSelfDeaf => _isSelfDeaf; + bool get hasVideo => _hasVideo; + bool? get hasStream => _hasStream; + VoiceChannel? get channel => _channel; + GuildMember? get member => _member; /// ### Mutes or unmute a server member /// @@ -40,7 +41,7 @@ class VoiceManager { ); if (response.statusCode == 204 || response.statusCode == 200) { - isMute = value; + _isMute = value; } } @@ -61,7 +62,7 @@ class VoiceManager { ); if (response.statusCode == 204 || response.statusCode == 200) { - isDeaf = value; + _isDeaf = value; } } @@ -102,22 +103,21 @@ class VoiceManager { if (response.statusCode == 204 || response.statusCode == 200) { final VoiceChannel? channel = member!.guild.channels.cache.get(channelId); if (channel != null) { - this.channel = channel; + _channel = channel; } } } - factory VoiceManager.from(dynamic payload, VoiceChannel? channel) { + factory VoiceManager.from(dynamic payload, Guild guild, VoiceChannel? channel) { return VoiceManager( - isMute: payload['mute'], - isDeaf: payload['deaf'], - isSelfMute: payload['self_mute'], - isSelfDeaf: payload['self_deaf'], - hasVideo: payload['self_video'], - hasStream: payload['self_stream'], - channel: channel + payload['deaf'] == true, + payload['mute'] == true, + payload['self_mute'] == true, + payload['self_deaf'] == true, + payload['self_video'] == true, + payload['self_stream'] == true, + channel, + guild.members.cache.get(payload['user']?['id']), ); } - - } From 701b5d6592c572d8e8eaaa7743e19278b888f050 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:57:20 +0200 Subject: [PATCH 22/41] refactor: WebhookManager --- lib/src/api/managers/webhook_manager.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/api/managers/webhook_manager.dart b/lib/src/api/managers/webhook_manager.dart index aa371d426..c95cf14a1 100644 --- a/lib/src/api/managers/webhook_manager.dart +++ b/lib/src/api/managers/webhook_manager.dart @@ -6,7 +6,7 @@ import 'package:mineral/core.dart'; import 'package:mineral/helper.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; -class WebhookManager implements CacheManager { +class WebhookManager extends CacheManager { @override Map cache = {}; From dc95c7e4b8c67320726c9aa30db35f91c629832b Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:57:28 +0200 Subject: [PATCH 23/41] refactor: CategoryChannel --- lib/src/api/channels/category_channel.dart | 66 ++++++++++------------ 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/lib/src/api/channels/category_channel.dart b/lib/src/api/channels/category_channel.dart index 7aa0ef94e..fbdab734f 100644 --- a/lib/src/api/channels/category_channel.dart +++ b/lib/src/api/channels/category_channel.dart @@ -3,30 +3,19 @@ import 'dart:convert'; import 'package:http/http.dart'; import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; -import 'package:mineral/src/api/channels/permission_overwrite.dart'; import 'package:mineral/src/api/managers/permission_overwrite_manager.dart'; -import 'package:mineral/src/api/managers/webhook_manager.dart'; class CategoryChannel extends Channel { - CategoryChannel({ - required Snowflake id, - required Snowflake? guildId, - required int? position, - required String label, - required Snowflake? applicationId, - required int? flags, - required PermissionOverwriteManager permissionOverwrites - }) : super( - id: id, - guildId: guildId, - position: position, - label: label, - applicationId: applicationId, - parentId: null, - type: ChannelType.guildCategory, - flags: flags, - webhooks: WebhookManager(guildId: guildId, channelId: id), - permissionOverwrites: permissionOverwrites + CategoryChannel( + super._id, + super._type, + super.position, + super.label, + super.applicationId, + super.flags, + super._webhooks, + super.permissionOverwrites, + super._guild ); Future update ({ String? label, int? position, List? permissionOverwrites }) async { @@ -42,9 +31,9 @@ class CategoryChannel extends Channel { CategoryChannel channel = CategoryChannel.from(payload); // Define deep properties - channel.guildId = guildId; - channel.guild = guild; - channel.parent = channel.parentId != null ? guild?.channels.cache.get(channel.parentId) : null; + channel.parent = channel.parent != null + ? guild?.channels.cache.get(payload['parent_id']) + : null; guild?.channels.cache.set(channel.id, channel); return channel; @@ -60,23 +49,28 @@ class CategoryChannel extends Channel { } factory CategoryChannel.from(dynamic payload) { - final PermissionOverwriteManager permissionOverwriteManager = PermissionOverwriteManager( - guildId: payload['guild_id'], - channelId: payload['id'] - ); + MineralClient client = ioc.singleton(ioc.services.client); + final permissionOverwriteManager = PermissionOverwriteManager(); + for(dynamic element in payload['permission_overwrites']) { final PermissionOverwrite overwrite = PermissionOverwrite.from(payload: element); permissionOverwriteManager.cache.putIfAbsent(overwrite.id, () => overwrite); } - return CategoryChannel( - id: payload['id'], - guildId: payload['guild_id'], - position: payload['position'], - label: payload['name'], - applicationId: payload['application_id'], - flags: payload['flags'], - permissionOverwrites: permissionOverwriteManager + final category = CategoryChannel( + payload['id'], + ChannelType.guildCategory, + payload['position'], + payload['name'], + payload['application_id'], + payload['flags'], + null, + permissionOverwriteManager, + client.guilds.cache.get(payload['guild_id']) ); + + category.permissionOverwrites?.channel = category; + + return category; } } From a884e5fe4f3c40a3eab7207145d32be81a7536e4 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:57:34 +0200 Subject: [PATCH 24/41] refactor: Channel --- lib/src/api/channels/channel.dart | 60 +++++++++++++++++-------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/lib/src/api/channels/channel.dart b/lib/src/api/channels/channel.dart index 2873c6ba2..714519339 100644 --- a/lib/src/api/channels/channel.dart +++ b/lib/src/api/channels/channel.dart @@ -39,37 +39,44 @@ Map channels = { }; class Channel extends PartialChannel { - ChannelType type; - Snowflake? guildId; - late Guild? guild; - int? position; - String? label; - Snowflake? applicationId; - Snowflake? parentId; - late CategoryChannel? parent; - int? flags; - WebhookManager webhooks; - PermissionOverwriteManager? permissionOverwrites; - - Channel({ - required id, - required this.type, - required this.guildId, - required this.position, - required this.label, - required this.applicationId, - required this.parentId, - required this.flags, - required this.webhooks, - this.permissionOverwrites - }): super(id: id); + final Guild? _guild; + late Channel? parent; + + final ChannelType _type; + int? _position; + String? _label; + final Snowflake? _applicationId; + final int? _flags; + final WebhookManager? _webhooks; + final PermissionOverwriteManager? _permissionOverwrites; + + Channel( + super.id, + this._type, + this._position, + this._label, + this._applicationId, + this._flags, + this._webhooks, + this._permissionOverwrites, + this._guild, + ); + + ChannelType get type => _type; + Guild? get guild => _guild; + int? get position => _position; + String? get label => _label; + Snowflake? get applicationId => _applicationId; + int? get flags => _flags; + WebhookManager? get webhooks => _webhooks; + PermissionOverwriteManager? get permissionOverwrites => _permissionOverwrites; Future setLabel (String label) async { Http http = ioc.singleton(ioc.services.http); Response response = await http.patch(url: "/channels/$id", payload: { 'label': label }); if (response.statusCode == 200) { - this.label = label; + _label = label; } return this as T; @@ -80,7 +87,7 @@ class Channel extends PartialChannel { Response response = await http.patch(url: "/channels/$id", payload: { 'position': position }); if (response.statusCode == 200) { - this.position = position; + _position = position; } return this as T; @@ -91,7 +98,6 @@ class Channel extends PartialChannel { Response response = await http.patch(url: "/channels/$id", payload: { 'parent_id': channel.id }); if (response.statusCode == 200) { - parentId = channel.id; parent = channel; } From cab4a55c19d1ae51ece4f8d34ebce5196bfc6852 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:57:40 +0200 Subject: [PATCH 25/41] refactor: DmChannel --- lib/src/api/channels/dm_channel.dart | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/src/api/channels/dm_channel.dart b/lib/src/api/channels/dm_channel.dart index bc186131e..f1ea58a2e 100644 --- a/lib/src/api/channels/dm_channel.dart +++ b/lib/src/api/channels/dm_channel.dart @@ -8,12 +8,12 @@ class DmChannel extends PartialChannel { MessageManager messages; Map recipients; - DmChannel({ - required id, - required this.lastMessageId, - required this.messages, - required this.recipients, - }): super(id: id); + DmChannel( + super.id, + this.lastMessageId, + this.messages, + this.recipients, + ); factory DmChannel.from({ required dynamic payload }) { MineralClient client = ioc.singleton(ioc.services.client); @@ -29,10 +29,10 @@ class DmChannel extends PartialChannel { } return DmChannel( - id: payload['id'], - lastMessageId: payload['last_message_id'], - messages: MessageManager(payload['id'], null), - recipients: users + payload['id'], + payload['last_message_id'], + MessageManager(), + users, ); } } From 5574b1f2bcf3ea87c96fee739f5262489e86fc2a Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:57:48 +0200 Subject: [PATCH 26/41] refactor: PartialChannel --- lib/src/api/channels/partial_channel.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/src/api/channels/partial_channel.dart b/lib/src/api/channels/partial_channel.dart index a76088b16..49db6fc55 100644 --- a/lib/src/api/channels/partial_channel.dart +++ b/lib/src/api/channels/partial_channel.dart @@ -2,6 +2,5 @@ import 'package:mineral/api.dart'; class PartialChannel { Snowflake id; - - PartialChannel({ required this.id }); + PartialChannel(this.id); } From 023d227fd0200e65b53c9db310cf73d2655cc447 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:57:56 +0200 Subject: [PATCH 27/41] refactor: PublicChannel --- lib/src/api/channels/public_thread.dart | 72 ++++++++++++++----------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/lib/src/api/channels/public_thread.dart b/lib/src/api/channels/public_thread.dart index 59a2d2cf7..5c7ba7075 100644 --- a/lib/src/api/channels/public_thread.dart +++ b/lib/src/api/channels/public_thread.dart @@ -1,34 +1,33 @@ import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; -import 'package:mineral/src/api/managers/webhook_manager.dart'; + class PublicThread extends Channel { + Channel? _parent; + Snowflake? _lastMessageId; + DateTime? _lastPinTimestamp; - String? name; - Snowflake? lastMessageId; - DateTime? lastPinTimestamp; - - PublicThread({ - required Snowflake id, - required Snowflake? guildId, - required int? position, - required String label, - required Snowflake? applicationId, - required Snowflake? parentId, - required int? flags, - required WebhookManager webhooks, - }) : super( - id: id, - type: ChannelType.guildPublicThread, - guildId: guildId, - position: position, - label: label, - applicationId: applicationId, - parentId: parentId, - flags: flags, - webhooks: webhooks, + PublicThread( + super._id, + super._type, + super._position, + super._label, + super._applicationId, + super._flags, + super._webhooks, + super._permissionOverwrites, + super._guild, + this._parent, + this._lastMessageId, + this._lastPinTimestamp, ); + @override + Channel? get parent => _parent; + + Snowflake? get lastMessageId => _lastMessageId; + DateTime? get lastPinTimestamp => _lastPinTimestamp; + Future create (Snowflake message, String name) async { Http http = ioc.singleton(ioc.services.http); await http.post(url: '/channels/$id/messages/$message/threads', payload: { @@ -37,15 +36,24 @@ class PublicThread extends Channel { } factory PublicThread.from({ required dynamic payload }) { + MineralClient client = ioc.singleton(ioc.services.client); + + Guild? guild = client.guilds.cache.get(payload['guild_id']); + Channel? parent = guild?.channels.cache.get(payload['parent_id']); + return PublicThread( - id: payload['id'], - guildId: payload['guild_id'], - position: payload['position'], - label: payload['name'], - applicationId: payload['application_id'], - parentId: payload['parent_id'], - flags: payload['flags'], - webhooks: payload['webhooks'], + payload['id'], + ChannelType.guildPublicThread, + payload['position'], + payload['name'], + payload['application_id'], + payload['flags'], + payload['webhooks'], + null, + guild, + parent, + payload['last_message_id'], + payload['last_pin_message'], ); } } From 220fe73113e18a56fe4ed9d9edd7608b006c657b Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:58:08 +0200 Subject: [PATCH 28/41] refactor: TextBasedChannel --- lib/src/api/channels/text_based_channel.dart | 81 ++++++++++---------- 1 file changed, 39 insertions(+), 42 deletions(-) diff --git a/lib/src/api/channels/text_based_channel.dart b/lib/src/api/channels/text_based_channel.dart index 725199592..4df325cb9 100644 --- a/lib/src/api/channels/text_based_channel.dart +++ b/lib/src/api/channels/text_based_channel.dart @@ -3,50 +3,48 @@ import 'dart:convert'; import 'package:http/http.dart'; import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; -import 'package:mineral/src/api/channels/permission_overwrite.dart'; import 'package:mineral/src/api/managers/message_manager.dart'; -import 'package:mineral/src/api/managers/permission_overwrite_manager.dart'; -import 'package:mineral/src/api/managers/webhook_manager.dart'; import 'package:mineral/src/api/managers/thread_manager.dart'; import 'package:mineral/src/internal/extensions/mineral_client.dart'; class TextBasedChannel extends Channel { - String? description; - bool nsfw; - Snowflake? lastMessageId; - DateTime? lastPinTimestamp; - MessageManager messages; - ThreadManager threads; - - TextBasedChannel({ - required Snowflake id, - required Snowflake? guildId, - required int? position, - required String label, - required Snowflake? applicationId, - required Snowflake? parentId, - required int? flags, - required this.description, - required this.nsfw, - required this.lastMessageId, - required this.lastPinTimestamp, - required this.messages, - required this.threads, - required PermissionOverwriteManager permissionOverwrites - }) : super( - id: id, - type: ChannelType.guildText, - guildId: guildId, - position: position, - label: label, - applicationId: applicationId, - parentId: parentId, - flags: flags, - webhooks: WebhookManager(guildId: guildId, channelId: id), - permissionOverwrites: permissionOverwrites + Channel? _parent; + String? _description; + bool _nsfw; + final Snowflake? _lastMessageId; + final DateTime? _lastPinTimestamp; + final MessageManager _messages; + final ThreadManager _threads; + + TextBasedChannel( + super._id, + super._type, + super._position, + super.label, + super._applicationId, + super.flags, + super._webhooks, + super._permissionOverwrites, + super._guild, + this._description, + this._nsfw, + this._lastMessageId, + this._lastPinTimestamp, + this._messages, + this._threads, ); + @override + Channel? get parent => _parent; + + String? get description => _description; + bool get isNsfw => _nsfw; + Snowflake? get lastMessageId => _lastMessageId; + DateTime? get lastPinTimestamp => _lastPinTimestamp; + MessageManager get messages => _messages; + ThreadManager get threads => _threads; + Future send ({ String? content, List? embeds, List? components, bool? tts }) async { MineralClient client = ioc.singleton(ioc.services.client); @@ -75,7 +73,7 @@ class TextBasedChannel extends Channel { Response response = await http.patch(url: "/channels/$id", payload: { 'topic': description }); if (response.statusCode == 200) { - this.description = description; + _description = description; } return this; @@ -86,7 +84,7 @@ class TextBasedChannel extends Channel { Response response = await http.patch(url: "/channels/$id", payload: { 'nsfw': value }); if (response.statusCode == 200) { - nsfw = value; + _nsfw = value; } return this; @@ -107,10 +105,9 @@ class TextBasedChannel extends Channel { dynamic payload = jsonDecode(response.body); TextChannel channel = TextChannel.from(payload); - // Define deep properties - channel.guildId = guildId; - channel.guild = guild; - channel.parent = channel.parentId != null ? guild?.channels.cache.get(channel.parentId) : null; + channel.parent = payload['parent_id'] != null + ? guild?.channels.cache.get(payload['parent_id']) + : null; guild?.channels.cache.set(channel.id, channel); return channel; From 27cd105bf1997a379158b90bff9e862e1abc8201 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:58:15 +0200 Subject: [PATCH 29/41] refactor: TextChannel --- lib/src/api/channels/text_channel.dart | 84 ++++++++++++-------------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/lib/src/api/channels/text_channel.dart b/lib/src/api/channels/text_channel.dart index 3e5b7421b..ebe659e9c 100644 --- a/lib/src/api/channels/text_channel.dart +++ b/lib/src/api/channels/text_channel.dart @@ -1,38 +1,28 @@ import 'package:mineral/api.dart'; -import 'package:mineral/src/api/channels/permission_overwrite.dart'; +import 'package:mineral/core.dart'; import 'package:mineral/src/api/managers/message_manager.dart'; + import 'package:mineral/src/api/managers/permission_overwrite_manager.dart'; import 'package:mineral/src/api/managers/thread_manager.dart'; +import 'package:mineral/src/api/managers/webhook_manager.dart'; class TextChannel extends TextBasedChannel { - TextChannel({ - required Snowflake id, - required Snowflake? guildId, - required int? position, - required String label, - required Snowflake? applicationId, - required Snowflake? parentId, - required int? flags, - required Snowflake? description, - required bool nsfw, - required Snowflake? lastMessageId, - required DateTime? lastPinTimestamp, - required PermissionOverwriteManager permissionOverwrites - }) : super( - id: id, - guildId: guildId, - position: position, - label: label, - applicationId: applicationId, - parentId: parentId, - flags: flags, - description: description, - nsfw: nsfw, - lastMessageId: lastMessageId, - lastPinTimestamp: lastPinTimestamp, - messages: MessageManager(id, guildId), - threads: ThreadManager(guildId: guildId), - permissionOverwrites: permissionOverwrites + TextChannel( + super.id, + super._type, + super._position, + super._label, + super._applicationId, + super._flags, + super._webhooks, + super.permissionOverwrites, + super._guild, + super.description, + super.nsfw, + super.lastMessageId, + super.lastPinTimestamp, + super._messages, + super._threads, ); @override @@ -51,29 +41,33 @@ class TextChannel extends TextBasedChannel { } factory TextChannel.from(dynamic payload) { - final PermissionOverwriteManager permissionOverwriteManager = PermissionOverwriteManager( - guildId: payload['guild_id'], - channelId: payload['id'] - ); + MineralClient client = ioc.singleton(ioc.services.client); + Guild? guild = client.guilds.cache.get(payload['guild_id']); + + final PermissionOverwriteManager permissionOverwriteManager = PermissionOverwriteManager(); for(dynamic element in payload['permission_overwrites']) { final PermissionOverwrite overwrite = PermissionOverwrite.from(payload: element); permissionOverwriteManager.cache.putIfAbsent(overwrite.id, () => overwrite); } TextChannel channel = TextChannel( - id: payload['id'], - guildId: payload['guild_id'], - position: payload['position'], - label: payload['name'], - applicationId: payload['application_id'], - parentId: payload['parent_id'], - flags: payload['flags'], - description: payload['topic'], - nsfw: payload['nsfw'] ?? false, - lastMessageId: payload['last_message_id'], - lastPinTimestamp: payload['last_pin_timestamp'] != null ? DateTime.parse(payload['last_pin_timestamp']) : null, - permissionOverwrites: permissionOverwriteManager + payload['id'], + ChannelType.guildText, + payload['position'], + payload['name'], + payload['application_id'], + payload['flags'], + WebhookManager(guildId: guild?.id), + permissionOverwriteManager, + guild, + payload['topic'], + payload['nsfw'] ?? false, + payload['last_message_id'], + payload['last_pin_timestamp'] != null ? DateTime.parse(payload['last_pin_timestamp']) : null, + MessageManager(), + ThreadManager(guildId: guild?.id), ); + channel.threads.channel = channel; return channel; From 40a871a42799a029f861917d877ef6f2ad2e766e Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 11:58:26 +0200 Subject: [PATCH 30/41] refactor: VoiceChannel --- lib/src/api/channels/voice_channel.dart | 99 ++++++++++++------------- 1 file changed, 47 insertions(+), 52 deletions(-) diff --git a/lib/src/api/channels/voice_channel.dart b/lib/src/api/channels/voice_channel.dart index cfb950667..938fa60a8 100644 --- a/lib/src/api/channels/voice_channel.dart +++ b/lib/src/api/channels/voice_channel.dart @@ -3,44 +3,38 @@ import 'dart:convert'; import 'package:http/http.dart'; import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; -import 'package:mineral/src/api/channels/permission_overwrite.dart'; import 'package:mineral/src/api/managers/permission_overwrite_manager.dart'; -import 'package:mineral/src/api/managers/webhook_manager.dart'; class VoiceChannel extends Channel { - int? bitrate; - int? userLimit; - int? rateLimitPerUser; - String? rtcRegion; - int? videoQualityMode; + int? _bitrate; + int? _userLimit; + int? _rateLimitPerUser; + String? _rtcRegion; + int? _videoQualityMode; - VoiceChannel({ - required Snowflake id, - required Snowflake? guildId, - required int? position, - required String label, - required Snowflake? applicationId, - required Snowflake? parentId, - required int? flags, - required this.bitrate, - required this.userLimit, - required this.rateLimitPerUser, - required this.rtcRegion, - required this.videoQualityMode, - required PermissionOverwriteManager permissionOverwrites - }) : super( - id: id, - type: ChannelType.guildVoice, - guildId: guildId, - position: position, - label: label, - applicationId: applicationId, - parentId: parentId, - flags: flags, - webhooks: WebhookManager(guildId: guildId, channelId: id), - permissionOverwrites: permissionOverwrites + VoiceChannel( + super.id, + super._type, + super._position, + super._label, + super._applicationId, + super._flags, + super._webhooks, + super._permissionOverwrites, + super._guild, + this._bitrate, + this._userLimit, + this._rateLimitPerUser, + this._rtcRegion, + this._videoQualityMode, ); + int? get bitrate => _bitrate; + int? get userLimit => _userLimit; + int? get rateLimitPerUser => _rateLimitPerUser; + String? get rtcRegion => _rtcRegion; + int? get videoQualityMode => _videoQualityMode; + Future update ({ String? label, String? description, int? delay, int? position, CategoryChannel? categoryChannel, bool? nsfw, List? permissionOverwrites }) async { Http http = ioc.singleton(ioc.services.http); @@ -58,38 +52,39 @@ class VoiceChannel extends Channel { VoiceChannel channel = VoiceChannel.from(payload); // Define deep properties - channel.guildId = guildId; - channel.guild = guild; - channel.parent = channel.parentId != null ? guild?.channels.cache.get(channel.parentId) : null; + channel.parent = payload['parent_id'] != null + ? guild?.channels.cache.get(payload['parent_id']) + : null; guild?.channels.cache.set(channel.id, channel); return channel; } factory VoiceChannel.from(dynamic payload) { - final PermissionOverwriteManager permissionOverwriteManager = PermissionOverwriteManager( - guildId: payload['guild_id'], - channelId: payload['id'] - ); + MineralClient client = ioc.singleton(ioc.services.client); + Guild? guild = client.guilds.cache.get(payload['guild_id']); + + final PermissionOverwriteManager permissionOverwriteManager = PermissionOverwriteManager(); for(dynamic element in payload['permission_overwrites']) { final PermissionOverwrite overwrite = PermissionOverwrite.from(payload: element); permissionOverwriteManager.cache.putIfAbsent(overwrite.id, () => overwrite); } return VoiceChannel( - id: payload['id'], - guildId: payload['guild_id'], - position: payload['position'], - label: payload['name'], - applicationId: payload['application_id'], - parentId: payload['parent_id'], - flags: payload['flags'], - bitrate: payload['bitrate'], - userLimit: payload['user_limit'] ?? false, - rateLimitPerUser: payload['rate_limit_per_user'], - rtcRegion: payload['rtc_region'] , - videoQualityMode: payload['video_quality_mode'], - permissionOverwrites: permissionOverwriteManager + payload['id'], + ChannelType.guildVoice, + payload['position'], + payload['name'], + payload['application_id'], + payload['flags'], + null, + permissionOverwriteManager, + guild, + payload['bitrate'], + payload['user_limit'] ?? false, + payload['rate_limit_per_user'], + payload['rtc_region'] , + payload['video_quality_mode'], ); } } From 3df7331fc257c69098a3228b59aa6f34d7eb98a0 Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 12:03:24 +0200 Subject: [PATCH 31/41] refactor: MineralClient --- lib/src/api/client/mineral_client.dart | 60 +++++++++++++++----------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/lib/src/api/client/mineral_client.dart b/lib/src/api/client/mineral_client.dart index 3fa38f006..3231b83c5 100644 --- a/lib/src/api/client/mineral_client.dart +++ b/lib/src/api/client/mineral_client.dart @@ -71,23 +71,31 @@ class ClientActivity { } class MineralClient { - User user; - GuildManager guilds; - DmChannelManager dmChannels; - UserManager users; - String sessionId; - Application application; - List intents; - - MineralClient({ - required this.user, - required this.guilds, - required this.dmChannels, - required this.users, - required this.sessionId, - required this.application, - required this.intents, - }); + User _user; + GuildManager _guilds; + DmChannelManager _dmChannels; + UserManager _users; + String _sessionId; + Application _application; + List _intents; + + MineralClient( + this._user, + this._guilds, + this._dmChannels, + this._users, + this._sessionId, + this._application, + this._intents, + ); + + User get user => _user; + GuildManager get guilds => _guilds; + DmChannelManager get dmChannels => _dmChannels; + UserManager get users => _users; + String get sessionId => _sessionId; + Application get application => _application; + List get intents => _intents; /// ### Defines the presence that this should adopt /// @@ -124,7 +132,7 @@ class MineralClient { Http http = ioc.singleton(ioc.services.http); await http.put( - url: "/applications/${application.id}/commands", + url: "/applications/${_application.id}/commands", payload: commands.map((command) => command.toJson()).toList() ); } @@ -139,7 +147,7 @@ class MineralClient { ]); await http.put( - url: "/applications/${application.id}/guilds/${guild.id}/commands", + url: "/applications/${_application.id}/guilds/${guild.id}/commands", payload: [ ...commands.map((command) => command.toJson()).toList(), ...contextMenus.map((contextMenus) => contextMenus.toJson()).toList() @@ -151,13 +159,13 @@ class MineralClient { ShardManager manager = ioc.singleton(ioc.services.shards); return MineralClient( - user: User.from(payload['user']), - guilds: GuildManager(), - users: UserManager(), - sessionId: payload['session_id'], - application: Application.from(payload['application']), - intents: manager.intents, - dmChannels: DmChannelManager() + User.from(payload['user']), + GuildManager(), + DmChannelManager(), + UserManager(), + payload['session_id'], + Application.from(payload['application']), + manager.intents, ); } } From 680c9a9c2186ed333ca71ddfa59618a4b644826d Mon Sep 17 00:00:00 2001 From: Freeze455 Date: Tue, 26 Jul 2022 12:09:57 +0200 Subject: [PATCH 32/41] refactor: Activity --- lib/src/api/activity.dart | 70 +++++++++++++++++++---------------- lib/src/api/guilds/guild.dart | 2 +- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/lib/src/api/activity.dart b/lib/src/api/activity.dart index 3f712283a..d521eebc6 100644 --- a/lib/src/api/activity.dart +++ b/lib/src/api/activity.dart @@ -1,48 +1,56 @@ -import 'dart:convert'; - import 'package:mineral/api.dart'; import 'package:mineral/src/api/client/client_presence.dart'; class Activity { - String? label; - GamePresence type; - String? url; - DateTime createdAt; - Timestamp? timestamps; - Snowflake? applicationId; - String? details; - String? state; + String? _label; + GamePresence _type; + String? _url; + DateTime _createdAt; + Timestamp? _timestamps; + Snowflake? _applicationId; + String? _details; + String? _state; // Emoji? emoji; // Party? party; // Asset? assets; // Secret? secrets; // Instance? instance; - int? flags; + int? _flags; // List