diff --git a/bin/mineral.dart b/bin/mineral.dart index 8bc698bb..a465f91f 100644 --- a/bin/mineral.dart +++ b/bin/mineral.dart @@ -1,6 +1,6 @@ import 'package:args/args.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/framework.dart'; Future main (List arguments) async { Kernel kernel = Kernel(); diff --git a/example/advanced/create_service.dart b/example/advanced/create_service.dart deleted file mode 100644 index e46febfd..00000000 --- a/example/advanced/create_service.dart +++ /dev/null @@ -1,50 +0,0 @@ -import 'package:http/http.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; - -/// Create your own service -/// Consider the following example : -/// We want to create a service that registers a message within a database via an API that will be contacted from our previously created HTTP service. - -/// First, we will need to create our service as a class. -class NotifyService { - Http http; - NotifyService(this.http); - - Future write (String message, { String action = 'notify' }) async { - Response response = await http.post(url: '/notify', payload: { - 'action': action, - 'message': message - }); - - return response.statusCode == 200; - } -} - -/// Now that our service is written, we will instantiate it in the framework IOC from the main file. -Future main () async { - Kernel kernel = Kernel() - ..intents.defined(all: true) - ..commands.register([HelloCommand()]); - - await kernel.init(); - - Http http = Http(baseUrl: 'https://my-api'); - ioc.bind(namespace: 'NotifyService', service: NotifyService(http)); -} - -/// Now it's time to call our service! To do this we will imagine using a command directly within our discord server. -@Command(name: 'notify', description: 'Say Hello World !', scope: 'GUILD') -@Option(name: 'action', description: 'Action', type: OptionType.string, required: true) -@Option(name: 'message', description: 'Message to send', type: OptionType.string, required: true) -class HelloCommand extends MineralCommand { - Future handle (CommandInteraction interaction) async { - final String? member = interaction.getString('action'); - final String? action = interaction.getString('action'); - - final NotifyService service = ioc.singleton('NotifyService'); - await service.write('Hello World !', action: action ?? 'Notify'); - - await interaction.reply(content: 'Hello $member !'); - } -} diff --git a/example/commands/simple_command.dart b/example/commands/simple_command.dart deleted file mode 100644 index b1221319..00000000 --- a/example/commands/simple_command.dart +++ /dev/null @@ -1,12 +0,0 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; - -@Command(name: 'simple-command', description: 'Simple command description', scope: 'GUILD', everyone: true) -class SimpleCommand extends MineralCommand { - Future handle (CommandInteraction interaction) async { - await interaction.reply( - content: 'Hello World !', - private: true - ); - } -} diff --git a/example/commands/sub_command.dart b/example/commands/sub_command.dart deleted file mode 100644 index c52738af..00000000 --- a/example/commands/sub_command.dart +++ /dev/null @@ -1,21 +0,0 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; - -@Command(name: 'command', description: 'Command with subcommands', scope: 'GUILD', everyone: true) -class SimpleCommand extends MineralCommand { - @Subcommand(name: 'first', description: 'First subcommand description') - Future first (CommandInteraction interaction) async { - await interaction.reply( - content: 'Hello World !', - private: true - ); - } - - @Subcommand(name: 'second', description: 'Second subcommand description') - Future second (CommandInteraction interaction) async { - await interaction.reply( - content: 'Hello World !', - private: true - ); - } -} diff --git a/example/commands/sub_command_group.dart b/example/commands/sub_command_group.dart deleted file mode 100644 index 10eee023..00000000 --- a/example/commands/sub_command_group.dart +++ /dev/null @@ -1,22 +0,0 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; - -@Command(name: 'command', description: 'Command with subcommands', scope: 'GUILD', everyone: true) -@CommandGroup(name: 'group', description: 'Group of subcommands') -class SimpleCommand extends MineralCommand { - @Subcommand(group: 'group', name: 'first', description: 'First subcommand description') - Future first (CommandInteraction interaction) async { - await interaction.reply( - content: 'Hello World !', - private: true - ); - } - - @Subcommand(group: 'group', name: 'second', description: 'Second subcommand description') - Future second (CommandInteraction interaction) async { - await interaction.reply( - content: 'Hello World !', - private: true - ); - } -} diff --git a/example/commands/sub_command_with_binding.dart b/example/commands/sub_command_with_binding.dart deleted file mode 100644 index b0890d61..00000000 --- a/example/commands/sub_command_with_binding.dart +++ /dev/null @@ -1,13 +0,0 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; - -@Command(name: 'command', description: 'Command with subcommands', scope: 'GUILD', everyone: true) -class SimpleCommand extends MineralCommand { - @Subcommand(name: 'first-subcommand', description: 'First subcommand description', bind: 'first') - Future first (CommandInteraction interaction) async { - await interaction.reply( - content: 'Hello World !', - private: true - ); - } -} diff --git a/example/context_menus/message_context_menu.dart b/example/context_menus/message_context_menu.dart deleted file mode 100644 index a3077890..00000000 --- a/example/context_menus/message_context_menu.dart +++ /dev/null @@ -1,9 +0,0 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; - -@ContextMenu(name: 'message', type: ContextMenuType.message, scope: 'GUILD') -class MessageContext extends MineralContextMenu { - Future handle (ContextMessageInteraction interaction) async { - await interaction.reply(content: interaction.message.content, private: true); - } -} diff --git a/example/context_menus/user_context_menu.dart b/example/context_menus/user_context_menu.dart deleted file mode 100644 index 29a578e7..00000000 --- a/example/context_menus/user_context_menu.dart +++ /dev/null @@ -1,9 +0,0 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; - -@ContextMenu(name: 'user', type: ContextMenuType.user, scope: 'GUILD') -class UserContext extends MineralContextMenu { - Future handle (ContextUserInteraction interaction) async { - await interaction.reply(content: interaction.target.toString(), private: true); - } -} diff --git a/example/events/button_interaction_create.dart b/example/events/button_interaction_create.dart deleted file mode 100644 index cbe73256..00000000 --- a/example/events/button_interaction_create.dart +++ /dev/null @@ -1,15 +0,0 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; -import 'package:mineral/core.dart'; - -@Event(Events.buttonCreate, customId: 'customId') // ๐Ÿ‘ˆ You can filter events by customId component -class ButtonCreate extends MineralEvent { - Future handle (ButtonInteraction interaction) async { - Console.info(message: "Button ${interaction.customId} was clicked"); - - await interaction.reply( - content: 'Button ${interaction.customId} was clicked', - private: true, - ); - } -} diff --git a/example/events/guild_create.dart b/example/events/guild_create.dart deleted file mode 100644 index 005a187e..00000000 --- a/example/events/guild_create.dart +++ /dev/null @@ -1,9 +0,0 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; - -@Event(Events.guildCreate) -class GuildCreate extends MineralEvent { - Future handle (Guild guild) async { - // Your code here - } -} diff --git a/example/events/message_create.dart b/example/events/message_create.dart deleted file mode 100644 index b4401ce7..00000000 --- a/example/events/message_create.dart +++ /dev/null @@ -1,9 +0,0 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; - -@Event(Events.messageCreate) -class MessageCreate extends MineralEvent { - Future handle (Message message) async { - // Your code here - } -} diff --git a/example/stores/shared_state.dart b/example/stores/shared_state.dart deleted file mode 100644 index c956a9e1..00000000 --- a/example/stores/shared_state.dart +++ /dev/null @@ -1,33 +0,0 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; -import 'package:mineral/core.dart'; - -@Store('my-store') -class MyStore extends MineralStore { - List state = []; - - // Add string to this - void addItem (T value) => state.add(value); - - // Verify if this contains given value - bool has (T value) => state.contains(value); -} - -/// Register our store into the main.dart -Future main () async { - Kernel kernel = Kernel() - ..stores.register([MyStore()]); - - await kernel.init(); -} - -/// Consume the store -@Event(Events.messageCreate) -class MessageCreate extends MineralEvent { - Future handle (Message message) async { - final store = stores.getStore('my-store'); - store.addItem('Hello World !'); - - Console.info(message: 'MyStore contains ${store.state.length} items.'); - } -} diff --git a/lib/api.dart b/lib/api.dart deleted file mode 100644 index 647f7893..00000000 --- a/lib/api.dart +++ /dev/null @@ -1,69 +0,0 @@ -/// The api is the Map of all the classes, enumerations of the framework -library api; - -export 'src/api/client/mineral_client.dart' show MineralClient, ClientActivity, ClientStatus; -export 'src/api/client/client_presence.dart' show ClientPresence, GamePresence; - -export 'src/api/application.dart' show Application; -export 'src/api/user.dart' show User; -export 'src/api/status.dart' show Status, StatusType; -export 'src/api/activity.dart' show Activity; -export 'src/api/guilds/guild_member.dart' show GuildMember; -export 'src/api/managers/member_role_manager.dart' show MemberRoleManager; -export 'src/api/managers/voice_manager.dart' show VoiceManager; - -export 'src/api/guilds/guild.dart' show Guild; -export 'src/api/guilds/guild_preview.dart' show GuildPreview; -export 'src/api/moderation_rule.dart' show ModerationEventType, ModerationTriggerType, ModerationPresetType, ModerationActionType, ModerationTriggerMetadata, ModerationActionMetadata, ModerationAction, ModerationRule; - -export 'src/api/guilds/guild_scheduled_event.dart' show ScheduledEventStatus, ScheduledEventEntityType, GuildScheduledEvent, ScheduledEventUser; - -export 'src/api/webhook.dart' show Webhook; - -export 'src/internal/managers/intent_manager.dart' show Intent; - -export 'src/api/channels/channel_builder.dart' show ChannelBuilder; - -export 'src/api/channels/voice_channel.dart' show VoiceChannel; -export 'src/api/channels/text_channel.dart' show TextChannel; -export 'src/api/channels/text_based_channel.dart' show TextBasedChannel; -export 'src/api/channels/partial_text_channel.dart' show PartialTextChannel; -export 'src/api/channels/guild_channel.dart' show GuildChannel; -export 'src/api/channels/category_channel.dart' show CategoryChannel; -export 'src/api/channels/partial_channel.dart' show PartialChannel, ChannelType; -export 'src/api/channels/forum_channel.dart' show ForumChannel; -export 'src/api/channels/thread_channel.dart' show ThreadChannel; -export 'src/api/permission_overwrite.dart' show PermissionOverwrite, PermissionOverwriteType; - -export 'src/api/messages/message.dart' show Message; -export 'src/api/messages/embed_builder.dart' show EmbedBuilder, Footer, Image, Thumbnail, Author, Field; -export 'src/api/color.dart' show Color; - -export 'src/api/emoji.dart' show EmojiBuilder, Emoji; -export 'src/api/role.dart' show Role; - -export 'src/api/builders/row_builder.dart' show RowBuilder; -export 'src/api/builders/select_menu_builder.dart' show SelectMenuBuilder, SelectMenuOption, EmojiOption; -export 'src/api/builders/modal_builder.dart' show ModalBuilder; -export 'src/api/builders/text_input_builder.dart' show TextInputBuilder, TextInputStyle; -export 'src/api/builders/button_builder.dart' show ButtonBuilder, ButtonStyle; -export 'src/api/builders/code_builder.dart' show CodeBuilder; -export 'src/api/builders/message_builder.dart' show MessageBuilder; -export 'src/api/builders/forum_tag_builder.dart' show ForumTagBuilder; -export 'src/api/builders/moderation_rules_builder.dart' show ModerationRulesBuilder; -export 'src/api/image_formater.dart' show ImageFormater; - -export 'src/api/interactions/command_interaction.dart' show CommandInteraction; -export 'src/api/interactions/button_interaction.dart' show ButtonInteraction; -export 'src/api/interactions/modal_interaction.dart' show ModalInteraction; -export 'src/api/interactions/select_menu_interaction.dart' show SelectMenuInteraction; -export 'src/api/interactions/context_user_interaction.dart' show ContextUserInteraction; -export 'src/api/interactions/context_message_interaction.dart' show ContextMessageInteraction; -export 'src/api/interactions/interaction.dart' show Interaction; - - -export 'src/api/utils.dart'; -export 'src/internal/extensions/collection.dart'; -export 'src/internal/extensions/string.dart'; - -typedef Snowflake = String; diff --git a/lib/core.dart b/lib/core.dart index 3dc816f6..0fae3b9c 100644 --- a/lib/core.dart +++ b/lib/core.dart @@ -1,15 +1,8 @@ /// ๐Ÿงก The neuralgic heart of the application, this module gathers all the functionalities of the framework. library core; -export 'src/internal/services/environment.dart' show Environment; - -export 'src/internal/kernel.dart' show Kernel; -export 'package:mineral_ioc/ioc.dart' show ioc, Service; - export 'src/constants.dart'; +export 'src/internal/kernel.dart' show Kernel; +export 'src/internal/services/environment.dart' show Environment; export 'src/internal/services/http.dart'; -export 'src/internal/entities/event.dart' show Event, Events, MineralEvent; -export 'src/internal/entities/command.dart' show Command, MineralCommand, Option, OptionType, OptionChoice, Subcommand, CommandGroup; -export 'src/internal/entities/store.dart' show Store, MineralStore; -export 'src/internal/entities/module.dart' show Module, MineralModule; -export 'src/internal/entities/context_menu.dart' show ContextMenu, MineralContextMenu; + diff --git a/lib/core/api.dart b/lib/core/api.dart new file mode 100644 index 00000000..2768135d --- /dev/null +++ b/lib/core/api.dart @@ -0,0 +1,43 @@ +/// The api is the Map of all the classes, enumerations of the framework +library api; + +export '../src/api/activity.dart' show Activity; +export '../src/api/application.dart' show Application; +export '../src/api/channels/category_channel.dart' show CategoryChannel; +export '../src/api/channels/forum_channel.dart' show ForumChannel; +export '../src/api/channels/guild_channel.dart' show GuildChannel; +export '../src/api/channels/partial_channel.dart' show PartialChannel, ChannelType; +export '../src/api/channels/partial_text_channel.dart' show PartialTextChannel; +export '../src/api/channels/text_based_channel.dart' show TextBasedChannel; +export '../src/api/channels/text_channel.dart' show TextChannel; +export '../src/api/channels/thread_channel.dart' show ThreadChannel; +export '../src/api/channels/voice_channel.dart' show VoiceChannel; +export '../src/api/client/client_presence.dart' show ClientPresence, GamePresence; +export '../src/api/client/mineral_client.dart' show MineralClient, ClientActivity, ClientStatus; +export '../src/api/color.dart' show Color; +export '../src/api/emoji.dart' show Emoji; +export '../src/api/guilds/guild.dart' show Guild; +export '../src/api/guilds/guild_member.dart' show GuildMember; +export '../src/api/guilds/guild_preview.dart' show GuildPreview; +export '../src/api/guilds/guild_scheduled_event.dart' show ScheduledEventStatus, ScheduledEventEntityType, GuildScheduledEvent, ScheduledEventUser; +export '../src/api/image_formater.dart' show ImageFormater; +export '../src/api/interactions/button_interaction.dart' show ButtonInteraction; +export '../src/api/interactions/command_interaction.dart' show CommandInteraction; +export '../src/api/interactions/context_message_interaction.dart' show ContextMessageInteraction; +export '../src/api/interactions/context_user_interaction.dart' show ContextUserInteraction; +export '../src/api/interactions/interaction.dart' show Interaction; +export '../src/api/interactions/modal_interaction.dart' show ModalInteraction; +export '../src/api/interactions/select_menu_interaction.dart' show SelectMenuInteraction; +export '../src/api/managers/member_role_manager.dart' show MemberRoleManager; +export '../src/api/managers/voice_manager.dart' show VoiceManager; +export '../src/api/messages/message.dart' show Message; +export '../src/api/moderation_rule.dart' show ModerationEventType, ModerationTriggerType, ModerationPresetType, ModerationActionType, ModerationTriggerMetadata, ModerationActionMetadata, ModerationAction, ModerationRule; +export '../src/api/permission_overwrite.dart' show PermissionOverwrite, PermissionOverwriteType; +export '../src/api/role.dart' show Role; +export '../src/api/status.dart' show Status, StatusType; +export '../src/api/user.dart' show User; +export '../src/api/utils.dart'; +export '../src/api/webhook.dart' show Webhook; +export '../src/internal/managers/intent_manager.dart' show Intent; + +typedef Snowflake = String; diff --git a/lib/core/builders.dart b/lib/core/builders.dart new file mode 100644 index 00000000..9e224932 --- /dev/null +++ b/lib/core/builders.dart @@ -0,0 +1,14 @@ +library builders; + +export '../src/api/builders/button_builder.dart' show ButtonBuilder, ButtonStyle; +export '../src/api/builders/channel_builder.dart' show ChannelBuilder; +export '../src/api/builders/code_builder.dart' show CodeBuilder; +export '../src/api/builders/embed_builder.dart' show EmbedBuilder, Footer, Image, Thumbnail, Author, Field; +export '../src/api/builders/emoji_builder.dart' show EmojiBuilder; +export '../src/api/builders/forum_tag_builder.dart' show ForumTagBuilder; +export '../src/api/builders/message_builder.dart' show MessageBuilder; +export '../src/api/builders/modal_builder.dart' show ModalBuilder; +export '../src/api/builders/moderation_rules_builder.dart' show ModerationRulesBuilder; +export '../src/api/builders/row_builder.dart' show RowBuilder; +export '../src/api/builders/select_menu_builder.dart' show SelectMenuBuilder, SelectMenuOption, EmojiOption; +export '../src/api/builders/text_input_builder.dart' show TextInputBuilder, TextInputStyle; \ No newline at end of file diff --git a/lib/core/collectors.dart b/lib/core/collectors.dart new file mode 100644 index 00000000..62a0eadb --- /dev/null +++ b/lib/core/collectors.dart @@ -0,0 +1,3 @@ +library collectors; + +export '../src/api/collectors/message_collector.dart' show MessageCollector; \ No newline at end of file diff --git a/lib/core/events.dart b/lib/core/events.dart new file mode 100644 index 00000000..9261c8f2 --- /dev/null +++ b/lib/core/events.dart @@ -0,0 +1,42 @@ +library events; + +export '../src/internal/websockets/events/accept_rules_event.dart'; +export '../src/internal/websockets/events/button_create_event.dart'; +export '../src/internal/websockets/events/channel_create_event.dart'; +export '../src/internal/websockets/events/channel_delete_event.dart'; +export '../src/internal/websockets/events/channel_update_event.dart'; +export '../src/internal/websockets/events/command_create_event.dart'; +export '../src/internal/websockets/events/guild_create_event.dart'; +export '../src/internal/websockets/events/guild_scheduled_event_create_event.dart'; +export '../src/internal/websockets/events/guild_scheduled_event_delete_event.dart'; +export '../src/internal/websockets/events/guild_scheduled_event_update_event.dart'; +export '../src/internal/websockets/events/guild_scheduled_event_user_add_event.dart'; +export '../src/internal/websockets/events/guild_scheduled_event_user_remove_event.dart'; +export '../src/internal/websockets/events/guild_update_event.dart'; +export '../src/internal/websockets/events/member_deaf_event.dart'; +export '../src/internal/websockets/events/member_join_event.dart'; +export '../src/internal/websockets/events/member_leave_event.dart'; +export '../src/internal/websockets/events/member_mute_event.dart'; +export '../src/internal/websockets/events/member_role_update_event.dart'; +export '../src/internal/websockets/events/member_self_deaf_event.dart'; +export '../src/internal/websockets/events/member_self_mute_event.dart'; +export '../src/internal/websockets/events/member_self_undeaf_event.dart'; +export '../src/internal/websockets/events/member_self_unmute_event.dart'; +export '../src/internal/websockets/events/member_undeaf_event.dart'; +export '../src/internal/websockets/events/member_unmute_event.dart'; +export '../src/internal/websockets/events/member_update_event.dart'; +export '../src/internal/websockets/events/message_create_event.dart'; +export '../src/internal/websockets/events/message_delete_event.dart'; +export '../src/internal/websockets/events/message_update_event.dart'; +export '../src/internal/websockets/events/modal_create_event.dart'; +export '../src/internal/websockets/events/moderation_rules_create_event.dart'; +export '../src/internal/websockets/events/moderation_rules_delete_event.dart'; +export '../src/internal/websockets/events/moderation_rules_update_event.dart'; +export '../src/internal/websockets/events/presence_update_event.dart'; +export '../src/internal/websockets/events/ready_event.dart'; +export '../src/internal/websockets/events/select_menu_create_event.dart'; +export '../src/internal/websockets/events/voice_join_event.dart'; +export '../src/internal/websockets/events/voice_leave_event.dart'; +export '../src/internal/websockets/events/voice_move_event.dart'; +export '../src/internal/websockets/events/voice_state_update_event.dart'; +export '../src/internal/websockets/events/webhook_update_event.dart'; \ No newline at end of file diff --git a/lib/core/extras.dart b/lib/core/extras.dart new file mode 100644 index 00000000..cbe636f4 --- /dev/null +++ b/lib/core/extras.dart @@ -0,0 +1,4 @@ +library extras; + +export '../src/internal/mixins/container.dart' show Container; +export '../src/internal/mixins/mineral_context.dart' show MineralContext; \ No newline at end of file diff --git a/lib/exception.dart b/lib/exception.dart index ca6d0019..97e67377 100644 --- a/lib/exception.dart +++ b/lib/exception.dart @@ -1,9 +1,9 @@ /// Core exceptions library exception; -export 'src/exceptions/token_exception.dart'; -export 'src/exceptions/empty_parameter_exception.dart'; -export 'src/exceptions/missing_feature_exception.dart'; export 'src/exceptions/api_exception.dart'; +export 'src/exceptions/empty_parameter_exception.dart'; export 'src/exceptions/http_exception.dart'; -export 'src/exceptions/invalid_parameter_exception.dart'; \ No newline at end of file +export 'src/exceptions/invalid_parameter_exception.dart'; +export 'src/exceptions/missing_feature_exception.dart'; +export 'src/exceptions/token_exception.dart'; \ No newline at end of file diff --git a/lib/framework.dart b/lib/framework.dart new file mode 100644 index 00000000..06863be0 --- /dev/null +++ b/lib/framework.dart @@ -0,0 +1,10 @@ +library framework; + +export '../src/console.dart'; +export '../src/internal/entities/command.dart' show MineralCommand, CommandBuilder, Option, OptionType, OptionChoice, SubCommandBuilder, CommandGroupBuilder, Scope; +export '../src/internal/entities/context_menu.dart' show MineralContextMenu; +export '../src/internal/entities/event.dart' show MineralEvent, Event; +export '../src/internal/entities/module.dart' show MineralModule; +export '../src/internal/entities/state.dart' show MineralState; +export '../src/internal/extensions/collection.dart'; +export '../src/internal/extensions/string.dart'; \ No newline at end of file diff --git a/lib/src/api/activity.dart b/lib/src/api/activity.dart index d521eebc..23778c9e 100644 --- a/lib/src/api/activity.dart +++ b/lib/src/api/activity.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/src/api/client/client_presence.dart'; class Activity { diff --git a/lib/src/api/application.dart b/lib/src/api/application.dart index 0c37ab72..2081f5e2 100644 --- a/lib/src/api/application.dart +++ b/lib/src/api/application.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; class Application { Snowflake _id; diff --git a/lib/src/api/builders/button_builder.dart b/lib/src/api/builders/button_builder.dart index 30a2366c..66a33a4f 100644 --- a/lib/src/api/builders/button_builder.dart +++ b/lib/src/api/builders/button_builder.dart @@ -1,5 +1,5 @@ -import 'package:mineral/api.dart'; import 'package:mineral/src/api/builders/component_builder.dart'; +import 'package:mineral/src/api/builders/emoji_builder.dart'; enum ButtonStyle { primary(1), diff --git a/lib/src/api/channels/channel_builder.dart b/lib/src/api/builders/channel_builder.dart similarity index 96% rename from lib/src/api/channels/channel_builder.dart rename to lib/src/api/builders/channel_builder.dart index 0c8fba08..9bef41a0 100644 --- a/lib/src/api/channels/channel_builder.dart +++ b/lib/src/api/builders/channel_builder.dart @@ -1,4 +1,5 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; class ChannelBuilder { Map payload; diff --git a/lib/src/api/builders/component_builder.dart b/lib/src/api/builders/component_builder.dart index 13bda5cb..28d2bb93 100644 --- a/lib/src/api/builders/component_builder.dart +++ b/lib/src/api/builders/component_builder.dart @@ -1,6 +1,8 @@ import 'package:collection/collection.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; +import 'package:mineral/framework.dart'; +import 'package:mineral_ioc/ioc.dart'; enum ComponentType { actionRow(1), @@ -21,7 +23,7 @@ abstract class ComponentBuilder { ComponentBuilder({ required this.type }); static wrap (dynamic payload, Snowflake? guildId) { - final Guild? guild = ioc.singleton(Service.client).guilds.cache.get(guildId); + final Guild? guild = ioc.use().guilds.cache.get(guildId); final componentType = ComponentType.values.firstWhereOrNull((element) => element.value == payload['type']); if (componentType == null) { diff --git a/lib/src/api/messages/embed_builder.dart b/lib/src/api/builders/embed_builder.dart similarity index 98% rename from lib/src/api/messages/embed_builder.dart rename to lib/src/api/builders/embed_builder.dart index a2b4bf7d..b26f39a6 100644 --- a/lib/src/api/messages/embed_builder.dart +++ b/lib/src/api/builders/embed_builder.dart @@ -1,5 +1,5 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral_ioc/ioc.dart'; class Footer { String text; @@ -259,7 +259,7 @@ class EmbedBuilder { } factory EmbedBuilder.fromGuildPreview(GuildPreview preview) { - MineralClient client = ioc.singleton(Service.client); + MineralClient client = ioc.use(); final EmbedBuilder embed = EmbedBuilder( title: preview.label, diff --git a/lib/src/api/builders/emoji_builder.dart b/lib/src/api/builders/emoji_builder.dart new file mode 100644 index 00000000..34eacf2a --- /dev/null +++ b/lib/src/api/builders/emoji_builder.dart @@ -0,0 +1,10 @@ +import 'package:mineral/src/api/emoji.dart'; + +class EmojiBuilder { + PartialEmoji emoji; + + EmojiBuilder(this.emoji); + + factory EmojiBuilder.fromUnicode(String label) => EmojiBuilder(PartialEmoji('', label, false)); + factory EmojiBuilder.fromEmoji(Emoji emoji) => EmojiBuilder(emoji); +} \ No newline at end of file diff --git a/lib/src/api/builders/forum_tag_builder.dart b/lib/src/api/builders/forum_tag_builder.dart index 57719482..aedbfc7b 100644 --- a/lib/src/api/builders/forum_tag_builder.dart +++ b/lib/src/api/builders/forum_tag_builder.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/src/api/builders/emoji_builder.dart'; class ForumTagBuilder { final String label; diff --git a/lib/src/api/builders/message_builder.dart b/lib/src/api/builders/message_builder.dart index c13ebce6..1eeefc81 100644 --- a/lib/src/api/builders/message_builder.dart +++ b/lib/src/api/builders/message_builder.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/builders.dart'; import 'package:mineral/src/api/builders/component_builder.dart'; import 'package:mineral/src/api/messages/message_sticker_item.dart'; diff --git a/lib/src/api/builders/modal_builder.dart b/lib/src/api/builders/modal_builder.dart index e693c545..ad1d6ab2 100644 --- a/lib/src/api/builders/modal_builder.dart +++ b/lib/src/api/builders/modal_builder.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/builders.dart'; import 'package:mineral/src/api/builders/component_builder.dart'; class ModalBuilder extends ComponentBuilder { diff --git a/lib/src/api/builders/moderation_rules_builder.dart b/lib/src/api/builders/moderation_rules_builder.dart index 969e2580..758aaf81 100644 --- a/lib/src/api/builders/moderation_rules_builder.dart +++ b/lib/src/api/builders/moderation_rules_builder.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/src/exceptions/too_many.dart'; class ModerationRulesBuilder { diff --git a/lib/src/api/builders/row_builder.dart b/lib/src/api/builders/row_builder.dart index b3e4abea..584f592d 100644 --- a/lib/src/api/builders/row_builder.dart +++ b/lib/src/api/builders/row_builder.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/builders.dart'; import 'package:mineral/src/api/builders/component_builder.dart'; class RowBuilder extends ComponentBuilder { diff --git a/lib/src/api/builders/select_menu_builder.dart b/lib/src/api/builders/select_menu_builder.dart index cee53b08..cd383fed 100644 --- a/lib/src/api/builders/select_menu_builder.dart +++ b/lib/src/api/builders/select_menu_builder.dart @@ -1,5 +1,7 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; import 'package:mineral/src/api/builders/component_builder.dart'; +import 'package:mineral/src/api/builders/emoji_builder.dart'; class SelectMenuBuilder extends ComponentBuilder { String customId; diff --git a/lib/src/api/channels/category_channel.dart b/lib/src/api/channels/category_channel.dart index 7e9c55ef..40aee85f 100644 --- a/lib/src/api/channels/category_channel.dart +++ b/lib/src/api/channels/category_channel.dart @@ -1,4 +1,6 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/permission_overwrite_manager.dart'; class CategoryChannel extends GuildChannel { diff --git a/lib/src/api/channels/dm_channel.dart b/lib/src/api/channels/dm_channel.dart index 5b7ad7a1..1f05cb9f 100644 --- a/lib/src/api/channels/dm_channel.dart +++ b/lib/src/api/channels/dm_channel.dart @@ -1,8 +1,10 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/message_manager.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; +import 'package:mineral_ioc/ioc.dart'; -class DmChannel extends PartialChannel { +class DmChannel extends PartialChannel with Container { Snowflake? lastMessageId; MessageManager messages; Map recipients; @@ -15,12 +17,10 @@ class DmChannel extends PartialChannel { ); factory DmChannel.fromPayload(dynamic payload) { - MineralClient client = ioc.singleton(Service.client); - Map users = {}; if (payload['recipients'] != null) { for (dynamic element in payload['recipients']) { - User? user = client.users.cache.get(element['id']); + User? user = ioc.use().users.cache.get(element['id']); user ??= User.from(element); users.putIfAbsent(user.id, () => user!); diff --git a/lib/src/api/channels/forum_channel.dart b/lib/src/api/channels/forum_channel.dart index 9918463f..70ad6359 100644 --- a/lib/src/api/channels/forum_channel.dart +++ b/lib/src/api/channels/forum_channel.dart @@ -1,4 +1,5 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; import 'package:mineral/src/api/managers/forum_discussion_manager.dart'; import 'package:mineral/src/api/managers/permission_overwrite_manager.dart'; diff --git a/lib/src/api/channels/guild_channel.dart b/lib/src/api/channels/guild_channel.dart index 6b73e722..b94cfc15 100644 --- a/lib/src/api/channels/guild_channel.dart +++ b/lib/src/api/channels/guild_channel.dart @@ -1,10 +1,13 @@ import 'package:http/http.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; +import 'package:mineral/src/api/builders/channel_builder.dart'; import 'package:mineral/src/api/managers/permission_overwrite_manager.dart'; +import 'package:mineral/src/console.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class GuildChannel extends PartialChannel { +class GuildChannel extends PartialChannel with Container { final Snowflake _guildId; final Snowflake? _parentId; final String _label; @@ -16,7 +19,7 @@ class GuildChannel extends PartialChannel { GuildChannel(this._guildId, this._parentId, this._label, this._type, this._position, this._flags, this._permissions, super.id); /// Get [Guild] from [Ioc] - Guild get guild => ioc.singleton(Service.client).guilds.cache.getOrFail(_guildId); + Guild get guild => container.use().guilds.cache.getOrFail(_guildId); /// Get [CategoryChannel] or [TextChannel] parent GuildChannel? get parent => guild.channels.cache.get(_parentId); @@ -54,8 +57,7 @@ class GuildChannel extends PartialChannel { Future update (ChannelBuilder builder) async { if (_validate()) { - Http http = ioc.singleton(Service.http); - await http.patch(url: '/channels/$id', payload: builder.payload); + await container.use().patch(url: '/channels/$id', payload: builder.payload); } } @@ -66,8 +68,7 @@ class GuildChannel extends PartialChannel { /// await channel.delete() /// ``` Future delete () async { - Http http = ioc.singleton(Service.http); - Response response = await http.destroy(url: '/channels/$id'); + Response response = await container.use().destroy(url: '/channels/$id'); guild.channels.cache.remove(this); return response.statusCode == 200; diff --git a/lib/src/api/channels/news_channel.dart b/lib/src/api/channels/news_channel.dart index b000a39b..aeb5763a 100644 --- a/lib/src/api/channels/news_channel.dart +++ b/lib/src/api/channels/news_channel.dart @@ -1,12 +1,13 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.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'; +import 'package:mineral/src/console.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class NewsChannel extends TextChannel { +class NewsChannel extends TextChannel with Container { NewsChannel( super.description, super.lastPinTime, @@ -33,8 +34,7 @@ class NewsChannel extends TextChannel { return; } - Http http = ioc.singleton(Service.http); - await http.post(url: '/channels/$id/followers', payload: { + await container.use().post(url: '/channels/$id/followers', payload: { 'webhook_channel_id': webhookId }); } diff --git a/lib/src/api/channels/partial_channel.dart b/lib/src/api/channels/partial_channel.dart index 4f51d872..f8353c9d 100644 --- a/lib/src/api/channels/partial_channel.dart +++ b/lib/src/api/channels/partial_channel.dart @@ -1,10 +1,9 @@ import 'package:collection/collection.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/src/api/channels/dm_channel.dart'; import 'package:mineral/src/api/channels/news_channel.dart'; import 'package:mineral/src/api/channels/stage_channel.dart'; -import 'package:mineral/src/api/channels/thread_channel.dart'; +import 'package:mineral/src/console.dart'; class PartialChannel { final Snowflake _id; diff --git a/lib/src/api/channels/partial_text_channel.dart b/lib/src/api/channels/partial_text_channel.dart index 1ca4fa6e..558518e4 100644 --- a/lib/src/api/channels/partial_text_channel.dart +++ b/lib/src/api/channels/partial_text_channel.dart @@ -1,13 +1,15 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; +import 'package:mineral/core/collectors.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/message_manager.dart'; - import 'package:mineral/src/internal/extensions/mineral_client.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class PartialTextChannel extends GuildChannel { +class PartialTextChannel extends GuildChannel with Container { final MessageManager _messages; final Snowflake? _lastMessageId; @@ -25,7 +27,7 @@ class PartialTextChannel extends GuildChannel { /// await channel.send(content: 'Hello world ! ๐Ÿ”ฅ'); /// ``` Future send ({ String? content, List? embeds, List? components, bool? tts }) async { - MineralClient client = ioc.singleton(Service.client); + MineralClient client = container.use(); Response response = await client.sendMessage(this, content: content, @@ -42,4 +44,8 @@ class PartialTextChannel extends GuildChannel { return null; } + + MessageCollector createMessageCollector (bool Function(Message message) filter, { int? max, Duration? duration }) { + return MessageCollector(filter, max, duration); + } } diff --git a/lib/src/api/channels/source_channel.dart b/lib/src/api/channels/source_channel.dart new file mode 100644 index 00000000..ceb112ec --- /dev/null +++ b/lib/src/api/channels/source_channel.dart @@ -0,0 +1,11 @@ +import 'package:mineral/core/api.dart'; + +class SourceChannel { + final Snowflake _id; + final String _label; + + SourceChannel(this._id, this._label); + + Snowflake get id => _id; + String get label => _label; +} \ No newline at end of file diff --git a/lib/src/api/channels/stage_channel.dart b/lib/src/api/channels/stage_channel.dart index b2528427..a494cc45 100644 --- a/lib/src/api/channels/stage_channel.dart +++ b/lib/src/api/channels/stage_channel.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/src/api/managers/permission_overwrite_manager.dart'; class StageChannel extends GuildChannel { diff --git a/lib/src/api/channels/text_based_channel.dart b/lib/src/api/channels/text_based_channel.dart index 1b70d7b7..53eb3df8 100644 --- a/lib/src/api/channels/text_based_channel.dart +++ b/lib/src/api/channels/text_based_channel.dart @@ -1,4 +1,5 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/src/api/builders/channel_builder.dart'; import 'package:mineral/src/api/managers/webhook_manager.dart'; class TextBasedChannel extends PartialTextChannel { diff --git a/lib/src/api/channels/text_channel.dart b/lib/src/api/channels/text_channel.dart index 66baedaf..54e85192 100644 --- a/lib/src/api/channels/text_channel.dart +++ b/lib/src/api/channels/text_channel.dart @@ -1,4 +1,5 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/src/api/builders/channel_builder.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'; diff --git a/lib/src/api/channels/thread_channel.dart b/lib/src/api/channels/thread_channel.dart index ea63a90b..de730281 100644 --- a/lib/src/api/channels/thread_channel.dart +++ b/lib/src/api/channels/thread_channel.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/src/api/managers/message_manager.dart'; import 'package:mineral/src/api/managers/permission_overwrite_manager.dart'; diff --git a/lib/src/api/channels/voice_channel.dart b/lib/src/api/channels/voice_channel.dart index a4d8a38d..f7b3ef83 100644 --- a/lib/src/api/channels/voice_channel.dart +++ b/lib/src/api/channels/voice_channel.dart @@ -1,4 +1,6 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; +import 'package:mineral/src/api/builders/channel_builder.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'; diff --git a/lib/src/api/client/client_presence.dart b/lib/src/api/client/client_presence.dart index 1c7a6e50..978de5cf 100644 --- a/lib/src/api/client/client_presence.dart +++ b/lib/src/api/client/client_presence.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; enum GamePresence { game(0), diff --git a/lib/src/api/client/mineral_client.dart b/lib/src/api/client/mineral_client.dart index 7eb2c7a3..4de5c521 100644 --- a/lib/src/api/client/mineral_client.dart +++ b/lib/src/api/client/mineral_client.dart @@ -1,14 +1,12 @@ -import 'dart:convert'; - -import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/command_manager.dart'; import 'package:mineral/src/api/managers/dm_channel_manager.dart'; import 'package:mineral/src/api/managers/guild_manager.dart'; import 'package:mineral/src/api/managers/user_manager.dart'; -import 'package:mineral/src/internal/entities/command.dart'; import 'package:mineral/src/internal/websockets/sharding/shard_manager.dart'; +import 'package:mineral_ioc/ioc.dart'; enum ClientStatus { online('online'), @@ -33,7 +31,7 @@ class ClientActivity { Object toJson () => { 'name': name, 'type': type.value }; } -class MineralClient { +class MineralClient extends MineralService { User _user; GuildManager _guilds; DmChannelManager _dmChannels; @@ -53,7 +51,7 @@ class MineralClient { this._application, this._intents, this._commands, - ); + ): super(inject: true); User get user => _user; GuildManager get guilds => _guilds; @@ -62,7 +60,7 @@ class MineralClient { String get sessionId => _sessionId; Application get application => _application; List get intents => _intents; - ShardManager get _shards => ioc.singleton(Service.shards); + ShardManager get _shards => ioc.use(); /// ### Returns the time the [MineralClient] is online Duration get uptimeDuration => DateTime.now().difference(uptime); @@ -116,48 +114,27 @@ class MineralClient { /// final int latency = client.getLatency(); /// ``` int getLatency () { - ShardManager manager = ioc.singleton(Service.shards); - return manager.getLatency(); + return ioc.use().getLatency(); } - Future registerGlobalCommands ({ required List commands }) async { - Http http = ioc.singleton(Service.http); - - await http.put( + Future registerGlobalCommands ({ required List commands }) async { + await ioc.use().put( url: "/applications/${_application.id}/commands", - payload: commands.map((command) => command.toJson()).toList() + payload: commands.map((command) => command.toJson).toList() ); } - Future registerGuildCommands ({ required Guild guild, required List commands, required List contextMenus }) async { - Http http = ioc.singleton(Service.http); - Response response = await http.put( + Future registerGuildCommands ({ required Guild guild, required List commands, required List contextMenus }) async { + await ioc.use().put( url: "/applications/${_application.id}/guilds/${guild.id}/commands", payload: [ - ...commands.map((command) => command.toJson()).toList(), - ...contextMenus.map((contextMenus) => contextMenus.toJson()).toList() + ...commands.map((command) => command.toJson).toList(), + ...contextMenus.map((contextMenus) => contextMenus.builder.toJson).toList() ] ); - - if (response.statusCode == 200) { - final List _commands = jsonDecode(response.body); - for (final element in _commands) { - final command = commands.firstWhere((command) => command.name == element['name']); - command.id = element['id']; - - if (command.scope == 'GUILD' || command.scope == guild.id) { - guild.commands.cache.putIfAbsent(command.name, () => command); - return; - } - - this._commands.cache.putIfAbsent(command.name, () => command); - } - } } factory MineralClient.from({ required dynamic payload }) { - ShardManager manager = ioc.singleton(Service.shards); - return MineralClient( User.from(payload['user']), GuildManager(), @@ -165,7 +142,7 @@ class MineralClient { UserManager(), payload['session_id'], Application.from(payload['application']), - manager.intents, + ioc.use().intents, CommandManager(null), ); } diff --git a/lib/src/api/collectors/collector.dart b/lib/src/api/collectors/collector.dart new file mode 100644 index 00000000..5ab9e1e0 --- /dev/null +++ b/lib/src/api/collectors/collector.dart @@ -0,0 +1,12 @@ +import 'dart:async'; + +abstract class Collector { + final StreamController controller = StreamController(); + final Type _event; + + Collector(this._event); + + Type get event => _event; + + Future collect (); +} \ No newline at end of file diff --git a/lib/src/api/collectors/message_collector.dart b/lib/src/api/collectors/message_collector.dart new file mode 100644 index 00000000..5905e6ad --- /dev/null +++ b/lib/src/api/collectors/message_collector.dart @@ -0,0 +1,42 @@ +import 'dart:async'; + +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/events.dart'; +import 'package:mineral/src/api/collectors/collector.dart'; +import 'package:mineral/src/internal/managers/collector_manager.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; + +class MessageCollector extends Collector with Container { + final Map _messages = {}; + StreamSubscription? subscription; + + final bool Function(Message message) _filter; + final int? _max; + Duration? _time; + + MessageCollector (this._filter, this._max, this._time): super(MessageCreateEvent) { + container.use().subscribe(this); + } + + @override + Future collect () async { + final completer = Completer(); + subscription = controller.stream.listen((message) async { + if (_filter(message)) { + _messages.putIfAbsent(message.id, () => message); + } + + if (_messages.length == _max) { + await _unsubscribe(); + completer.complete(_messages); + } + }); + + return completer.future; + } + + Future _unsubscribe () async { + container.use().unsubscribe(this); + await subscription?.cancel(); + } +} \ No newline at end of file diff --git a/lib/src/api/emoji.dart b/lib/src/api/emoji.dart index 55883392..75434b43 100644 --- a/lib/src/api/emoji.dart +++ b/lib/src/api/emoji.dart @@ -1,17 +1,10 @@ import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/emoji_manager.dart'; import 'package:mineral/src/api/managers/member_manager.dart'; - -class EmojiBuilder { - PartialEmoji emoji; - - EmojiBuilder(this.emoji); - - factory EmojiBuilder.fromUnicode(String label) => EmojiBuilder(PartialEmoji('', label, false)); - factory EmojiBuilder.fromEmoji(Emoji emoji) => EmojiBuilder(emoji); -} +import 'package:mineral_ioc/ioc.dart'; class PartialEmoji { final Snowflake _id; @@ -66,8 +59,7 @@ class Emoji extends PartialEmoji { /// } /// ``` Future setLabel (String label) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/${manager.guild.id}/emojis/$id", payload: { 'name': label }); + Response response = await ioc.use().patch(url: "/guilds/${manager.guild.id}/emojis/$id", payload: { 'name': label }); if (response.statusCode == 200) { _label = label; @@ -90,8 +82,7 @@ class Emoji extends PartialEmoji { /// await emoji.delete(reason: 'I will destroy this..'); /// ``` Future delete () async { - Http http = ioc.singleton(Service.http); - Response response = await http.destroy(url: "/guilds/${manager.guild.id}/emojis/$id"); + Response response = await ioc.use().destroy(url: "/guilds/${manager.guild.id}/emojis/$id"); if (response.statusCode == 200) { manager.cache.remove(id); diff --git a/lib/src/api/forum_tag.dart b/lib/src/api/forum_tag.dart index 716c2325..3b0423c4 100644 --- a/lib/src/api/forum_tag.dart +++ b/lib/src/api/forum_tag.dart @@ -1,5 +1,4 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/src/api/emoji.dart'; +import 'package:mineral/core/api.dart'; class ForumTag { final Snowflake _id; @@ -14,4 +13,8 @@ class ForumTag { Snowflake get id => _id; String get label => _label; bool get moderated => _moderated; + + Snowflake get emojiId => _emojiId; + String get emojiLabel => _emojiLabel; + Snowflake get channelId => _channelId; } diff --git a/lib/src/api/guilds/guild.dart b/lib/src/api/guilds/guild.dart index daffb0e3..2d32aae0 100644 --- a/lib/src/api/guilds/guild.dart +++ b/lib/src/api/guilds/guild.dart @@ -1,24 +1,24 @@ import 'dart:convert'; +import 'package:collection/collection.dart'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/exception.dart'; -import 'package:mineral/helper.dart'; -import 'package:mineral/src/api/managers/command_manager.dart'; -import 'package:mineral/src/api/managers/guild_role_manager.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/channel_manager.dart'; +import 'package:mineral/src/api/managers/command_manager.dart'; import 'package:mineral/src/api/managers/emoji_manager.dart'; +import 'package:mineral/src/api/managers/guild_role_manager.dart'; +import 'package:mineral/src/api/managers/guild_scheduled_event_manager.dart'; import 'package:mineral/src/api/managers/guild_webhook_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/sticker_manager.dart'; import 'package:mineral/src/api/managers/webhook_manager.dart'; -import 'package:mineral/src/api/managers/guild_scheduled_event_manager.dart'; import 'package:mineral/src/api/welcome_screen.dart'; - -import 'package:collection/collection.dart'; +import 'package:mineral/src/helper.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; enum VerificationLevel { none(0), @@ -31,7 +31,19 @@ enum VerificationLevel { const VerificationLevel(this.value); } -class Guild { +class SourceGuild { + final Snowflake _id; + final String _label; + final ImageFormater _icon; + + SourceGuild(this._id, this._label, this._icon); + + Snowflake get id => _id; + String get label => _label; + ImageFormater? get icon => _icon; +} + +class Guild with Container { Snowflake _id; String _name; Snowflake _ownerId; @@ -184,8 +196,7 @@ class Guild { /// await guild.setName('Guild name'); /// ``` Future setName (String name) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'name': name }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'name': name }); if (response.statusCode == 200) { _name = name; @@ -201,8 +212,7 @@ class Guild { /// await guild.setVerificationLevel(VerificationLevel.veryHigh); /// ``` Future setVerificationLevel (VerificationLevel level) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'verification_level': level.value }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'verification_level': level.value }); if (response.statusCode == 200) { _verificationLevel = level; @@ -218,8 +228,7 @@ class Guild { /// await guild.setMessageNotification(1); /// ``` Future setMessageNotification (int level) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'default_message_notifications': level }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'default_message_notifications': level }); if (response.statusCode == 200) { _defaultMessageNotifications = level; @@ -237,8 +246,7 @@ class Guild { /// await guild.setExplicitContentFilter(2); /// ``` Future setExplicitContentFilter (int level) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'explicit_content_filter': level }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'explicit_content_filter': level }); if (response.statusCode == 200) { _explicitContentFilter = level; @@ -256,8 +264,7 @@ class Guild { /// } /// ``` Future setAfkChannel (VoiceChannel channel) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'afk_channel_id': channel.id }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'afk_channel_id': channel.id }); if (response.statusCode == 200) { _afkChannelId = channel.id; @@ -279,15 +286,14 @@ class Guild { /// } /// ``` Future setOwner (GuildMember guildMember) async { - MineralClient client = ioc.singleton(Service.client); - Http http = ioc.singleton(Service.http); + MineralClient client = container.use(); if (owner.id != client.user.id) { Console.error(message: "You cannot change the owner of the server because it does not belong to the ${client.user.username} client."); return; } - Response response = await http.patch(url: "/guilds/$id", payload: { 'owner_id': guildMember.user.id }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'owner_id': guildMember.user.id }); if (response.statusCode == 200) { _ownerId = guildMember.id; @@ -309,8 +315,7 @@ class Guild { String file = await Helper.getPicture(filename); - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'splash': file }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'splash': file }); if (response.statusCode == 200) { _splash = ImageFormater(file, ''); @@ -330,8 +335,7 @@ class Guild { throw MissingFeatureException(cause: "The $name guild does not have the ${GuildFeature.inviteSplash} feature."); } - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'splash': null }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'splash': null }); if (response.statusCode == 200) { _splash = null; @@ -353,8 +357,7 @@ class Guild { String file = await Helper.getPicture(filename); - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'discovery_splash': file }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'discovery_splash': file }); if (response.statusCode == 200) { _discoverySplash = ImageFormater(file, ''); @@ -374,8 +377,7 @@ class Guild { throw MissingFeatureException(cause: "The $name guild does not have the ${GuildFeature.discoverable} feature."); } - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'discovery_splash': null }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'discovery_splash': null }); if (response.statusCode == 200) { _discoverySplash = null; @@ -397,8 +399,7 @@ class Guild { String file = await Helper.getPicture(filename); - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'banner': file }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'banner': file }); if (response.statusCode == 200) { _banner = ImageFormater(file, ''); @@ -418,8 +419,7 @@ class Guild { throw MissingFeatureException(cause: "The $name guild does not have the ${GuildFeature.banner} feature."); } - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'banner': null }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'banner': null }); if (response.statusCode == 200) { _banner = null; @@ -435,8 +435,7 @@ class Guild { Future setIcon (String filename) async { String file = await Helper.getPicture(filename); - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'icon': file }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'icon': file }); if (response.statusCode == 200) { _icon = ImageFormater(file, ''); @@ -450,8 +449,7 @@ class Guild { /// await guild.removeIcon(); /// ``` Future removeIcon () async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'icon': null }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'icon': null }); if (response.statusCode == 200) { _icon = null; @@ -469,8 +467,7 @@ class Guild { /// } /// ``` Future setSystemChannel (TextChannel channel) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'system_channel_id': channel.id }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'system_channel_id': channel.id }); if (response.statusCode == 200) { _systemChannelId = channel.id; @@ -489,8 +486,7 @@ class Guild { /// } /// ``` Future setRulesChannel (TextChannel channel) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'rules_channel_id': channel.id }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'rules_channel_id': channel.id }); if (response.statusCode == 200) { _rulesChannelId = channel.id; @@ -509,8 +505,7 @@ class Guild { /// } /// ``` Future setPublicUpdateChannel (TextChannel channel) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'public_updates_channel_id': channel.id }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'public_updates_channel_id': channel.id }); if (response.statusCode == 200) { _publicUpdatesChannelId = channel.id; @@ -527,8 +522,7 @@ class Guild { /// await guild.setPreferredLocale(Locale.fr); // ๐Ÿ‘ˆ Now you can use Lang enum /// ``` Future setPreferredLocale (Locale locale) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$id", payload: { 'public_updates_channel_id': locale }); + Response response = await container.use().patch(url: "/guilds/$id", payload: { 'public_updates_channel_id': locale }); if (response.statusCode == 200) { _preferredLocale = locale as String; @@ -542,18 +536,16 @@ class Guild { /// await guild.leave(); /// ``` Future leave () async { - Http http = ioc.singleton(Service.http); - Response response = await http.destroy(url: '/users/@me/guilds/$id'); + Response response = await container.use().destroy(url: '/users/@me/guilds/$id'); if (response.statusCode == 204) { - MineralClient client = ioc.singleton(Service.client); + MineralClient client = container.use(); client.guilds.cache.remove(this); } } Future preview () async { - Http http = ioc.singleton(Service.http); - Response response = await http.get(url: '/guilds/$id/preview'); + Response response = await container.use().get(url: '/guilds/$id/preview'); return GuildPreview.from( guild: this, @@ -568,8 +560,7 @@ class Guild { /// await member.unban(); /// ``` Future ban (Snowflake memberId, { String? reason }) async { - Http http = ioc.singleton(Service.http); - Response response = await http.destroy(url: '/guilds/$id/bans/$memberId'); + Response response = await container.use().destroy(url: '/guilds/$id/bans/$memberId'); return response.statusCode == 200; } diff --git a/lib/src/api/guilds/guild_member.dart b/lib/src/api/guilds/guild_member.dart index 5ee9e6a9..6c773185 100644 --- a/lib/src/api/guilds/guild_member.dart +++ b/lib/src/api/guilds/guild_member.dart @@ -1,9 +1,11 @@ import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/guild_role_manager.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class GuildMember { +class GuildMember with Container { User _user; String? _nickname; ImageFormater? _avatar; @@ -51,9 +53,8 @@ class GuildMember { /// await member.setUsername('John Doe'); /// ``` Future setUsername (String name) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/${guild.id}/members/${user.id}", payload: { 'nick': name }); + Response response = await container.use().patch(url: "/guilds/${guild.id}/members/${user.id}", payload: { 'nick': name }); if (response.statusCode == 200) { _nickname = name; } @@ -72,9 +73,8 @@ class GuildMember { /// ``` Future timeout (DateTime expiration) async { // @Todo add ADMINISTRATOR permission or is the owner of the guild constraint - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: '/guilds/${guild.id}/members/${user.id}', payload: { 'communication_disabled_until': expiration.toIso8601String() }); + Response response = await container.use().patch(url: '/guilds/${guild.id}/members/${user.id}', payload: { 'communication_disabled_until': expiration.toIso8601String() }); if (response.statusCode == 200 || response.statusCode == 204) { _timeoutDuration = expiration; } @@ -87,9 +87,8 @@ class GuildMember { /// await member.removeTimeout(); /// ``` Future removeTimeout () async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: '/guilds/${guild.id}/members/${user.id}', payload: { 'communication_disabled_until': null }); + Response response = await container.use().patch(url: '/guilds/${guild.id}/members/${user.id}', payload: { 'communication_disabled_until': null }); if (response.statusCode == 200 || response.statusCode == 204) { _timeoutDuration = null; } @@ -108,9 +107,8 @@ class GuildMember { /// await member.ban(count: 7); /// ``` Future ban ({ int? count, String? reason }) async { - Http http = ioc.singleton(Service.http); - Response response = await http.put(url: "/guilds/${guild.id}/bans/${user.id}", payload: { + Response response = await container.use().put(url: "/guilds/${guild.id}/bans/${user.id}", payload: { 'delete_message_days': count, 'reason': reason }); @@ -127,8 +125,7 @@ class GuildMember { /// await member.removeTimeout(); /// ``` Future kick ({ int? count, String? reason }) async { - Http http = ioc.singleton(Service.http); - await http.destroy(url: "/guilds/${guild.id}/members/${user.id}"); + await container.use().destroy(url: "/guilds/${guild.id}/members/${user.id}"); } /// ### Returns whether of this is a bot diff --git a/lib/src/api/guilds/guild_member_reaction.dart b/lib/src/api/guilds/guild_member_reaction.dart index e90cf2e7..b7367f65 100644 --- a/lib/src/api/guilds/guild_member_reaction.dart +++ b/lib/src/api/guilds/guild_member_reaction.dart @@ -1,11 +1,12 @@ import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/src/api/emoji.dart'; import 'package:mineral/src/api/managers/guild_member_reaction_manager.dart'; import 'package:mineral/src/api/messages/partial_message.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class GuildMemberReaction { +class GuildMemberReaction with Container { final GuildMemberReactionManager _manager; final PartialEmoji _partialEmoji; final PartialMessage _message; @@ -14,13 +15,11 @@ class GuildMemberReaction { GuildMemberReaction(this._manager, this._partialEmoji, this._message, this.user); Future remove () async { - Http http = ioc.singleton(Service.http); - String _emoji = _partialEmoji is Emoji - ? '${_partialEmoji.label}:${_partialEmoji.id}' - : _partialEmoji.label; + ? '${_partialEmoji.label}:${_partialEmoji.id}' + : _partialEmoji.label; - Response response = await http.destroy(url: '/channels/${_message.channel.id}/messages/${_message.id}/reactions/$_emoji/${user.id}'); + Response response = await container.use().destroy(url: '/channels/${_message.channel.id}/messages/${_message.id}/reactions/$_emoji/${user.id}'); if (response.statusCode == 200) { _manager.reactions.remove(_emoji); } diff --git a/lib/src/api/guilds/guild_preview.dart b/lib/src/api/guilds/guild_preview.dart index eea2bc7a..f4e2e486 100644 --- a/lib/src/api/guilds/guild_preview.dart +++ b/lib/src/api/guilds/guild_preview.dart @@ -1,5 +1,6 @@ -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/sticker.dart'; class GuildPreview { diff --git a/lib/src/api/guilds/guild_scheduled_event.dart b/lib/src/api/guilds/guild_scheduled_event.dart index 53368b32..d8bd6ef5 100644 --- a/lib/src/api/guilds/guild_scheduled_event.dart +++ b/lib/src/api/guilds/guild_scheduled_event.dart @@ -1,4 +1,5 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/channel_manager.dart'; import 'package:mineral/src/api/managers/member_manager.dart'; diff --git a/lib/src/api/interactions/button_interaction.dart b/lib/src/api/interactions/button_interaction.dart index 0ca80d86..a6a313bf 100644 --- a/lib/src/api/interactions/button_interaction.dart +++ b/lib/src/api/interactions/button_interaction.dart @@ -1,6 +1,7 @@ import 'dart:core'; -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; class ButtonInteraction extends Interaction { Snowflake _customId; @@ -9,6 +10,7 @@ class ButtonInteraction extends Interaction { ButtonInteraction( super._id, + super._label, super._applicationId, super._version, super._typeId, @@ -28,6 +30,7 @@ class ButtonInteraction extends Interaction { factory ButtonInteraction.fromPayload(dynamic payload) { return ButtonInteraction( payload['id'], + null, payload['application_id'], payload['version'], payload['type'], diff --git a/lib/src/api/interactions/command_interaction.dart b/lib/src/api/interactions/command_interaction.dart index e52a52f6..a59fb733 100644 --- a/lib/src/api/interactions/command_interaction.dart +++ b/lib/src/api/interactions/command_interaction.dart @@ -1,16 +1,19 @@ import 'dart:core'; -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class CommandInteraction extends Interaction { +class CommandInteraction extends Interaction with Container { String _identifier; Snowflake? _channelId; - Map data = {}; + Map _data = {}; + Map _params = {}; CommandInteraction( super._id, + super._label, super._applicationId, super._version, super._typeId, @@ -19,10 +22,14 @@ class CommandInteraction extends Interaction { super._guildId, this._identifier, this._channelId, + this._data, + this._params, ); String get identifier => _identifier; TextBasedChannel? get channel => guild?.channels.cache.get(_channelId); + Map get data => _data; + Map get params => _params; /// ### Returns an instance of [PartialTextChannel] or null if the command has the designed option /// @@ -31,7 +38,7 @@ class CommandInteraction extends Interaction { /// Channel? channel = interaction.getChannel('option_name'); /// ``` T? getChannel (String optionName) { - return guild?.channels.cache.get(data[optionName]?['value']); + return guild?.channels.cache.get(params[optionName]); } /// ### Returns an instance of [PartialTextChannel] if the command has the designed option @@ -41,7 +48,7 @@ class CommandInteraction extends Interaction { /// final Channel channel = interaction.getChannelOrFail('option_name'); /// ``` T getChannelOrFail (String optionName) { - return guild!.channels.cache.getOrFail(data[optionName]['value']); + return guild!.channels.cache.getOrFail(params[optionName]); } /// ### Returns an [int] if the command has the designed option @@ -51,7 +58,7 @@ class CommandInteraction extends Interaction { /// int? value = interaction.getInteger('option_name'); /// ``` int? getInteger (String optionName) { - return data[optionName]?['value']; + return params[optionName]; } /// ### Returns an [int] or null if the command has the designed option @@ -61,7 +68,7 @@ class CommandInteraction extends Interaction { /// final int value = interaction.getIntegerOrFail('option_name'); /// ``` int getIntegerOrFail (String optionName) { - return data[optionName]['value']; + return params[optionName]; } /// ### Returns an [String] or null if the command has the designed option @@ -71,7 +78,7 @@ class CommandInteraction extends Interaction { /// String? str = interaction.getString('option_name'); /// ``` String? getString (String optionName) { - return data[optionName]?['value']; + return params[optionName].toString(); } /// ### Returns an [String] if the command has the designed option @@ -80,8 +87,8 @@ class CommandInteraction extends Interaction { /// ```dart /// final String str = interaction.getStringOrFail('option_name'); /// ``` - String? getStringOrFail (String optionName) { - return data[optionName]?['value']; + String getStringOrFail (String optionName) { + return params[optionName].toString(); } /// ### Returns an instance of [GuildMember] if the command has the designed option @@ -91,7 +98,7 @@ class CommandInteraction extends Interaction { /// GuildMember? member = interaction.getMember('option_name'); /// ``` GuildMember? getMember (String optionName) { - return guild?.members.cache.get(data[optionName]?['value']); + return guild?.members.cache.get(params[optionName]); } /// ### Returns an instance of [GuildMember] if the command has the designed option @@ -101,7 +108,7 @@ class CommandInteraction extends Interaction { /// final GuildMember member = interaction.getMemberOrFail('option_name'); /// ``` GuildMember getMemberOrFail (String optionName) { - return guild!.members.cache.getOrFail(data[optionName]['value']); + return guild!.members.cache.getOrFail(params[optionName]); } /// ### Returns an instance of [User] or null if the command has the designed option @@ -111,8 +118,8 @@ class CommandInteraction extends Interaction { /// User? user = interaction.getUser('option_name'); /// ``` User? getUser (String optionName) { - final MineralClient client = ioc.singleton(Service.client); - return client.users.cache.get(data[optionName]?['value']); + final MineralClient client = container.use(); + return client.users.cache.get(params[optionName]); } /// ### Returns an instance of [User] if the command has the designed option @@ -122,8 +129,8 @@ class CommandInteraction extends Interaction { /// final User user = interaction.getUserOrFail('option_name'); /// ``` User getUserOrFail (String optionName) { - final MineralClient client = ioc.singleton(Service.client); - return client.users.cache.getOrFail(data[optionName]['value']); + final MineralClient client = container.use(); + return client.users.cache.getOrFail(params[optionName]); } /// ### Returns an [bool] or null if the command has the designed option @@ -133,7 +140,7 @@ class CommandInteraction extends Interaction { /// bool? boolean = interaction.getBoolean('option_name'); /// ``` bool? getBoolean (String optionName) { - return data[optionName]?['value']; + return params[optionName]; } /// ### Returns an [bool] if the command has the designed option @@ -143,7 +150,7 @@ class CommandInteraction extends Interaction { /// final bool boolean = interaction.getBooleanOrFail('option_name'); /// ``` bool getBooleanOrFail (String optionName) { - return data[optionName]['value']; + return params[optionName]; } /// ### Returns an instance of [Role] or null if the command has the designed option @@ -153,7 +160,7 @@ class CommandInteraction extends Interaction { /// Role? role = interaction.getRole('option_name'); /// ``` Role? getRole (String optionName) { - return guild?.roles.cache.get(data[optionName]?['value']); + return guild?.roles.cache.get(params[optionName]); } /// ### Returns an instance of [Role] if the command has the designed option @@ -163,7 +170,7 @@ class CommandInteraction extends Interaction { /// Role? role = interaction.getRole('option_name'); /// ``` Role getRoleOrFail (String optionName) { - return guild!.roles.cache.getOrFail(data[optionName]['value']); + return guild!.roles.cache.getOrFail(params[optionName]); } /// ### Returns an [T] if the command has the designed option @@ -174,7 +181,7 @@ class CommandInteraction extends Interaction { /// int? value = interaction.getChoice('option_name'); /// ``` T? getChoice (String optionName) { - return data[optionName]?['value']; + return params[optionName]; } /// ### Returns an value if the command has the designed option @@ -184,12 +191,28 @@ class CommandInteraction extends Interaction { /// dynamic mentionable = interaction.getMentionable('option_name'); /// ``` dynamic getMentionable (String optionName) { - return data[optionName]?['value']; + return params[optionName]; } - factory CommandInteraction.from({ required dynamic payload }) { + factory CommandInteraction.fromPayload(dynamic payload) { + final Map params = {}; + void walk (List options) { + for (final option in options) { + if (option['options'] != null) { + walk(option['options']); + } else { + params.putIfAbsent(option['name'], () => option['value']); + } + } + } + + if (payload['data']?['options'] != null) { + walk(payload['data']['options']); + } + return CommandInteraction( payload['id'], + payload['data']['name'], payload['application_id'], payload['version'], payload['type'], @@ -198,6 +221,8 @@ class CommandInteraction extends Interaction { payload['guild_id'], payload['data']['name'], payload['channel_id'], + payload['data'], + params ); } } diff --git a/lib/src/api/interactions/context_menu_interaction.dart b/lib/src/api/interactions/context_menu_interaction.dart new file mode 100644 index 00000000..487944e5 --- /dev/null +++ b/lib/src/api/interactions/context_menu_interaction.dart @@ -0,0 +1,24 @@ +import 'package:mineral/core/api.dart'; + +class ContextMenuInteraction extends Interaction { + final int _type; + + ContextMenuInteraction( + this._type, + super._id, + super._label, + super._applicationId, + super._version, + super._typeId, + super._token, + super._userId, + super._guildId, + ); + + ContextMenuType get typeMenu => ContextMenuType.values.firstWhere((element) => element.value == _type); + + Object get toJson => { + 'name': label, + 'type': _type + }; +} \ No newline at end of file diff --git a/lib/src/api/interactions/context_message_interaction.dart b/lib/src/api/interactions/context_message_interaction.dart index aff95f96..f8e95af9 100644 --- a/lib/src/api/interactions/context_message_interaction.dart +++ b/lib/src/api/interactions/context_message_interaction.dart @@ -1,19 +1,23 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; +import 'package:mineral/src/api/interactions/context_menu_interaction.dart'; -class ContextMessageInteraction extends Interaction { +class ContextMessageInteraction extends ContextMenuInteraction { Snowflake _channelId; Message _message; ContextMessageInteraction( + this._channelId, + this._message, + super._type, super._id, + super._label, super._applicationId, super._version, super._typeId, super._token, super._userId, super._guildId, - this._channelId, - this._message, ); Message get message => _message; @@ -21,15 +25,17 @@ class ContextMessageInteraction extends Interaction { factory ContextMessageInteraction.from({ required Message message, required dynamic payload }) { return ContextMessageInteraction( + payload['channel_id'], + message, + payload['type'], payload['id'], + payload['data']['name'], payload['application_id'], payload['version'], payload['type'], payload['token'], payload['member']?['user']?['id'], payload['guild_id'], - payload['guild_id'], - message, ); } } diff --git a/lib/src/api/interactions/context_user_interaction.dart b/lib/src/api/interactions/context_user_interaction.dart index e31e6b58..63ad8455 100644 --- a/lib/src/api/interactions/context_user_interaction.dart +++ b/lib/src/api/interactions/context_user_interaction.dart @@ -1,19 +1,23 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; +import 'package:mineral/src/api/interactions/context_menu_interaction.dart'; -class ContextUserInteraction extends Interaction { +class ContextUserInteraction extends ContextMenuInteraction { Snowflake? _targetId; Snowflake? _channelId; ContextUserInteraction( + this._targetId, + this._channelId, + super._type, super._id, + super._label, super._applicationId, super._version, - super._type, + super._typeId, super._token, super._user, super._guild, - this._targetId, - this._channelId, ); GuildMember? get target => guild?.members.cache.get(_targetId); @@ -21,15 +25,17 @@ class ContextUserInteraction extends Interaction { factory ContextUserInteraction.from({ required dynamic payload }) { return ContextUserInteraction( + payload['target_id'], + payload['channel_id'], + payload['type'], payload['id'], + payload['name'], payload['application_id'], payload['version'], payload['type'], payload['token'], payload['member']?['user']?['id'], payload['guild_id'], - payload['target_id'], - payload['channel_id'] ); } } diff --git a/lib/src/api/interactions/interaction.dart b/lib/src/api/interactions/interaction.dart index 53e7fda6..31641321 100644 --- a/lib/src/api/interactions/interaction.dart +++ b/lib/src/api/interactions/interaction.dart @@ -1,5 +1,8 @@ -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; +import 'package:mineral/framework.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; enum InteractionCallbackType { pong(1), @@ -14,8 +17,9 @@ enum InteractionCallbackType { const InteractionCallbackType(this.value); } -class Interaction { +class Interaction with Container { Snowflake _id; + String? _label; Snowflake _applicationId; int _version; int _typeId; @@ -23,18 +27,19 @@ class Interaction { Snowflake? _userId; Snowflake? _guildId; - Interaction(this._id, this._applicationId, this._version, this._typeId, this._token, this._userId, this._guildId); + Interaction(this._id, this._label, this._applicationId, this._version, this._typeId, this._token, this._userId, this._guildId); Snowflake get id => _id; + String? get label => _label; Snowflake get applicationId => _applicationId; int get version => _version; InteractionType get type => InteractionType.values.firstWhere((element) => element.value == _typeId); String get token => _token; - Guild? get guild => ioc.singleton(Service.client).guilds.cache.get(_guildId); + Guild? get guild => container.use().guilds.cache.get(_guildId); User get user => _guildId != null ? guild!.members.cache.getOrFail(_userId).user - : ioc.singleton(Service.client).users.cache.getOrFail(_userId); + : container.use().users.cache.getOrFail(_userId); GuildMember? get member => guild?.members.cache.get(_userId); @@ -45,8 +50,6 @@ class Interaction { /// await interaction.reply(content: 'Hello ${interaction.user.username}'); /// ``` Future reply ({ String? content, List? embeds, List? components, bool? tts, bool? private }) async { - Http http = ioc.singleton(Service.http); - List embedList = []; if (embeds != null) { for (EmbedBuilder element in embeds) { @@ -61,7 +64,7 @@ class Interaction { } } - await http.post(url: "/interactions/$id/$token/callback", payload: { + await container.use().post(url: "/interactions/$id/$token/callback", payload: { 'type': InteractionCallbackType.channelMessageWithSource.value, 'data': { 'tts': tts ?? false, @@ -84,9 +87,7 @@ class Interaction { /// await interaction.modal(modal); /// ``` Future modal (ModalBuilder modal) async { - Http http = ioc.singleton(Service.http); - - await http.post(url: "/interactions/$id/$token/callback", payload: { + await container.use().post(url: "/interactions/$id/$token/callback", payload: { 'type': InteractionCallbackType.modal.value, 'data': modal.toJson(), }); @@ -95,6 +96,7 @@ class Interaction { factory Interaction.from({ required dynamic payload }) { return Interaction( payload['id'], + null, payload['application_id'], payload['version'], payload['type'], diff --git a/lib/src/api/interactions/modal_interaction.dart b/lib/src/api/interactions/modal_interaction.dart index 560b177f..de7a6c16 100644 --- a/lib/src/api/interactions/modal_interaction.dart +++ b/lib/src/api/interactions/modal_interaction.dart @@ -1,6 +1,7 @@ import 'dart:core'; -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; class ModalInteraction extends Interaction { Snowflake _customId; @@ -10,6 +11,7 @@ class ModalInteraction extends Interaction { ModalInteraction( super._id, + super._label, super._applicationId, super._version, super._typeId, @@ -34,6 +36,7 @@ class ModalInteraction extends Interaction { factory ModalInteraction.from({ required dynamic payload }) { return ModalInteraction( payload['id'], + null, payload['application_id'], payload['version'], payload['type'], diff --git a/lib/src/api/interactions/select_menu_interaction.dart b/lib/src/api/interactions/select_menu_interaction.dart index 2d3d2a78..88e2df52 100644 --- a/lib/src/api/interactions/select_menu_interaction.dart +++ b/lib/src/api/interactions/select_menu_interaction.dart @@ -1,6 +1,7 @@ import 'dart:core'; -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; class SelectMenuInteraction extends Interaction { Message? _message; @@ -11,6 +12,7 @@ class SelectMenuInteraction extends Interaction { SelectMenuInteraction( super._id, + super._label, super._applicationId, super._version, super._type, @@ -47,6 +49,7 @@ class SelectMenuInteraction extends Interaction { factory SelectMenuInteraction.from({ required Message? message, required dynamic payload }) { return SelectMenuInteraction( payload['id'], + null, payload['application_id'], payload['version'], payload['type'], diff --git a/lib/src/api/managers/cache_manager.dart b/lib/src/api/managers/cache_manager.dart index 4f8d8fd4..bac9fe32 100644 --- a/lib/src/api/managers/cache_manager.dart +++ b/lib/src/api/managers/cache_manager.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; abstract class CacheManager { final Map _cache = {}; diff --git a/lib/src/api/managers/channel_manager.dart b/lib/src/api/managers/channel_manager.dart index b09f700f..de6b4429 100644 --- a/lib/src/api/managers/channel_manager.dart +++ b/lib/src/api/managers/channel_manager.dart @@ -1,23 +1,25 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/channels/partial_channel.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class ChannelManager extends CacheManager { +class ChannelManager extends CacheManager with Container { final Snowflake _guildId; ChannelManager(this._guildId); - Guild get guild => ioc.singleton(Service.client).guilds.cache.getOrFail(_guildId); + Guild get guild => container.use().guilds.cache.getOrFail(_guildId); Future> sync () async { - Http http = ioc.singleton(Service.http); cache.clear(); - Response response = await http.get(url: "/guilds/$_guildId/channels"); + Response response = await container.use().get(url: "/guilds/$_guildId/channels"); dynamic payload = jsonDecode(response.body); for (dynamic element in payload) { @@ -32,10 +34,7 @@ class ChannelManager extends CacheManager { } Future create (ChannelBuilder builder) async { - Http http = ioc.singleton(Service.http); - - - Response response = await http.post(url: '/guilds/$_guildId/channels', payload: builder.payload); + Response response = await container.use().post(url: '/guilds/$_guildId/channels', payload: builder.payload); dynamic payload = jsonDecode(response.body); final GuildChannel? channel = ChannelWrapper.create(payload); diff --git a/lib/src/api/managers/command_manager.dart b/lib/src/api/managers/command_manager.dart index 3fc552c1..35428fb2 100644 --- a/lib/src/api/managers/command_manager.dart +++ b/lib/src/api/managers/command_manager.dart @@ -1,12 +1,12 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; -import 'package:mineral/src/internal/entities/command.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class CommandManager extends CacheManager { +class CommandManager extends CacheManager with Container { final Snowflake? _guildId; CommandManager(this._guildId); - Guild? get guild => ioc.singleton(Service.client).guilds.cache.get(_guildId); + Guild? get guild => container.use().guilds.cache.get(_guildId); } diff --git a/lib/src/api/managers/emoji_manager.dart b/lib/src/api/managers/emoji_manager.dart index 5e601d41..1db6e0f7 100644 --- a/lib/src/api/managers/emoji_manager.dart +++ b/lib/src/api/managers/emoji_manager.dart @@ -1,20 +1,20 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/exception.dart'; -import 'package:mineral/helper.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; +import 'package:mineral/src/helper.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class EmojiManager extends CacheManager { +class EmojiManager extends CacheManager with Container { late final Guild guild; Future> sync () async { - Http http = ioc.singleton(Service.http); cache.clear(); - Response response = await http.get(url: "/guilds/${guild.id}/emojis"); + Response response = await container.use().get(url: "/guilds/${guild.id}/emojis"); dynamic payload = jsonDecode(response.body); for(dynamic element in payload) { @@ -36,10 +36,9 @@ class EmojiManager extends CacheManager { throw EmptyParameterException(cause: 'Parameter "path" cannot be null or empty'); } - Http http = ioc.singleton(Service.http); String image = await Helper.getPicture(path); - Response response = await http.post(url: "/guilds/${guild.id}/emojis", payload: { + Response response = await container.use().post(url: "/guilds/${guild.id}/emojis", payload: { 'name': label, 'image': image, 'roles': roles ?? [], @@ -47,7 +46,6 @@ class EmojiManager extends CacheManager { Emoji emoji = Emoji.from( memberManager: guild.members, - //roleManager: guild!.roles, emojiManager: this, payload: jsonDecode(response.body), ); diff --git a/lib/src/api/managers/forum_discussion_manager.dart b/lib/src/api/managers/forum_discussion_manager.dart index cf8b44af..304a6d54 100644 --- a/lib/src/api/managers/forum_discussion_manager.dart +++ b/lib/src/api/managers/forum_discussion_manager.dart @@ -1,14 +1,14 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; +import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; import 'package:mineral/src/api/channels/guild_channel.dart'; -import 'package:mineral/src/api/channels/thread_channel.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -import 'package:mineral/core.dart'; - -class ForumDiscussionManager extends CacheManager { +class ForumDiscussionManager extends CacheManager with Container { final Snowflake _channelId; ForumDiscussionManager(this._channelId); @@ -17,9 +17,7 @@ class ForumDiscussionManager extends CacheManager { /// Warning guild requires [GuildFeature.community] feature /// ``` Future create (String label, MessageBuilder message, { int? archiveDuration, int? rateLimit, List? tags, bool? pin }) async { - Http http = ioc.singleton(Service.http); - - Response response = await http.post(url: '/channels/$_channelId/threads', payload: { + Response response = await container.use().post(url: '/channels/$_channelId/threads', payload: { 'name': label, 'auto_archive_duration': archiveDuration, 'rate_limit_per_user': rateLimit, diff --git a/lib/src/api/managers/guild_manager.dart b/lib/src/api/managers/guild_manager.dart index 1a45986e..f8285fbe 100644 --- a/lib/src/api/managers/guild_manager.dart +++ b/lib/src/api/managers/guild_manager.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; class GuildManager extends CacheManager { diff --git a/lib/src/api/managers/guild_member_reaction_manager.dart b/lib/src/api/managers/guild_member_reaction_manager.dart index 5bd8e718..c6ea98dd 100644 --- a/lib/src/api/managers/guild_member_reaction_manager.dart +++ b/lib/src/api/managers/guild_member_reaction_manager.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/src/api/guilds/guild_member_reaction.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; import 'package:mineral/src/api/messages/partial_message.dart'; diff --git a/lib/src/api/managers/guild_role_manager.dart b/lib/src/api/managers/guild_role_manager.dart index 8a0a613c..5ee202ce 100644 --- a/lib/src/api/managers/guild_role_manager.dart +++ b/lib/src/api/managers/guild_role_manager.dart @@ -1,18 +1,20 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/exception.dart'; -import 'package:mineral/helper.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; +import 'package:mineral/src/helper.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class GuildRoleManager extends CacheManager { +class GuildRoleManager extends CacheManager with Container { final Snowflake _guildId; GuildRoleManager(this._guildId); - Guild get guild => ioc.singleton(Service.client).guilds.cache.getOrFail(_guildId); + Guild get guild => container.use().guilds.cache.getOrFail(_guildId); Role get everyone => cache.findOrFail((role) => role.label == '@everyone'); /// Synchronise the cache from the Discord API @@ -22,10 +24,9 @@ class GuildRoleManager extends CacheManager { /// await guild.roles.sync(); /// ``` Future> sync () async { - Http http = ioc.singleton(Service.http); cache.clear(); - Response response = await http.get(url: "/guilds/$_guildId/roles"); + Response response = await container.use().get(url: "/guilds/$_guildId/roles"); dynamic payload = jsonDecode(response.body); for(dynamic element in payload) { @@ -60,8 +61,7 @@ class GuildRoleManager extends CacheManager { String? _icon = icon != null ? await Helper.getPicture(icon) : null; int? _permissions = permissions != null ? Helper.reduceRolePermissions(permissions) : null; - Http http = ioc.singleton(Service.http); - Response response = await http.post(url: "/guilds/$_guildId}/roles", payload: { + Response response = await container.use().post(url: "/guilds/$_guildId}/roles", payload: { 'name': label, 'color': color != null ? Helper.toRgbColor(color) : null, 'hoist': hoist ?? false, diff --git a/lib/src/api/managers/guild_scheduled_event_manager.dart b/lib/src/api/managers/guild_scheduled_event_manager.dart index 7109c21a..a651a839 100644 --- a/lib/src/api/managers/guild_scheduled_event_manager.dart +++ b/lib/src/api/managers/guild_scheduled_event_manager.dart @@ -1,17 +1,16 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class GuildScheduledEventManager extends CacheManager { +class GuildScheduledEventManager extends CacheManager with Container { late final Guild guild; Future> sync() async { - final Http http = ioc.singleton(Service.http); - - Response response = await http.get(url: "/guilds/${guild.id}/scheduled-events"); + Response response = await container.use().get(url: "/guilds/${guild.id}/scheduled-events"); if (response.statusCode == 200) { cache.clear(); diff --git a/lib/src/api/managers/guild_webhook_manager.dart b/lib/src/api/managers/guild_webhook_manager.dart index 8ecdddbb..5d17af58 100644 --- a/lib/src/api/managers/guild_webhook_manager.dart +++ b/lib/src/api/managers/guild_webhook_manager.dart @@ -1,19 +1,20 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; import 'package:mineral/src/api/managers/webhook_manager.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class GuildWebhookManager extends CacheManager { +class GuildWebhookManager extends CacheManager with Container { late final Guild guild; GuildWebhookManager(); Future> sync () async { - Http http = ioc.singleton(Service.http); - Response response = await http.get(url: "/guilds/${guild.id}/webhooks"); + Response response = await container.use().get(url: "/guilds/${guild.id}/webhooks"); for (dynamic element in jsonDecode(response.body)) { Webhook webhook = Webhook.from(payload: element); diff --git a/lib/src/api/managers/member_manager.dart b/lib/src/api/managers/member_manager.dart index 51c74677..32a4b953 100644 --- a/lib/src/api/managers/member_manager.dart +++ b/lib/src/api/managers/member_manager.dart @@ -1,18 +1,18 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class MemberManager extends CacheManager { +class MemberManager extends CacheManager with Container { late final Guild _guild; Guild get guild => _guild; Future> sync () async { - Http http = ioc.singleton(Service.http); - - Response response = await http.get(url: "/guilds/${_guild.id}/members"); + Response response = await container.use().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)); diff --git a/lib/src/api/managers/member_role_manager.dart b/lib/src/api/managers/member_role_manager.dart index 9d942abc..972f0d46 100644 --- a/lib/src/api/managers/member_role_manager.dart +++ b/lib/src/api/managers/member_role_manager.dart @@ -1,13 +1,15 @@ import 'dart:convert'; 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/core/api.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; +import 'package:mineral/src/api/managers/guild_role_manager.dart'; import 'package:mineral/src/exceptions/not_exist.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class MemberRoleManager extends CacheManager { +class MemberRoleManager extends CacheManager with Container { late final Guild _guild; GuildRoleManager manager; Snowflake memberId; @@ -33,9 +35,7 @@ class MemberRoleManager extends CacheManager { /// ```dart /// await member.roles.add('446556480850755604', reason: 'I love this user'); /// ``` - Future add (Snowflake id, {String? reason}) async { - Http http = ioc.singleton(Service.http); - Role? role = manager.cache.get(id); + Future add (Snowflake id, {String? reason}) async { Role? role = manager.cache.get(id); if(role == null) { throw NotExist(prefix: 'role not exist', cause: 'You can\'t add a role that don\'t exist!'); @@ -46,7 +46,7 @@ class MemberRoleManager extends CacheManager { headers.putIfAbsent('X-Audit-Log-Reason', () => reason); } - Response response = await http.put( + Response response = await container.use().put( url: '/guilds/${manager.guild.id}/members/$memberId/roles/$id', payload: {}, headers: headers @@ -75,14 +75,12 @@ class MemberRoleManager extends CacheManager { /// await member.roles.remove('446556480850755604', reason: 'Hello, World!'); /// ``` Future remove (Snowflake id, {String? reason}) async { - Http http = ioc.singleton(Service.http); - Map headers = {}; if(reason != null) { headers.putIfAbsent('X-Audit-Log-Reason', () => reason); } - Response response = await http.destroy( + Response response = await container.use().destroy( url: '/guilds/${manager.guild.id}/members/$memberId/roles/$id', headers: headers ); @@ -116,9 +114,7 @@ class MemberRoleManager extends CacheManager { } Future> sync () async { - Http http = ioc.singleton(Service.http); - - Response response = await http.get(url: "/guilds/${manager.guild.id}/members/$memberId"); + Response response = await container.use().get(url: "/guilds/${manager.guild.id}/members/$memberId"); if(response.statusCode == 200) { cache.clear(); dynamic payload = jsonDecode(response.body)['roles']; diff --git a/lib/src/api/managers/message_manager.dart b/lib/src/api/managers/message_manager.dart index 9d626ae9..bbfebb2a 100644 --- a/lib/src/api/managers/message_manager.dart +++ b/lib/src/api/managers/message_manager.dart @@ -1,19 +1,19 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; import 'package:mineral/src/api/messages/partial_message.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class MessageManager extends CacheManager { +class MessageManager extends CacheManager with Container { late final TextBasedChannel channel; Future> sync () async { - Http http = ioc.singleton(Service.http); cache.clear(); - Response response = await http.get(url: "/channels/${channel.id}/messages"); + Response response = await container.use().get(url: "/channels/${channel.id}/messages"); dynamic payload = jsonDecode(response.body); for(dynamic element in payload) { diff --git a/lib/src/api/managers/message_reaction_manager.dart b/lib/src/api/managers/message_reaction_manager.dart index bd3b6708..e2727ece 100644 --- a/lib/src/api/managers/message_reaction_manager.dart +++ b/lib/src/api/managers/message_reaction_manager.dart @@ -1,13 +1,15 @@ import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; -import 'package:mineral/src/api/channels/partial_channel.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; +import 'package:mineral/src/api/builders/emoji_builder.dart'; import 'package:mineral/src/api/guilds/guild_member_reaction.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; import 'package:mineral/src/api/managers/guild_member_reaction_manager.dart'; import 'package:mineral/src/api/messages/partial_message.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class MessageReactionManager extends CacheManager { +class MessageReactionManager extends CacheManager with Container { Map users = {}; final C _channel; @@ -16,14 +18,13 @@ class MessageReactionManager MessageReactionManager(this._channel); Future add (EmojiBuilder emojiBuilder) async { - Http http = ioc.singleton(Service.http); - MineralClient client = ioc.singleton(Service.client); + MineralClient client = container.use(); String _emoji = emojiBuilder.emoji is Emoji ? '${emojiBuilder.emoji.label}:${emojiBuilder.emoji.id}' : emojiBuilder.emoji.label; - Response response = await http.put(url: '/channels/${_channel.id}/messages/${message.id}/reactions/$_emoji/@me', payload: {}); + Response response = await container.use().put(url: '/channels/${_channel.id}/messages/${message.id}/reactions/$_emoji/@me', payload: {}); if (response.statusCode == 204) { final key = emojiBuilder.emoji.id != '' ? emojiBuilder.emoji.id : emojiBuilder.emoji.label; @@ -47,9 +48,7 @@ class MessageReactionManager } Future removeAll () async { - Http http = ioc.singleton(Service.http); - - Response response = await http.destroy(url: '/channels/${message.channel.id}/messages/${message.id}/reactions'); + Response response = await container.use().destroy(url: '/channels/${message.channel.id}/messages/${message.id}/reactions'); if (response.statusCode == 200) { cache.clear(); } diff --git a/lib/src/api/managers/moderation_rule_manager.dart b/lib/src/api/managers/moderation_rule_manager.dart index 4883ae28..0c5bdb2a 100644 --- a/lib/src/api/managers/moderation_rule_manager.dart +++ b/lib/src/api/managers/moderation_rule_manager.dart @@ -1,11 +1,13 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class ModerationRuleManager extends CacheManager { +class ModerationRuleManager extends CacheManager with Container { final Snowflake _guildId; ModerationRuleManager(this._guildId); @@ -26,9 +28,7 @@ class ModerationRuleManager extends CacheManager { /// ); /// ``` Future create (ModerationRulesBuilder builder) async { - Http http = ioc.singleton(Service.http); - - Response response = await http.post(url: "/guilds/$_guildId/auto-moderation/rules", payload: { + Response response = await container.use().post(url: "/guilds/$_guildId/auto-moderation/rules", payload: { 'name': builder.label, 'event_type': builder.moderationEventType.value, 'trigger_type': builder.moderationTriggerType.value, @@ -49,8 +49,7 @@ class ModerationRuleManager extends CacheManager { /// final rules = await guild.moderationRules.sync(); /// ``` Future>sync () async { - Http http = ioc.singleton(Service.http); - Response response = await http.get(url: "/guilds/$_guildId/auto-moderation/rules"); + Response response = await container.use().get(url: "/guilds/$_guildId/auto-moderation/rules"); for (final payload in jsonDecode(response.body)) { final ModerationRule rule = ModerationRule.fromPayload(payload); diff --git a/lib/src/api/managers/permission_overwrite_manager.dart b/lib/src/api/managers/permission_overwrite_manager.dart index a2265c40..7c2a88df 100644 --- a/lib/src/api/managers/permission_overwrite_manager.dart +++ b/lib/src/api/managers/permission_overwrite_manager.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; class PermissionOverwriteManager extends CacheManager { diff --git a/lib/src/api/managers/sticker_manager.dart b/lib/src/api/managers/sticker_manager.dart index 8f0e22f0..cf68dd39 100644 --- a/lib/src/api/managers/sticker_manager.dart +++ b/lib/src/api/managers/sticker_manager.dart @@ -1,20 +1,20 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; import 'package:mineral/core.dart'; -import 'package:mineral/helper.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; import 'package:mineral/src/api/sticker.dart'; +import 'package:mineral/src/console.dart'; +import 'package:mineral/src/helper.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class StickerManager extends CacheManager { +class StickerManager extends CacheManager with Container { late final 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)) { - Http http = ioc.singleton(Service.http); - Response response = await http.post(url: "/guilds/${guild.id}/stickers", payload: { + Response response = await container.use().post(url: "/guilds/${guild.id}/stickers", payload: { 'name': name, 'description': description, 'tags': tags, @@ -39,10 +39,9 @@ class StickerManager extends CacheManager { } Future> sync () async { - Http http = ioc.singleton(Service.http); cache.clear(); - Response response = await http.get(url: "/guilds/${guild.id}/stickers"); + Response response = await container.use().get(url: "/guilds/${guild.id}/stickers"); dynamic payload = jsonDecode(response.body); for(dynamic element in payload) { diff --git a/lib/src/api/managers/thread_manager.dart b/lib/src/api/managers/thread_manager.dart index fdd9e123..03874191 100644 --- a/lib/src/api/managers/thread_manager.dart +++ b/lib/src/api/managers/thread_manager.dart @@ -1,27 +1,26 @@ - import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/src/api/channels/thread_channel.dart'; -import 'package:mineral/src/api/managers/cache_manager.dart'; - import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; +import 'package:mineral/framework.dart'; +import 'package:mineral/src/api/managers/cache_manager.dart'; import 'package:mineral/src/internal/extensions/mineral_client.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class ThreadManager extends CacheManager { +class ThreadManager extends CacheManager with Container { final Snowflake _guildId; ThreadManager(this._guildId); /// Get [Guild] from [Ioc] - Guild get guild => ioc.singleton(Service.client).guilds.cache.getOrFail(_guildId); + Guild get guild => container.use().guilds.cache.getOrFail(_guildId); Future> sync () async { - Http http = ioc.singleton(Service.http); cache.clear(); - Response response = await http.get(url: "/guilds/$_guildId/threads/active"); + Response response = await container.use().get(url: "/guilds/$_guildId/threads/active"); dynamic payload = jsonDecode(response.body); for (dynamic element in payload) { @@ -33,7 +32,7 @@ class ThreadManager extends CacheManager { } Future create ({ Snowflake? messageId, String? label }) async { - MineralClient client = ioc.singleton(Service.client); + MineralClient client = container.use(); return await client.createChannel(_guildId, ChannelBuilder({ 'name': label, 'auto_archive_duration': '60', diff --git a/lib/src/api/managers/user_manager.dart b/lib/src/api/managers/user_manager.dart index 1b0321aa..c718c787 100644 --- a/lib/src/api/managers/user_manager.dart +++ b/lib/src/api/managers/user_manager.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; class UserManager extends CacheManager { diff --git a/lib/src/api/managers/voice_manager.dart b/lib/src/api/managers/voice_manager.dart index 7fd9597b..430cb65c 100644 --- a/lib/src/api/managers/voice_manager.dart +++ b/lib/src/api/managers/voice_manager.dart @@ -1,9 +1,11 @@ import 'package:http/http.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; +import 'package:mineral/src/console.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class VoiceManager { +class VoiceManager with Container { bool _isDeaf; bool _isMute; bool _isSelfMute; @@ -23,7 +25,7 @@ class VoiceManager { bool get hasVideo => _hasVideo; bool? get hasStream => _hasStream; - Guild get guild => ioc.singleton(Service.client).guilds.cache.getOrFail(_guildId); + Guild get guild => container.use().guilds.cache.getOrFail(_guildId); VoiceChannel? get channel => guild.channels.cache.get(_channelId); GuildMember get member => guild.members.cache.getOrFail(_memberId); @@ -37,9 +39,8 @@ class VoiceManager { /// await member.setMute(true); /// } Future setMute(bool value) async { - final Http http = ioc.singleton(Service.http); - final Response response = await http.patch( + final Response response = await container.use().patch( url: '/guilds/$_guildId/members/$_memberId', payload: {'mute': value} ); @@ -62,8 +63,7 @@ class VoiceManager { /// await member.setDeaf(true); /// } Future setDeaf(bool value) async { - final Http http = ioc.singleton(Service.http); - final Response response = await http.patch( + final Response response = await container.use().patch( url: '/guilds/$_guildId/members/$_memberId', payload: {'deaf': value} ); @@ -104,8 +104,7 @@ class VoiceManager { } Future _updateChannel(Snowflake? channelId) async { - final Http http = ioc.singleton(Service.http); - final Response response = await http.patch( + final Response response = await container.use().patch( url: '/guilds/$_guildId/members/$_memberId', payload: {'channel_id': channelId} ); diff --git a/lib/src/api/managers/webhook_manager.dart b/lib/src/api/managers/webhook_manager.dart index 626847d1..9f022fdd 100644 --- a/lib/src/api/managers/webhook_manager.dart +++ b/lib/src/api/managers/webhook_manager.dart @@ -1,23 +1,24 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; -import 'package:mineral/helper.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/cache_manager.dart'; +import 'package:mineral/src/helper.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class WebhookManager extends CacheManager { +class WebhookManager extends CacheManager with Container { final Snowflake? _channelId; final Snowflake? _guildId; WebhookManager(this._guildId, this._channelId); /// Get [Guild] from [Ioc] - Guild get guild => ioc.singleton(Service.client).guilds.cache.getOrFail(_guildId); + Guild get guild => container.use().guilds.cache.getOrFail(_guildId); Future> sync () async { - Http http = ioc.singleton(Service.http); - Response response = await http.get(url: "/channels/$_channelId/webhooks"); + Response response = await container.use().get(url: "/channels/$_channelId/webhooks"); for (dynamic element in jsonDecode(response.body)) { Webhook webhook = Webhook.from(payload: element); @@ -28,22 +29,13 @@ class WebhookManager extends CacheManager { } Future create ({ required String label, String? avatar }) async { - Http http = ioc.singleton(Service.http); - Response response = await http.post(url: "/channels/$_channelId/webhooks", payload: { + Response response = await container.use().post(url: "/channels/$_channelId/webhooks", payload: { 'name': label, 'avatar': avatar != null ? await Helper.getPicture(avatar) : null }); - if (response.statusCode == 200) { - dynamic payload = jsonDecode(response.body); - - Webhook webhook = Webhook.from(payload: payload); - webhook..channel = guild.channels.cache.get(payload['guild_id']) - ..user = guild.members.cache.get(payload['user']['id']) - ..guild = guild; - - return webhook; - } - return null; + return response.statusCode == 200 + ? Webhook.from(payload: jsonDecode(response.body)) + : null; } } diff --git a/lib/src/api/messages/dm_message.dart b/lib/src/api/messages/dm_message.dart index ea3a453a..0738fdc7 100644 --- a/lib/src/api/messages/dm_message.dart +++ b/lib/src/api/messages/dm_message.dart @@ -1,13 +1,16 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; -import 'package:mineral/src/api/channels/dm_channel.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/builders/component_builder.dart'; +import 'package:mineral/src/api/channels/dm_channel.dart'; import 'package:mineral/src/api/managers/message_reaction_manager.dart'; import 'package:mineral/src/api/messages/message_attachment.dart'; import 'package:mineral/src/api/messages/message_sticker_item.dart'; import 'package:mineral/src/api/messages/partial_message.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; +import 'package:mineral_ioc/ioc.dart'; -class DmMessage extends PartialMessage { +class DmMessage extends PartialMessage with Container { User author; DmMessage( @@ -30,7 +33,7 @@ class DmMessage extends PartialMessage { ); factory DmMessage.from({ required DmChannel channel, required dynamic payload }) { - MineralClient client = ioc.singleton(Service.client); + MineralClient client = ioc.use(); User? user = client.users.cache.get(payload['author']['id']); List embeds = []; diff --git a/lib/src/api/messages/message.dart b/lib/src/api/messages/message.dart index b30f5257..ee4384d6 100644 --- a/lib/src/api/messages/message.dart +++ b/lib/src/api/messages/message.dart @@ -1,19 +1,20 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/builders/component_builder.dart'; import 'package:mineral/src/api/managers/message_reaction_manager.dart'; import 'package:mineral/src/api/messages/message_attachment.dart'; import 'package:mineral/src/api/messages/message_mention.dart'; import 'package:mineral/src/api/messages/message_sticker_item.dart'; import 'package:mineral/src/api/messages/partial_message.dart'; - import 'package:mineral/src/internal/extensions/mineral_client.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class Message extends PartialMessage { +class Message extends PartialMessage with Container { Snowflake _authorId; final MessageMention _mentions; @@ -45,9 +46,8 @@ class Message extends PartialMessage { MessageMention get mentions => _mentions; Future edit ({ String? content, List? embeds, List? components, bool? tts }) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch( + Response response = await container.use().patch( url: '/channels/${channel.id}/messages/$id', payload: { 'content': content, @@ -69,8 +69,7 @@ class Message extends PartialMessage { return; } - Http http = ioc.singleton(Service.http); - await http.post(url: '/channels/${super.channel.id}/messages/${super.id}/crosspost', payload: {}); + await container.use().post(url: '/channels/${super.channel.id}/messages/${super.id}/crosspost', payload: {}); } Future pin (Snowflake webhookId) async { @@ -79,8 +78,7 @@ class Message extends PartialMessage { return; } - Http http = ioc.singleton(Service.http); - await http.put(url: '/channels/${channel.id}/pins/$id', payload: {}); + await container.use().put(url: '/channels/${channel.id}/pins/$id', payload: {}); } Future unpin () async { @@ -89,12 +87,11 @@ class Message extends PartialMessage { return; } - Http http = ioc.singleton(Service.http); - await http.destroy(url: '/channels/${channel.id}/pins/$id'); + await container.use().destroy(url: '/channels/${channel.id}/pins/$id'); } Future reply ({ String? content, List? embeds, List? components, bool? tts }) async { - MineralClient client = ioc.singleton(Service.client); + MineralClient client = container.use(); Response response = await client.sendMessage(channel, content: content, diff --git a/lib/src/api/messages/message_attachment.dart b/lib/src/api/messages/message_attachment.dart index 30e502c3..2f0aa917 100644 --- a/lib/src/api/messages/message_attachment.dart +++ b/lib/src/api/messages/message_attachment.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; class MessageAttachment { Snowflake id; diff --git a/lib/src/api/messages/message_mention.dart b/lib/src/api/messages/message_mention.dart index 1da0663f..fb8419f1 100644 --- a/lib/src/api/messages/message_mention.dart +++ b/lib/src/api/messages/message_mention.dart @@ -1,4 +1,5 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; class MessageMention { final GuildChannel _channel; diff --git a/lib/src/api/messages/message_sticker_item.dart b/lib/src/api/messages/message_sticker_item.dart index 53d7e8e1..2262ce7e 100644 --- a/lib/src/api/messages/message_sticker_item.dart +++ b/lib/src/api/messages/message_sticker_item.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/src/api/sticker.dart'; class MessageStickerItem { diff --git a/lib/src/api/messages/partial_message.dart b/lib/src/api/messages/partial_message.dart index da9022cc..4da24727 100644 --- a/lib/src/api/messages/partial_message.dart +++ b/lib/src/api/messages/partial_message.dart @@ -1,12 +1,14 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/builders/component_builder.dart'; import 'package:mineral/src/api/managers/message_reaction_manager.dart'; import 'package:mineral/src/api/messages/message_attachment.dart'; import 'package:mineral/src/api/messages/message_sticker_item.dart'; +import 'package:mineral/src/internal/mixins/container.dart'; -class PartialMessage { +class PartialMessage with Container { final Snowflake _id; final String _content; final bool _tts; @@ -65,8 +67,9 @@ class PartialMessage { bool get isPinned => _pinned; - PartialChannel get channel => _guildId != null - ? ioc.singleton(Service.client).guilds.cache.getOrFail(_guildId).channels.cache.getOrFail(_channelId) - : ioc.singleton(Service.client).dmChannels.cache.getOrFail(_channelId); + dynamic get channel => _guildId != null + ? container.use().guilds.cache.getOrFail(_guildId).channels.cache.getOrFail(_channelId) + : container.use().dmChannels.cache.getOrFail(_channelId); + MessageReactionManager get reactions => _reactions; } diff --git a/lib/src/api/moderation_rule.dart b/lib/src/api/moderation_rule.dart index 828ec789..7f9c0cfb 100644 --- a/lib/src/api/moderation_rule.dart +++ b/lib/src/api/moderation_rule.dart @@ -1,8 +1,10 @@ import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/exception.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/exceptions/too_many.dart'; +import 'package:mineral_ioc/ioc.dart'; enum ModerationEventType { messageSend(1); @@ -160,8 +162,7 @@ class ModerationRule { /// await rule.setLabel('My label'); /// ``` Future setLabel(String label) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$guildId/auto-moderation/rules/$id", payload: { 'label': label }); + Response response = await ioc.use().patch(url: "/guilds/$guildId/auto-moderation/rules/$id", payload: { 'label': label }); if (response.statusCode == 200) { this.label = label; @@ -175,8 +176,7 @@ class ModerationRule { /// await rule.setEventType(ModerationEventType.messageSend); /// ``` Future setEventType(ModerationEventType event) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$guildId/auto-moderation/rules/$id", payload: { 'event_type': event.value }); + Response response = await ioc.use().patch(url: "/guilds/$guildId/auto-moderation/rules/$id", payload: { 'event_type': event.value }); if (response.statusCode == 200) { eventType = event; @@ -195,8 +195,7 @@ class ModerationRule { /// await rule.setTriggerMetadata(metadata); /// ``` Future setTriggerMetadata(ModerationTriggerMetadata triggerMetadata) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$guildId/auto-moderation/rules/$id", payload: { 'trigger_metadata': triggerMetadata.toJson() }); + Response response = await ioc.use().patch(url: "/guilds/$guildId/auto-moderation/rules/$id", payload: { 'trigger_metadata': triggerMetadata.toJson() }); if (response.statusCode == 200) { this.triggerMetadata = triggerMetadata; @@ -217,8 +216,7 @@ class ModerationRule { /// await rule.setActions([action]); /// ``` Future setActions(List actions) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$guildId/auto-moderation/rules/$id", payload: { + Response response = await ioc.use().patch(url: "/guilds/$guildId/auto-moderation/rules/$id", payload: { 'actions': actions.map((ModerationAction action) => action.toJson()) }); @@ -234,8 +232,7 @@ class ModerationRule { /// await rule.setEnabled(true); /// ``` Future setEnabled(bool value) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$guildId/auto-moderation/rules/$id", payload: { 'value': value }); + Response response = await ioc.use().patch(url: "/guilds/$guildId/auto-moderation/rules/$id", payload: { 'value': value }); if (response.statusCode == 200) { enabled = value; @@ -258,8 +255,7 @@ class ModerationRule { TooMany(cause: "The list of roles cannot exceed $maxItems items (currently ${roles.length} given)"); } - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$guildId/auto-moderation/rules/$id", payload: { + Response response = await ioc.use().patch(url: "/guilds/$guildId/auto-moderation/rules/$id", payload: { 'exempt_roles': roles.map((Role role) => role.id) }); @@ -284,8 +280,7 @@ class ModerationRule { TooMany(cause: "The list of channels cannot exceed $maxItems items (currently ${channels.length} given)"); } - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/$guildId/auto-moderation/rules/$id", payload: { + Response response = await ioc.use().patch(url: "/guilds/$guildId/auto-moderation/rules/$id", payload: { 'exempt_roles': channels.map((GuildChannel channel) => channel.id) }); @@ -300,14 +295,13 @@ class ModerationRule { /// await rule.delete(); /// ``` Future delete() async { - Http http = ioc.singleton(Service.http); - Response response = await http.destroy(url: "/guilds/$guildId/auto-moderation/rules/$id"); + Response response = await ioc.use().destroy(url: "/guilds/$guildId/auto-moderation/rules/$id"); return response.statusCode == 204; } factory ModerationRule.fromPayload (dynamic payload) { - Guild guild = ioc.singleton(Service.client).guilds.cache.getOrFail(payload['guild_id']); + Guild guild = ioc.use().guilds.cache.getOrFail(payload['guild_id']); List actions = []; if (payload['actions'] != null) { diff --git a/lib/src/api/permission_overwrite.dart b/lib/src/api/permission_overwrite.dart index 3a1d8e7d..fe726874 100644 --- a/lib/src/api/permission_overwrite.dart +++ b/lib/src/api/permission_overwrite.dart @@ -1,5 +1,5 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/helper.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/src/helper.dart'; enum PermissionOverwriteType { role(0), diff --git a/lib/src/api/role.dart b/lib/src/api/role.dart index 89922dd7..d6ce5059 100644 --- a/lib/src/api/role.dart +++ b/lib/src/api/role.dart @@ -1,9 +1,10 @@ import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; import 'package:mineral/exception.dart'; -import 'package:mineral/helper.dart'; import 'package:mineral/src/api/managers/guild_role_manager.dart'; +import 'package:mineral/src/helper.dart'; +import 'package:mineral_ioc/ioc.dart'; class Tag { Snowflake? botId; @@ -60,9 +61,8 @@ class Role { /// } /// ``` Future setLabel (String label) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'name': label }); + Response response = await ioc.use().patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'name': label }); if (response.statusCode == 200) { _label = label; } @@ -80,10 +80,9 @@ class Role { /// } /// Future setPermissions (List permissions) async { - Http http = ioc.singleton(Service.http); int _permissions = Helper.reduceRolePermissions(permissions); - Response response = await http.patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'permissions': _permissions }); + Response response = await ioc.use().patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'permissions': _permissions }); if (response.statusCode == 200) { this._permissions = _permissions; @@ -108,10 +107,9 @@ class Role { /// await role.setColor(Color('#ffffff')); /// ``` Future setColor (Color color) async { - Http http = ioc.singleton(Service.http); int _color = Helper.toRgbColor(color); - Response response = await http.patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'color': _color }); + Response response = await ioc.use().patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'color': _color }); if (response.statusCode == 200) { this._color = _color; } @@ -127,9 +125,8 @@ class Role { /// } /// ``` Future setHoist (bool hoist) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'hoist': hoist }); + Response response = await ioc.use().patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'hoist': hoist }); if (response.statusCode == 200) { _hoist = hoist; } @@ -163,8 +160,7 @@ class Role { String icon = await Helper.getPicture(path); - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'icon': icon }); + Response response = await ioc.use().patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'icon': icon }); if (response.statusCode == 200) { _icon = icon; } @@ -190,8 +186,7 @@ class Role { throw MissingFeatureException(cause: "Guild ${manager.guild.name} has no 'ROLE_ICONS' feature."); } - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'icon': null }); + Response response = await ioc.use().patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'icon': null }); if (response.statusCode == 200) { _icon = null; } @@ -217,8 +212,7 @@ class Role { throw MissingFeatureException(cause: "Guild ${manager.guild.name} has no 'ROLE_ICONS' feature."); } - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'unicode_emoji': unicode }); + Response response = await ioc.use().patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'unicode_emoji': unicode }); if (response.statusCode == 200) { _unicodeEmoji = unicode; } @@ -234,8 +228,7 @@ class Role { /// } /// ``` Future setMentionable (bool mentionable) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'mentionable': mentionable }); + Response response = await ioc.use().patch(url: "/guilds/${manager.guild.id}/roles/$id", payload: { 'mentionable': mentionable }); if (response.statusCode == 200) { _mentionable = mentionable; @@ -264,8 +257,7 @@ class Role { return; } - Http http = ioc.singleton(Service.http); - Response response = await http.destroy(url: "/guilds/${manager.guild.id}/roles/$id"); + Response response = await ioc.use().destroy(url: "/guilds/${manager.guild.id}/roles/$id"); if (response.statusCode == 200) { manager.cache.remove(id); diff --git a/lib/src/api/status.dart b/lib/src/api/status.dart index 5b1ce4ab..0954cfde 100644 --- a/lib/src/api/status.dart +++ b/lib/src/api/status.dart @@ -1,4 +1,5 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; enum StatusType { online('online'), diff --git a/lib/src/api/sticker.dart b/lib/src/api/sticker.dart index ee6d505f..89ba170a 100644 --- a/lib/src/api/sticker.dart +++ b/lib/src/api/sticker.dart @@ -1,7 +1,9 @@ - import 'package:http/http.dart'; -import 'package:mineral/api.dart'; +import 'package:http/http.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/managers/sticker_manager.dart'; +import 'package:mineral_ioc/ioc.dart'; enum StickerType { standard(1), @@ -62,43 +64,35 @@ class Sticker { int? get sortValue => _sortValue; Future setName (String name) async { - Http http = ioc.singleton(Service.http); - - Response response = await http.patch(url: "/guilds/${guild.id}/stickers/$id", payload: { 'name': name }); + Response response = await ioc.use().patch(url: "/guilds/${guild.id}/stickers/$id", payload: { 'name': name }); if (response.statusCode == 200) { _name = name; } } Future setDescription (String description) async { - Http http = ioc.singleton(Service.http); - - Response response = await http.patch(url: "/guilds/${guild.id}/stickers/$id", payload: { 'description': description }); + Response response = await ioc.use().patch(url: "/guilds/${guild.id}/stickers/$id", payload: { 'description': description }); if (response.statusCode == 200) { _description = description; } } Future setTags (String tags) async { - Http http = ioc.singleton(Service.http); - - Response response = await http.patch(url: "/guilds/${guild.id}/stickers/$id", payload: { 'tags': tags }); + Response response = await ioc.use().patch(url: "/guilds/${guild.id}/stickers/$id", payload: { 'tags': tags }); if (response.statusCode == 200) { _tags = tags; } } Future delete () async { - Http http = ioc.singleton(Service.http); - - Response response = await http.destroy(url: "/guilds/${guild.id}/stickers/$id"); + Response response = await ioc.use().destroy(url: "/guilds/${guild.id}/stickers/$id"); if (response.statusCode == 200) { manager.cache.remove(id); } } factory Sticker.from(dynamic payload) { - MineralClient client = ioc.singleton(Service.client); + MineralClient client = ioc.use(); Guild guild = client.guilds.cache.getOrFail(payload['guild_id']); GuildMember? member = guild.members.cache.get(payload['user']?['id']); diff --git a/lib/src/api/user.dart b/lib/src/api/user.dart index 2d217ee8..786f4572 100644 --- a/lib/src/api/user.dart +++ b/lib/src/api/user.dart @@ -1,11 +1,14 @@ import 'dart:convert'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/api/channels/dm_channel.dart'; import 'package:mineral/src/api/messages/dm_message.dart'; import 'package:mineral/src/internal/extensions/mineral_client.dart'; +import 'package:mineral_ioc/ioc.dart'; class User { Snowflake _id; @@ -47,7 +50,7 @@ class User { /// Return [GuildMember] of [Guild] context for this GuildMember? toGuildMember (Snowflake guildId) { - MineralClient client = ioc.singleton(Service.client); + MineralClient client = ioc.use(); return client.guilds.cache.get(guildId)?.members.cache.get(_id); } @@ -59,14 +62,12 @@ class User { /// await member.user.send(content: 'Hello World !'); /// ``` Future send ({ String? content, List? embeds, List? components, bool? tts }) async { - MineralClient client = ioc.singleton(Service.client); - Http http = ioc.singleton(Service.http); - + MineralClient client = ioc.use(); DmChannel? channel = client.dmChannels.cache.get(_id); /// Get channel if exist or create if (channel == null) { - Response response = await http.post(url: '/users/@me/channels', payload: { 'recipient_id': _id }); + Response response = await ioc.use().post(url: '/users/@me/channels', payload: { 'recipient_id': _id }); if (response.statusCode == 200) { channel = DmChannel.fromPayload(jsonDecode(response.body)); client.dmChannels.cache.putIfAbsent(channel.id, () => channel!); diff --git a/lib/src/api/webhook.dart b/lib/src/api/webhook.dart index c68c739a..0dfa8f81 100644 --- a/lib/src/api/webhook.dart +++ b/lib/src/api/webhook.dart @@ -1,7 +1,12 @@ +import 'package:mineral/framework.dart'; import 'package:http/http.dart'; -import 'package:mineral/api.dart'; import 'package:mineral/core.dart'; -import 'package:mineral/helper.dart'; +import 'package:mineral/core/api.dart'; +import 'package:mineral/core/builders.dart'; +import 'package:mineral/src/api/channels/source_channel.dart'; +import 'package:mineral/src/api/guilds/guild.dart'; +import 'package:mineral/src/helper.dart'; +import 'package:mineral_ioc/ioc.dart'; enum WebhookType { incoming(1), @@ -13,34 +18,46 @@ enum WebhookType { } class Webhook { - Snowflake id; - WebhookType type; - Snowflake? guildId; - late Guild? guild; - Snowflake? channelId; - late GuildChannel? channel; - User? user; - String? label; - String? avatar; - String token; - Snowflake? applicationId; - dynamic sourceGuild; - dynamic sourceChannel; - String? url; - - Webhook({ - required this.id, - required this.type, - required this.guildId, - required this.channelId, - required this.label, - required this.avatar, - required this.token, - required this.applicationId, - required this.sourceGuild, - required this.sourceChannel, - required this.url, - }); + Snowflake _id; + int _type; + Snowflake? _guildId; + Snowflake? _channelId; + Snowflake? _creatorId; + String? _label; + String? _avatar; + String _token; + Snowflake? _applicationId; + String? _url; + SourceChannel? _sourceChannel; + SourceGuild? _sourceGuild; + + Webhook( + this._id, + this._type, + this._guildId, + this._channelId, + this._creatorId, + this._label, + this._avatar, + this._token, + this._applicationId, + this._url, + this._sourceChannel, + this._sourceGuild, + ); + + Snowflake get id => _id; + WebhookType get type => WebhookType.values.firstWhere((element) => element.value == _type); + Guild? get guild => ioc.use().guilds.cache.get(_guildId); + GuildChannel? get channel => guild?.channels.cache.get(_channelId); + User? get creator => guild?.members.cache.get(_creatorId); + String? get label => _label; + String? get avatar => _avatar; + String get token => _token; + Snowflake? get applicationId => _applicationId; + String? get url => _url; + SourceChannel? get sourceChannel => _sourceChannel; + SourceGuild? get sourceGuild => _sourceGuild; /// ### Update the label of this /// @@ -49,11 +66,10 @@ class Webhook { /// await webhook.setLabel('My webhook name'); /// ``` Future setLabel (String label) async { - Http http = ioc.singleton(Service.http); - Response response = await http.patch(url: "/webhooks/$id", payload: { 'name': label }); + Response response = await ioc.use().patch(url: "/webhooks/$id", payload: { 'name': label }); if (response.statusCode == 200) { - this.label = label; + _label = label; } } @@ -64,15 +80,28 @@ class Webhook { /// await webhook.setAvatar('assets/images/my_picture.png'); /// ``` Future setAvatar (String avatar) async { - Http http = ioc.singleton(Service.http); String path = await Helper.getPicture(avatar); - Response response = await http.patch(url: "/webhooks/$id", payload: { 'avatar': path }); + Response response = await ioc.use().patch(url: "/webhooks/$id", payload: { 'avatar': path }); if (response.statusCode == 200) { avatar = path; } } + /// ### Update the avatar of this + /// + /// Example : + /// ```dart + /// await webhook.setChannel('xxxxxxx'); + /// ``` + Future setChannel (Snowflake channelId) async { + Response response = await ioc.use().patch(url: "/webhooks/$id", payload: { 'channel_id': channelId }); + + if (response.statusCode == 200) { + _channelId = channelId; + } + } + /// ### Updates multiple properties of this in a single request. /// When you need to update more than 2 fields, we advise you to use this method to reduce the number of outgoing requests. /// @@ -81,19 +110,18 @@ class Webhook { /// await webhook.update(label: 'My webhook name', avatar: 'assets/images/my_picture.png'); /// ``` Future update ({ String? label, String? avatar }) async { - Http http = ioc.singleton(Service.http); String? path = avatar != null ? await Helper.getPicture(avatar) : this.label; - Response response = await http.patch(url: "/webhooks/$id", payload: { + Response response = await ioc.use().patch(url: "/webhooks/$id", payload: { 'label': label ?? this.label, 'avatar': path, }); if (response.statusCode == 200) { - if (label != null) this.label = label; - if (avatar != null) this.avatar = path; + if (label != null) _label = label; + if (avatar != null) _avatar = path; } } /// ### Send a message from the webhook @@ -103,7 +131,6 @@ class Webhook { /// await webhook.execute(content: 'Hello World !'); /// ``` Future execute ({ String? content, String? username, String? avatarUrl, bool? tts, List? embeds, List? components, bool? suppressEmbed }) async { - Http http = ioc.singleton(Service.http); List embedList = []; if (embeds != null) { @@ -119,7 +146,7 @@ class Webhook { } } - await http.post(url: "/webhooks/$id/$token", payload: { + await ioc.use().post(url: "/webhooks/$id/$token", payload: { 'username': username, 'avatar_url': avatarUrl, 'content': content, @@ -137,25 +164,25 @@ class Webhook { /// await webhook.delete(); /// ``` Future delete () async { - Http http = ioc.singleton(Service.http); - Response response = await http.destroy(url: "/webhooks/$id/$token"); + Response response = await ioc.use().destroy(url: "/webhooks/$id/$token"); return response.statusCode == 200; } factory Webhook.from ({ required dynamic payload }) { return Webhook( - id: payload['id'], - type: WebhookType.values.firstWhere((element) => element.value == payload['type']), - guildId: payload['guild_id'], - channelId: payload['channel_id'], - label: payload['name'], - avatar: payload['avatar'], - token: payload['token'], - applicationId: payload['application_id'], - sourceGuild: payload['source_guild'], - sourceChannel: payload['source_channel'], - url: payload['url'] + payload['id'], + payload['type'], + payload['guild_id'], + payload['channel_id'], + payload['user']['id'], + payload['name'], + payload['avatar'], + payload['token'], + payload['application_id'], + payload['url'], + SourceChannel(payload['source_channel']['id'], payload['source_channel']['name']), + SourceGuild(payload['source_guild']['id'], payload['source_guild']['name'], ImageFormater(payload['source_channel']['icon'], 'icons/${payload['source_guild']['id']}')) ); } } diff --git a/lib/src/api/welcome_screen.dart b/lib/src/api/welcome_screen.dart index d9fbb3ad..5a54ea63 100644 --- a/lib/src/api/welcome_screen.dart +++ b/lib/src/api/welcome_screen.dart @@ -1,4 +1,4 @@ -import 'package:mineral/api.dart'; +import 'package:mineral/core/api.dart'; class ScreenField { Snowflake channelId; diff --git a/lib/src/commands/create_project.dart b/lib/src/commands/create_project.dart index 9628d5f9..71c6d797 100644 --- a/lib/src/commands/create_project.dart +++ b/lib/src/commands/create_project.dart @@ -1,8 +1,7 @@ import 'dart:io'; import 'package:args/args.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/internal/managers/cli_manager.dart'; import 'package:path/path.dart'; diff --git a/lib/src/commands/help.dart b/lib/src/commands/help.dart index 42d0c10a..4f383540 100644 --- a/lib/src/commands/help.dart +++ b/lib/src/commands/help.dart @@ -1,8 +1,8 @@ import 'package:collection/collection.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; -import 'package:mineral/core.dart'; +import 'package:mineral/framework.dart'; +import 'package:mineral/src/console.dart'; import 'package:mineral/src/internal/managers/cli_manager.dart'; +import 'package:mineral_ioc/ioc.dart'; class Help extends MineralCliCommand { @override @@ -19,8 +19,7 @@ class Help extends MineralCliCommand { ? command.key.split(':').first : 'Available commands'; - CliManager cli = ioc.singleton(Service.cli); - MineralCliCommand? mineralCommand = cli.commands.get(command.key); + MineralCliCommand? mineralCommand = ioc.use().commands.get(command.key); if (commands.containsKey(key)) { commands.get(key)?.add({ 'name': command.key, 'args': mineralCommand?.description }); diff --git a/lib/src/commands/make_command.dart b/lib/src/commands/make_command.dart index 025d7e4e..3c875dad 100644 --- a/lib/src/commands/make_command.dart +++ b/lib/src/commands/make_command.dart @@ -2,8 +2,8 @@ import 'dart:io'; import 'package:args/args.dart'; import 'package:interact/interact.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; +import 'package:mineral/framework.dart'; +import 'package:mineral/src/console.dart'; import 'package:mineral/src/internal/managers/cli_manager.dart'; import 'package:path/path.dart'; @@ -37,9 +37,9 @@ class MakeCommand extends MineralCliCommand { label: 'Where do you want to place your file ?', list: directories, items: directories.map((directory) => directory.path - .replaceAll(join(Directory.current.path, 'src'), 'App') - .replaceAll('\\', '/')) - .toList() + .replaceAll(join(Directory.current.path, 'src'), 'App') + .replaceAll('\\', '/')) + .toList() ); file = File(join(location.path, '${filename.snakeCase}.dart')); @@ -59,11 +59,13 @@ class MakeCommand extends MineralCliCommand { } String getTemplate (String filename) => ''' -import 'package:mineral/core.dart'; -import 'package:mineral/api.dart'; +import 'package:mineral/framework.dart'; +import 'package:mineral/core/api.dart'; -@Command(name: '${filename.toLowerCase()}', description: '${filename.capitalCase} command description', scope: 'GUILD') class ${filename.pascalCase} extends MineralCommand { + ${filename.pascalCase} () { + register(CommandBuilder('${filename.toLowerCase()}', '${filename.capitalCase} command description', scope: Scope.guild)); + } Future handle (CommandInteraction interaction) async { // Your code here await interaction.reply(content: 'Hello World ! ๐Ÿ’ช'); diff --git a/lib/src/commands/make_event.dart b/lib/src/commands/make_event.dart index 4d76fb64..6621655b 100644 --- a/lib/src/commands/make_event.dart +++ b/lib/src/commands/make_event.dart @@ -1,13 +1,11 @@ import 'dart:io'; -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; -import 'package:path/path.dart'; import 'package:args/args.dart'; import 'package:interact/interact.dart'; -import 'package:mineral/core.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/internal/managers/cli_manager.dart'; +import 'package:path/path.dart'; class MakeEvent extends MineralCliCommand { @override @@ -25,12 +23,6 @@ class MakeEvent extends MineralCliCommand { String filename = args.arguments.elementAt(1).capitalCase; - final event = Console.cli.choice( - label: 'Which event would you like to use ?', - list: Events.values, - items: Events.values.map((e) => e.toString()).toList() - ); - final useExistLocation = Confirm( prompt: 'Do you want to use an existing location on your disk ?', defaultValue: true, @@ -61,27 +53,19 @@ class MakeEvent extends MineralCliCommand { } await file.create(recursive: true); - await writeFileContent(file, getTemplate(filename, event)); + await writeFileContent(file, getTemplate(filename)); Console.cli.success(message: 'File created ${file.uri}'); } - String getTemplate (String filename, Events event) { - List params = []; - for (MapEntry param in event.params.entries) { - params.add('${param.value} ${param.key}'); - } - - return ''' -import 'package:mineral/core.dart'; -import 'package:mineral/api.dart'; + String getTemplate (String filename) => ''' +import 'package:mineral/framework.dart'; +import 'package:mineral/core/events.dart'; -@Event(${event.toString()}) -class ${filename.pascalCase} extends MineralEvent { - Future handle (${params.join(', ')}) async { +class ${filename.pascalCase} extends MineralEvent { + Future handle (event) async { // Your code here } } - '''; - } +'''; } diff --git a/lib/src/commands/make_module.dart b/lib/src/commands/make_module.dart index c8c1d61e..5d570b06 100644 --- a/lib/src/commands/make_module.dart +++ b/lib/src/commands/make_module.dart @@ -2,8 +2,8 @@ import 'dart:io'; import 'package:args/args.dart'; import 'package:interact/interact.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; +import 'package:mineral/framework.dart'; +import 'package:mineral/src/console.dart'; import 'package:mineral/src/internal/managers/cli_manager.dart'; import 'package:path/path.dart'; @@ -38,9 +38,9 @@ class MakeModule extends MineralCliCommand { label: 'Where do you want to place your file ?', list: directories, items: directories.map((directory) => directory.path - .replaceAll(join(Directory.current.path, 'src'), 'App') - .replaceAll('\\', '/')) - .toList() + .replaceAll(join(Directory.current.path, 'src'), 'App') + .replaceAll('\\', '/')) + .toList() ); directory = Directory(join(location.path, filename.snakeCase)); @@ -66,16 +66,16 @@ class MakeModule extends MineralCliCommand { } String getTemplate (String filename) => ''' -import 'package:mineral/core.dart'; +import 'package:mineral/framework.dart'; -@Module(identifier: '${filename.snakeCase}', label: '${filename.capitalCase} module') class ${filename.pascalCase} extends MineralModule { + ${filename.pascalCase} (): super('${filename.snakeCase}', '${filename.capitalCase}', '${filename.capitalCase} description'); @override Future init () async { commands.register([]); events.register([]); contextMenus.register([]); - stores.register([]); + states.register([]); } } '''; diff --git a/lib/src/commands/make_store.dart b/lib/src/commands/make_shared_state.dart similarity index 74% rename from lib/src/commands/make_store.dart rename to lib/src/commands/make_shared_state.dart index 58225877..a213cb72 100644 --- a/lib/src/commands/make_store.dart +++ b/lib/src/commands/make_shared_state.dart @@ -2,17 +2,16 @@ import 'dart:io'; import 'package:args/args.dart'; import 'package:interact/interact.dart'; -import 'package:mineral/api.dart'; -import 'package:mineral/console.dart'; +import 'package:mineral/framework.dart'; import 'package:mineral/src/internal/managers/cli_manager.dart'; import 'package:path/path.dart'; -class MakeStore extends MineralCliCommand { +class MakeSharedState extends MineralCliCommand { @override - String name = 'make:store'; + String name = 'make:state'; @override - String description = 'Make a new store file'; + String description = 'Make a new state file'; @override Future handle (ArgResults args) async { @@ -37,9 +36,9 @@ class MakeStore extends MineralCliCommand { label: 'Where do you want to place your file ?', list: directories, items: directories.map((directory) => directory.path - .replaceAll(join(Directory.current.path, 'src'), 'App') - .replaceAll('\\', '/')) - .toList() + .replaceAll(join(Directory.current.path, 'src'), 'App') + .replaceAll('\\', '/')) + .toList() ); file = File(join(location.path, '${filename.snakeCase}.dart')); @@ -59,17 +58,12 @@ class MakeStore extends MineralCliCommand { } String getTemplate (String filename) => ''' -import 'package:mineral/core.dart'; +import 'package:mineral/framework.dart'; -@Store('${filename.snakeCase}') -class ${filename.pascalCase} extends MineralStore { - @override - dynamic state = []; +class ${filename.pascalCase} extends MineralState { + ${filename.pascalCase}(): super('${filename.pascalCase}', '${filename.pascalCase} description'); // Please define your shared methods below - void addState (value) { - state.add(value); - } } '''; } diff --git a/lib/src/commands/start_project.dart b/lib/src/commands/start_project.dart index 312a65be..a3198a9b 100644 --- a/lib/src/commands/start_project.dart +++ b/lib/src/commands/start_project.dart @@ -2,7 +2,7 @@ import 'dart:convert'; import 'dart:io'; import 'package:args/args.dart'; -import 'package:mineral/console.dart'; +import 'package:mineral/src/console.dart'; import 'package:mineral/src/exceptions/code_error_exception.dart'; import 'package:mineral/src/internal/managers/cli_manager.dart'; import 'package:path/path.dart'; diff --git a/lib/console.dart b/lib/src/console.dart similarity index 91% rename from lib/console.dart rename to lib/src/console.dart index 628c1770..624a1400 100644 --- a/lib/console.dart +++ b/lib/src/console.dart @@ -10,7 +10,7 @@ import 'package:mineral_ioc/ioc.dart'; class Cli { void log ({ required String message, String level = 'info'}) { if (level == 'debug') { - final Environment environment = ioc.singleton(Service.environment); + final Environment environment = ioc.use(); final String? logLevel = environment.get('LOG_LEVEL'); if (logLevel != 'debug') return; @@ -42,8 +42,8 @@ class Console { static void log ({ required String message, String level = 'info'}) { if (level == 'debug') { - final Environment environment = ioc.singleton(Service.environment); - final String? logLevel = environment.get('LOG_LEVEL'); + final Environment environment = ioc.use(); + final String? logLevel = environment.getOr('LOG_LEVEL', defaultValue: 'info'); if (logLevel != 'debug') return; } @@ -52,7 +52,7 @@ class Console { } static debug ({ String prefix = 'debug', required String message }) { - ReporterManager? reporter = ioc.singleton(Service.reporter); + ReporterManager? reporter = ioc.use(); if (reporter != null && reporter.reportLevel == 'debug') { _report('[ $prefix ] $message'); @@ -95,9 +95,10 @@ class Console { } static _report (String message) { - ReporterManager? reporter = ioc.singleton(Service.reporter); - if (reporter != null) { + try { + ReporterManager? reporter = ioc.use(); reporter.write(message); + } catch (err) { } } } diff --git a/lib/src/exceptions/already_exist.dart b/lib/src/exceptions/already_exist.dart index 0e86447e..6ae078cc 100644 --- a/lib/src/exceptions/already_exist.dart +++ b/lib/src/exceptions/already_exist.dart @@ -1,5 +1,4 @@ -import 'package:mineral/console.dart'; -import 'package:mineral/core.dart'; +import 'package:mineral/src/console.dart'; import 'package:mineral/src/internal/managers/reporter_manager.dart'; import 'package:mineral_ioc/ioc.dart'; @@ -10,7 +9,7 @@ class AlreadyExist implements Exception { @override String toString () { - ReporterManager? reporter = ioc.singleton(Service.reporter); + ReporterManager? reporter = ioc.use(); if (reporter != null) { reporter.write('[ $prefix ] $cause'); } diff --git a/lib/src/exceptions/api_exception.dart b/lib/src/exceptions/api_exception.dart index 75d15743..6f3bab41 100644 --- a/lib/src/exceptions/api_exception.dart +++ b/lib/src/exceptions/api_exception.dart @@ -9,7 +9,7 @@ class ApiException implements Exception { @override String toString () { - ReporterManager? reporter = ioc.singleton(Service.reporter); + ReporterManager? reporter = ioc.use(); if (reporter != null) { reporter.write('[ $code ] $cause'); } diff --git a/lib/src/exceptions/code_error_exception.dart b/lib/src/exceptions/code_error_exception.dart index 43f49d92..631ddda1 100644 --- a/lib/src/exceptions/code_error_exception.dart +++ b/lib/src/exceptions/code_error_exception.dart @@ -1,4 +1,4 @@ -import 'package:mineral/console.dart'; +import 'package:mineral/src/console.dart'; import 'package:mineral/src/internal/managers/reporter_manager.dart'; import 'package:mineral_ioc/ioc.dart'; @@ -9,7 +9,7 @@ class CodeErrorException implements Exception { @override String toString () { - ReporterManager? reporter = ioc.singleton(Service.reporter); + ReporterManager? reporter = ioc.use(); if (reporter != null) { reporter.write('[ $prefix ] $cause'); } diff --git a/lib/src/exceptions/empty_parameter_exception.dart b/lib/src/exceptions/empty_parameter_exception.dart index a3d67cac..2e4c4780 100644 --- a/lib/src/exceptions/empty_parameter_exception.dart +++ b/lib/src/exceptions/empty_parameter_exception.dart @@ -1,4 +1,4 @@ -import 'package:mineral/console.dart'; +import 'package:mineral/src/console.dart'; import 'package:mineral/src/internal/managers/reporter_manager.dart'; import 'package:mineral_ioc/ioc.dart'; @@ -9,7 +9,7 @@ class EmptyParameterException implements Exception { @override String toString () { - ReporterManager? reporter = ioc.singleton(Service.reporter); + ReporterManager? reporter = ioc.use(); if (reporter != null) { reporter.write('[ $prefix ] $cause'); } diff --git a/lib/src/exceptions/http_exception.dart b/lib/src/exceptions/http_exception.dart index d34c4baa..75206a22 100644 --- a/lib/src/exceptions/http_exception.dart +++ b/lib/src/exceptions/http_exception.dart @@ -1,5 +1,5 @@ -import 'package:mineral/core.dart'; import 'package:mineral/src/internal/managers/reporter_manager.dart'; +import 'package:mineral_ioc/ioc.dart'; class HttpException implements Exception { int code; @@ -9,7 +9,7 @@ class HttpException implements Exception { @override String toString () { - ReporterManager? reporter = ioc.singleton(Service.reporter); + ReporterManager? reporter = ioc.use(); if (reporter != null) { reporter.write('[ $code ] $cause'); } diff --git a/lib/src/exceptions/invalid_parameter_exception.dart b/lib/src/exceptions/invalid_parameter_exception.dart index d4fa7096..32f7f6e8 100644 --- a/lib/src/exceptions/invalid_parameter_exception.dart +++ b/lib/src/exceptions/invalid_parameter_exception.dart @@ -7,7 +7,7 @@ class InvalidParameterException implements Exception { @override String toString () { - ReporterManager? reporter = ioc.singleton(Service.reporter); + ReporterManager? reporter = ioc.use(); if (reporter != null) { reporter.write('[ Invalid parameter ] $cause'); } diff --git a/lib/src/exceptions/missing_feature_exception.dart b/lib/src/exceptions/missing_feature_exception.dart index 2a8e2494..01ab99c4 100644 --- a/lib/src/exceptions/missing_feature_exception.dart +++ b/lib/src/exceptions/missing_feature_exception.dart @@ -1,4 +1,4 @@ -import 'package:mineral/console.dart'; +import 'package:mineral/src/console.dart'; import 'package:mineral/src/internal/managers/reporter_manager.dart'; import 'package:mineral_ioc/ioc.dart'; @@ -10,7 +10,7 @@ class MissingFeatureException implements Exception { @override String toString () { - ReporterManager? reporter = ioc.singleton(Service.reporter); + ReporterManager? reporter = ioc.use(); if (reporter != null) { reporter.write('[ $prefix ] $cause'); } diff --git a/lib/src/exceptions/missing_method_exception.dart b/lib/src/exceptions/missing_method_exception.dart index d7b9f6dc..631352dd 100644 --- a/lib/src/exceptions/missing_method_exception.dart +++ b/lib/src/exceptions/missing_method_exception.dart @@ -1,4 +1,4 @@ -import 'package:mineral/console.dart'; +import 'package:mineral/src/console.dart'; import 'package:mineral/src/internal/managers/reporter_manager.dart'; import 'package:mineral_ioc/ioc.dart'; @@ -10,7 +10,7 @@ class MissingMethodException implements Exception { @override String toString () { - ReporterManager? reporter = ioc.singleton(Service.reporter); + ReporterManager? reporter = ioc.use(); if (reporter != null) { reporter.write('[ $prefix ] $cause'); } diff --git a/lib/src/exceptions/missing_premium_subscription.dart b/lib/src/exceptions/missing_premium_subscription.dart index bfb5639e..a94b8886 100644 --- a/lib/src/exceptions/missing_premium_subscription.dart +++ b/lib/src/exceptions/missing_premium_subscription.dart @@ -1,4 +1,4 @@ -import 'package:mineral/console.dart'; +import 'package:mineral/src/console.dart'; import 'package:mineral/src/internal/managers/reporter_manager.dart'; import 'package:mineral_ioc/ioc.dart'; @@ -9,7 +9,7 @@ class MissingPremiumSubscription implements Exception { @override String toString () { - ReporterManager? reporter = ioc.singleton(Service.reporter); + ReporterManager? reporter = ioc.use(); if (reporter != null) { reporter.write('[ $prefix ] $cause'); } diff --git a/lib/src/exceptions/not_exist.dart b/lib/src/exceptions/not_exist.dart index 0ab8e867..68a07c20 100644 --- a/lib/src/exceptions/not_exist.dart +++ b/lib/src/exceptions/not_exist.dart @@ -1,4 +1,4 @@ -import 'package:mineral/console.dart'; +import 'package:mineral/src/console.dart'; import 'package:mineral/src/internal/managers/reporter_manager.dart'; import 'package:mineral_ioc/ioc.dart'; @@ -9,7 +9,7 @@ class NotExist implements Exception { @override String toString () { - ReporterManager? reporter = ioc.singleton(Service.reporter); + ReporterManager? reporter = ioc.use(); if (reporter != null) { reporter.write('[ $prefix ] $cause'); } diff --git a/lib/src/exceptions/shard_exception.dart b/lib/src/exceptions/shard_exception.dart index a8c67ab8..5e13c40c 100644 --- a/lib/src/exceptions/shard_exception.dart +++ b/lib/src/exceptions/shard_exception.dart @@ -1,4 +1,4 @@ -import 'package:mineral/console.dart'; +import 'package:mineral/src/console.dart'; import 'package:mineral/src/internal/managers/reporter_manager.dart'; import 'package:mineral_ioc/ioc.dart'; @@ -9,7 +9,7 @@ class ShardException implements Exception { @override String toString () { - ReporterManager? reporter = ioc.singleton(Service.reporter); + ReporterManager? reporter = ioc.use(); if (reporter != null) { reporter.write('[ $prefix ] $cause'); } diff --git a/lib/src/exceptions/token_exception.dart b/lib/src/exceptions/token_exception.dart index 63b285b7..03c37563 100644 --- a/lib/src/exceptions/token_exception.dart +++ b/lib/src/exceptions/token_exception.dart @@ -1,4 +1,4 @@ -import 'package:mineral/console.dart'; +import 'package:mineral/src/console.dart'; import 'package:mineral/src/internal/managers/reporter_manager.dart'; import 'package:mineral_ioc/ioc.dart'; @@ -9,7 +9,7 @@ class TokenException implements Exception { @override String toString () { - ReporterManager? reporter = ioc.singleton(Service.reporter); + ReporterManager? reporter = ioc.use(); if (reporter != null) { reporter.write('[ $prefix ] $cause'); } diff --git a/lib/src/exceptions/too_many.dart b/lib/src/exceptions/too_many.dart index b83fecf5..d0fc2d9a 100644 --- a/lib/src/exceptions/too_many.dart +++ b/lib/src/exceptions/too_many.dart @@ -1,4 +1,4 @@ -import 'package:mineral/console.dart'; +import 'package:mineral/src/console.dart'; import 'package:mineral/src/internal/managers/reporter_manager.dart'; import 'package:mineral_ioc/ioc.dart'; @@ -9,7 +9,7 @@ class TooMany implements Exception { @override String toString () { - ReporterManager? reporter = ioc.singleton(Service.reporter); + ReporterManager? reporter = ioc.use(); if (reporter != null) { reporter.write('[ $prefix ] $cause'); } diff --git a/lib/helper.dart b/lib/src/helper.dart similarity index 97% rename from lib/helper.dart rename to lib/src/helper.dart index eda89d00..36008667 100644 --- a/lib/helper.dart +++ b/lib/src/helper.dart @@ -4,7 +4,8 @@ library helper; import 'dart:convert'; import 'dart:io'; import 'dart:typed_data'; -import 'package:mineral/api.dart'; + +import 'package:mineral/core/api.dart'; import 'package:path/path.dart' as path; class Helper { diff --git a/lib/src/internal/entities/command.dart b/lib/src/internal/entities/command.dart index 2c02ed1e..aa377881 100644 --- a/lib/src/internal/entities/command.dart +++ b/lib/src/internal/entities/command.dart @@ -1,44 +1,51 @@ -import 'package:mineral/api.dart'; -import 'package:mineral/core.dart'; -import 'package:mineral/helper.dart'; -import 'package:mineral/src/internal/managers/store_manager.dart'; - -class MineralCommand { - late MineralClient client; - late StoreManager stores; - late Environment environment; -} +import 'package:mineral/core/api.dart'; +import 'package:mineral/src/helper.dart'; -class Command { - final String type = 'command'; - final String name; - final String description; - final String scope; - final bool? everyone; - final bool? dmChannel; - final List? permissions; +import '../../exceptions/missing_method_exception.dart'; + +class Scope { + final String mode; + + static Scope get guild => Scope('GUILD'); + static Scope get global => Scope('GLOBAL'); - const Command ({ required this.name, required this.description, required this.scope, this.everyone, this.dmChannel, this.permissions }); + Scope(this.mode); } -class CommandGroup { - final String name; - final String description; - final int type = 2; +abstract class MineralCommand { + late final CommandBuilder command; - const CommandGroup ({ required this.name, required this.description }); + void register(CommandBuilder builder) { + command = builder; + } + + Future handle (CommandInteraction interaction) async { + throw MissingMethodException(cause: 'The handle method does not exist on your command ${command.label}'); + } } -class Subcommand { - final String name; - final String description; - final int type = 1; - final String? group; - final String? bind; +class AbstractCommand { + final String _label; + final String _description; + final Scope? _scope; + + AbstractCommand(this._label, this._description, this._scope); - const Subcommand ({ required this.name, required this.description, this.group, this.bind }); + String get label => _label; + String get description => _description; + Scope? get scope => _scope; + + Object get toJson => {}; } +enum CommandType { + command(0), + subcommand(1), + group(2); + + final int type; + const CommandType(this.type); +} enum OptionType { string(3), @@ -55,13 +62,98 @@ enum OptionType { const OptionType(this.value); } -class OptionChoice { - final String label; - final String value; +class CommandGroupBuilder extends AbstractCommand { + final List _subcommands = []; - const OptionChoice({ required this.label, required this.value }); + CommandGroupBuilder(String label, String description): super(label, description, null); - Object toJson () => { 'name': label, 'value': value }; + List get subcommands => _subcommands; + + void addSubcommand (SubCommandBuilder builder) { + _subcommands.add(builder); + } + + @override + Object get toJson => { + 'name': _label.toLowerCase(), + 'description': _description, + 'type': CommandType.group.type, + 'options': _subcommands.map((option) => option.toJson).toList(), + }; +} + +class SubCommandBuilder extends AbstractCommand { + final Function _method; + final List