From bc8fb88440c30a4c2282ee8d2f0d08e25a8f127e Mon Sep 17 00:00:00 2001 From: LeadcodeDev Date: Sat, 6 Jan 2024 01:04:14 +0100 Subject: [PATCH] feat(api): refactor message packet --- .../data/packets/message_create_packet.dart | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/domains/data/packets/message_create_packet.dart b/lib/domains/data/packets/message_create_packet.dart index d51c495e..0cef72ce 100644 --- a/lib/domains/data/packets/message_create_packet.dart +++ b/lib/domains/data/packets/message_create_packet.dart @@ -10,20 +10,24 @@ final class MessageCreatePacket implements ListenablePacket { PacketType get event => PacketType.messageCreate; final MemoryStorageContract storage; + const MessageCreatePacket(this.storage); @override void listen(Map payload) { - final { 'message': ShardMessage message, 'dispatch': Function(InternalEventParams) dispatch } = payload; + final {'message': ShardMessage message, 'dispatch': Function(InternalEventParams) dispatch} = + payload; - final messageInstance = switch(message.payload['guild_id']) { - String() => ServerMessage.fromJson( - server: storage.servers[message.payload['guild_id']]!, - json: message.payload - ), - _ => throw 'Not implemented', - }; + switch (message.payload['guild_id']) { + case String(): + sendServerMessage(dispatch, message.payload); + default: + throw 'Not implemented'; + } + } - dispatch(InternalEventParams('ServerMessageEvent', [messageInstance])); + void sendServerMessage(Function(InternalEventParams) dispatch, Map json) { + final message = ServerMessage.fromJson(server: storage.servers[json['guild_id']]!, json: json); + dispatch(InternalEventParams('ServerMessageEvent', [message])); } }