Skip to content

Commit

Permalink
refactor: Work in packets
Browse files Browse the repository at this point in the history
  • Loading branch information
LeadcodeDev committed Jul 26, 2022
1 parent b0db18d commit 0313f85
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 105 deletions.
3 changes: 2 additions & 1 deletion lib/src/api/channels/text_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class TextChannel extends TextBasedChannel {
payload['name'],
payload['application_id'],
payload['flags'],
WebhookManager(guildId: guild?.id),
WebhookManager(),
permissionOverwriteManager,
guild,
payload['topic'],
Expand All @@ -64,6 +64,7 @@ class TextChannel extends TextBasedChannel {
ThreadManager(guildId: guild?.id),
);

channel.webhooks?.channel = channel;
channel.threads.channel = channel;

return channel;
Expand Down
3 changes: 1 addition & 2 deletions lib/src/api/managers/moderation_rule_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import 'package:mineral/core.dart';
import 'package:mineral/src/api/managers/cache_manager.dart';

class ModerationRuleManager extends CacheManager<ModerationRule> {
late final Guild _guild;
Guild get guild => _guild;
late final Guild guild;

Future<ModerationRule?> create ({ required String label, required ModerationEventType eventType, required ModerationTriggerType triggerType, ModerationTriggerMetadata? triggerMetadata, List<ModerationAction>? actions, bool? enabled, List<Snowflake>? exemptRoles, List<Snowflake>? exemptChannels }) async {
Http http = ioc.singleton(ioc.services.http);
Expand Down
3 changes: 1 addition & 2 deletions lib/src/api/managers/sticker_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import 'package:mineral/src/api/managers/cache_manager.dart';
import 'package:mineral/src/api/sticker.dart';

class StickerManager extends CacheManager<Sticker> {
late final Guild _guild;
Guild get guild => _guild;
late final Guild guild;

Future<Sticker?> 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)) {
Expand Down
7 changes: 3 additions & 4 deletions lib/src/api/managers/voice_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ class VoiceManager {
bool _isSelfDeaf;
bool _hasVideo;
bool? _hasStream;
VoiceChannel? _channel;
VoiceChannel? channel;
GuildMember? member;

VoiceManager( this._isDeaf, this._isMute, this._isSelfMute, this._isSelfDeaf, this._hasVideo, this._hasStream, this._channel, this.member);
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;

/// ### Mutes or unmute a server member
///
Expand Down Expand Up @@ -102,7 +101,7 @@ class VoiceManager {
if (response.statusCode == 204 || response.statusCode == 200) {
final VoiceChannel? channel = member!.guild.channels.cache.get(channelId);
if (channel != null) {
_channel = channel;
this.channel = channel;
}
}
}
Expand Down
15 changes: 5 additions & 10 deletions lib/src/api/managers/webhook_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@ import 'package:mineral/helper.dart';
import 'package:mineral/src/api/managers/cache_manager.dart';

class WebhookManager extends CacheManager<Webhook> {
@override
Map<Snowflake, Webhook> cache = {};
late final Channel? channel;
late final Guild? guild;

Snowflake? channelId;
Snowflake? guildId;
late Guild? guild;
WebhookManager();

WebhookManager({ this.channelId, required this.guildId });

@override
Future<Map<Snowflake, Webhook>> sync () async {
Http http = ioc.singleton(ioc.services.http);
Response response = await http.get(url: "/channels/$channelId/webhooks");
Response response = await http.get(url: "/channels/${channel?.id}/webhooks");

for (dynamic element in jsonDecode(response.body)) {
Webhook webhook = Webhook.from(payload: element);
Expand All @@ -31,7 +26,7 @@ class WebhookManager extends CacheManager<Webhook> {

Future<Webhook?> create ({ required String label, String? avatar }) async {
Http http = ioc.singleton(ioc.services.http);
Response response = await http.post(url: "/channels/$channelId/webhooks", payload: {
Response response = await http.post(url: "/channels/${channel?.id}/webhooks", payload: {
'name': label,
'avatar': avatar != null ? await Helper.getPicture(avatar) : null
});
Expand Down
17 changes: 8 additions & 9 deletions lib/src/api/sticker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class Sticker {
String _tags;
StickerType _type;
FormatType _format;
Guild _guild;
GuildMember? _guildMember;
int? _sortValue;
GuildMember? member;
late StickerManager manager;
late Guild guild;

Sticker(
this._id,
Expand All @@ -49,8 +49,6 @@ class Sticker {
this._tags,
this._type,
this._format,
this._guild,
this._guildMember,
this._sortValue,
);

Expand All @@ -61,8 +59,6 @@ class Sticker {
String get tags => _tags;
StickerType get type => _type;
FormatType get format => _format;
Guild get guild => _guild;
GuildMember? get guildMember => _guildMember;
int? get sortValue => _sortValue;

Future<void> setName (String name) async {
Expand Down Expand Up @@ -107,17 +103,20 @@ class Sticker {
Guild guild = client.guilds.cache.getOrFail(payload['guild_id']);
GuildMember? member = guild.members.cache.get(payload['user']?['id']);

return Sticker(
final sticker = Sticker(
payload['id'],
payload['pack_id'],
payload['name'],
payload['description'],
payload['tags'],
StickerType.values.firstWhere((element) => element.value == payload['type']),
payload['format_type'],
guild,
member,
payload['sortValue']
);

sticker.guild = guild;
sticker.member = member;

return sticker;
}
}
1 change: 1 addition & 0 deletions lib/src/internal/websockets/packets/channel_create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ChannelCreate implements WebsocketPacket {
: null;

channel?.webhooks?.guild = guild;
channel?.webhooks?.channel = channel;

guild?.channels.cache.putIfAbsent(channel!.id, () => channel!);
}
Expand Down
26 changes: 9 additions & 17 deletions lib/src/internal/websockets/packets/guild_create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:mineral/src/api/managers/emoji_manager.dart';
import 'package:mineral/src/api/managers/guild_scheduled_event_manager.dart';
import 'package:mineral/src/api/managers/member_manager.dart';
import 'package:mineral/src/api/managers/moderation_rule_manager.dart';
import 'package:mineral/src/api/managers/voice_manager.dart';
import 'package:mineral/src/api/managers/webhook_manager.dart';
import 'package:mineral/src/internal/managers/command_manager.dart';
import 'package:mineral/src/internal/managers/context_menu_manager.dart';
Expand All @@ -37,10 +36,10 @@ class GuildCreate implements WebsocketPacket {

Map<Snowflake, VoiceManager> voices = {};
for(dynamic voiceMember in websocketResponse.payload['voice_states']) {
print(voiceMember);
// final VoiceManager voiceManager = VoiceManager.from(voiceMember, null);
// voices.putIfAbsent(voiceMember['user_id'], () => voiceManager);
// voices.putIfAbsent(voiceMember['channel_id'], () => voiceManager);
final VoiceManager voiceManager = VoiceManager.from(voiceMember, null, null);

voices.putIfAbsent(voiceMember['user_id'], () => voiceManager);
voices.putIfAbsent(voiceMember['channel_id'], () => voiceManager);
}

MemberManager memberManager = MemberManager();
Expand Down Expand Up @@ -70,7 +69,7 @@ class GuildCreate implements WebsocketPacket {

ModerationRuleManager moderationManager = ModerationRuleManager();

WebhookManager webhookManager = WebhookManager(guildId: websocketResponse.payload['id']);
WebhookManager webhookManager = WebhookManager();

Guild guild = Guild.from(
emojiManager: emojiManager,
Expand Down Expand Up @@ -115,24 +114,17 @@ class GuildCreate implements WebsocketPacket {

// Assign guild channels
guild.channels.cache.forEach((Snowflake id, Channel channel) {
// if(voices.containsKey(id)) {
// voices.get(id)!.channel = channel as VoiceChannel;
// }
if(voices.containsKey(id)) {
voices.getOrFail(id).channel = channel as VoiceChannel;
}
});

// moderationManager.guild = guild;
//
// guild.stickers.guild = guild;
// guild.stickers.cache.forEach((_, sticker) {
// sticker.guild = guild;
// sticker.guildMember = guild.channels.cache.get(sticker.guildMemberId);
// });

guild.owner = memberManager.cache.getOrFail(websocketResponse.payload['owner_id']);
guild.afkChannel = guild.channels.cache.get<VoiceChannel>(guild.afkChannelId);
guild.systemChannel = guild.channels.cache.get<TextChannel>(guild.systemChannelId);
guild.rulesChannel = guild.channels.cache.get<TextChannel>(guild.rulesChannelId);
guild.publicUpdatesChannel = guild.channels.cache.get<TextChannel>(guild.publicUpdatesChannelId);
guild.webhooks.guild = guild;
guild.emojis.guild = guild;
guild.roles.guild = guild;
guild.scheduledEvents.guild = guild;
Expand Down
129 changes: 69 additions & 60 deletions lib/src/internal/websockets/packets/guild_update.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
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/channel_manager.dart';
import 'package:mineral/src/api/managers/emoji_manager.dart';
import 'package:mineral/src/api/managers/guild_scheduled_event_manager.dart';
import 'package:mineral/src/api/managers/member_manager.dart';
import 'package:mineral/src/api/managers/moderation_rule_manager.dart';
import 'package:mineral/src/api/managers/webhook_manager.dart';
import 'package:mineral/src/internal/managers/event_manager.dart';
import 'package:mineral/src/internal/websockets/websocket_packet.dart';
import 'package:mineral/src/internal/websockets/websocket_response.dart';

Expand All @@ -9,65 +18,65 @@ class GuildUpdate implements WebsocketPacket {

@override
Future<void> handle(WebsocketResponse websocketResponse) async {
// EventManager manager = ioc.singleton(ioc.services.event);
// MineralClient client = ioc.singleton(ioc.services.client);
//
// Guild? before = client.guilds.cache.get(websocketResponse.payload['id']);
//
// GuildRoleManager roleManager = GuildRoleManager(guildId: websocketResponse.payload['id']);
// for (dynamic item in websocketResponse.payload['roles']) {
// Role role = Role.from(roleManager: roleManager, payload: item);
// roleManager.cache.putIfAbsent(role.id, () => role);
// }
//
// MemberManager memberManager = MemberManager(guildId: websocketResponse.payload['id']);
// memberManager.cache.addAll(before!.members.cache);
//
// ChannelManager channelManager = ChannelManager(guildId: websocketResponse.payload['id']);
// channelManager.cache.addAll(before.channels.cache);
//
// EmojiManager emojiManager = EmojiManager(guildId: websocketResponse.payload['id']);
// emojiManager.cache.addAll(before.emojis.cache);
//
// ModerationRuleManager moderationManager = ModerationRuleManager(guildId: websocketResponse.payload['id']);
//
// WebhookManager webhookManager = WebhookManager(guildId: websocketResponse.payload['id']);
// webhookManager.cache.addAll(before.webhooks.cache);
//
// GuildScheduledEventManager guildScheduledEventManager = GuildScheduledEventManager(guildId: websocketResponse.payload['id']);
// guildScheduledEventManager.cache.addAll(before.scheduledEvents.cache);
//
// Guild after = Guild.from(
// emojiManager: emojiManager,
// memberManager: memberManager,
// roleManager: roleManager,
// channelManager: channelManager,
// moderationRuleManager: moderationManager,
// webhookManager: webhookManager,
// payload: websocketResponse.payload,
// guildScheduledEventManager: guildScheduledEventManager
// );
//
// moderationManager.guild = after;
//
// after.stickers.guild = after;
// after.stickers.cache.forEach((_, sticker) {
// sticker.guild = after;
// sticker.guildMember = after.channels.cache.get(sticker.guildMemberId);
// });
//
// after.afkChannel = after.channels.cache.get<VoiceChannel>(after.afkChannelId);
// after.systemChannel = after.channels.cache.get<TextChannel>(after.systemChannelId);
// after.rulesChannel = after.channels.cache.get<TextChannel>(after.rulesChannelId);
// after.publicUpdatesChannel = after.channels.cache.get<TextChannel>(after.publicUpdatesChannelId);
// after.emojis.guild = after;
// after.roles.guild = after;
//
// manager.emit(
// event: Events.guildUpdate,
// params: [before, after]
// );
//
// client.guilds.cache.set(after.id, after);
EventManager manager = ioc.singleton(ioc.services.event);
MineralClient client = ioc.singleton(ioc.services.client);

Guild? before = client.guilds.cache.get(websocketResponse.payload['id']);

GuildRoleManager roleManager = GuildRoleManager();
for (dynamic item in websocketResponse.payload['roles']) {
Role role = Role.from(roleManager: roleManager, payload: item);
roleManager.cache.putIfAbsent(role.id, () => role);
}

MemberManager memberManager = MemberManager();
memberManager.cache.addAll(before!.members.cache);

ChannelManager channelManager = ChannelManager();
channelManager.cache.addAll(before.channels.cache);

EmojiManager emojiManager = EmojiManager();
emojiManager.cache.addAll(before.emojis.cache);

ModerationRuleManager moderationManager = ModerationRuleManager();

WebhookManager webhookManager = WebhookManager();
webhookManager.cache.addAll(before.webhooks.cache);

GuildScheduledEventManager guildScheduledEventManager = GuildScheduledEventManager();
guildScheduledEventManager.cache.addAll(before.scheduledEvents.cache);

Guild after = Guild.from(
emojiManager: emojiManager,
memberManager: memberManager,
roleManager: roleManager,
channelManager: channelManager,
moderationRuleManager: moderationManager,
webhookManager: webhookManager,
payload: websocketResponse.payload,
guildScheduledEventManager: guildScheduledEventManager
);

moderationManager.guild = after;

after.stickers.guild = after;
after.stickers.cache.forEach((_, sticker) {
sticker.guild = after;
sticker.member = after.channels.cache.get(sticker.member?.id);
});

after.afkChannel = after.channels.cache.get<VoiceChannel>(after.afkChannelId);
after.systemChannel = after.channels.cache.get<TextChannel>(after.systemChannelId);
after.rulesChannel = after.channels.cache.get<TextChannel>(after.rulesChannelId);
after.publicUpdatesChannel = after.channels.cache.get<TextChannel>(after.publicUpdatesChannelId);
after.emojis.guild = after;
after.roles.guild = after;

manager.emit(
event: Events.guildUpdate,
params: [before, after]
);

client.guilds.cache.set(after.id, after);
}
}

0 comments on commit 0313f85

Please sign in to comment.