Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement plugins #60

Merged
merged 8 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.2.1
- Fix wrong userId key into interactions

## 1.2.0
- Implement `setDefaultReactionEmoji` method
- Implement `setTags` method
Expand Down
2 changes: 2 additions & 0 deletions lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export 'src/api/interactions/context_user_interaction.dart' show ContextUserInte
export 'src/api/interactions/context_message_interaction.dart' show ContextMessageInteraction;
export 'src/api/interactions/interaction.dart' show Interaction;

export 'src/api/components/code_builder.dart' show CodeBuilder;

export 'src/api/utils.dart';
export 'src/internal/extensions/collection.dart';
export 'src/internal/extensions/string.dart';
Expand Down
16 changes: 16 additions & 0 deletions lib/src/api/components/code_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'dart:convert';

class CodeBuilder {
final String _language;
final dynamic _code;
int? indent;


CodeBuilder(this._language, this._code, { this.indent });

@override
String toString () {
final encoder = JsonEncoder.withIndent(List.filled(indent ?? 2, ' ').join());
return '```$_language\n' + encoder.convert(_code) + '```';
}
}
2 changes: 1 addition & 1 deletion lib/src/api/interactions/button_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ButtonInteraction extends Interaction {
payload['version'],
payload['type'],
payload['token'],
payload['user']?['id'],
payload['member']?['user']?['id'],
payload['guild_id'],
payload['message_id'],
payload['data']['custom_id'],
Expand Down
2 changes: 1 addition & 1 deletion lib/src/api/interactions/command_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class CommandInteraction extends Interaction {
payload['version'],
payload['type'],
payload['token'],
payload['user']?['id'],
payload['member']?['user']?['id'],
payload['guild_id'],
payload['data']['name'],
payload['channel_id'],
Expand Down
2 changes: 1 addition & 1 deletion lib/src/api/interactions/context_message_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ContextMessageInteraction extends Interaction {
payload['version'],
payload['type'],
payload['token'],
payload['user']?['id'],
payload['member']?['user']?['id'],
payload['guild_id'],
payload['guild_id'],
message,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/api/interactions/context_user_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ContextUserInteraction extends Interaction {
payload['version'],
payload['type'],
payload['token'],
payload['user']?['id'],
payload['member']?['user']?['id'],
payload['guild_id'],
payload['target_id'],
payload['channel_id']
Expand Down
2 changes: 1 addition & 1 deletion lib/src/api/interactions/select_menu_interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class SelectMenuInteraction extends Interaction {
payload['version'],
payload['type'],
payload['token'],
payload['user']?['id'],
payload['member']?['user']?['id'],
payload['guild_id'],
message,
payload['data']['custom_id'],
Expand Down
19 changes: 6 additions & 13 deletions lib/src/internal/kernel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Kernel {
ContextMenuManager contextMenus = ContextMenuManager();
IntentManager intents = IntentManager();
PluginManager plugins = PluginManager();
Environment environment = Environment();

Kernel() {
ioc.bind(namespace: Service.event, service: events);
Expand All @@ -39,6 +40,8 @@ class Kernel {
ioc.bind(namespace: Service.modules, service: modules);
ioc.bind(namespace: Service.cli, service: cli);
ioc.bind(namespace: Service.contextMenu, service: contextMenus);
ioc.bind(namespace: Service.environment, service: environment);

}

void loadConsole () {
Expand All @@ -52,7 +55,7 @@ class Kernel {
}

Future<void> init () async {
Environment environment = await _loadEnvironment();
await environment.load();

Http http = Http(baseUrl: 'https://discord.com/api');
http.defineHeader(header: 'Content-Type', value: 'application/json');
Expand All @@ -63,10 +66,7 @@ class Kernel {
ReporterManager reporter = ReporterManager(Directory(join(Directory.current.path, 'logs')));
reporter.reportLevel = report;

ioc.bind(
namespace: Service.reporter,
service: reporter
);
ioc.bind(namespace: Service.reporter, service: reporter);
}

String? token = environment.get('APP_TOKEN');
Expand All @@ -78,6 +78,7 @@ class Kernel {
}

await modules.load(this);
await plugins.load();

final String? shardsCount = environment.get('SHARDS_COUNT');

Expand All @@ -86,12 +87,4 @@ class Kernel {

ioc.bind(namespace: Service.shards, service: manager);
}


Future<Environment> _loadEnvironment () async {
Environment environment = Environment();
ioc.bind(namespace: Service.environment, service: environment);

return await environment.load();
}
}
13 changes: 11 additions & 2 deletions lib/src/internal/managers/plugin_manager.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import 'dart:io';
import 'dart:mirrors';

import 'package:mineral_ioc/ioc.dart';

class PluginManager {
final List _plugins = [];

void use (List plugins) {
for (final plugin in plugins) {
ioc.bind(namespace: 'Mineral/Plugins/${plugin.label}', service: plugin);
_plugins.addAll(plugins);
}

Future<void> load () async {
for (final plugin in _plugins) {
final namespace = reflect(plugin).type.getField(Symbol('namespace'));
ioc.bind(namespace: namespace.reflectee, service: plugin);

plugin..root = Directory.current
..init();
}
Expand Down
1 change: 0 additions & 1 deletion lib/src/internal/managers/store_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:mirrors';
import 'package:mineral/core.dart';
import 'package:mineral/src/exceptions/already_exist.dart';
import 'package:mineral/src/exceptions/not_exist.dart';
import 'package:mineral_ioc/ioc.dart';

class StoreManager {
final Map<String, dynamic> _stores = {};
Expand Down
4 changes: 2 additions & 2 deletions lib/src/internal/services/environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class Environment {
for (String line in content) {
if (line.isNotEmpty) {
List<String> content = line.split(':');
String key = content[0].trim();
String value = content[1].trim();
String key = content.removeAt(0).trim();
String value = content.join(':').trim();

_cache.putIfAbsent(key, () => value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'package:mineral/src/internal/managers/context_menu_manager.dart';
import 'package:mineral/src/internal/managers/event_manager.dart';
import 'package:mineral/src/internal/websockets/websocket_packet.dart';
import 'package:mineral/src/internal/websockets/websocket_response.dart';
import 'package:mineral_ioc/ioc.dart';

class InteractionCreate implements WebsocketPacket {
@override
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: mineral
description: Mineral is a Discord framework for designing discord bots in Dart.
version: 1.2.0
version: 1.2.1

repository: https://github.com/mineral-dart/core
# homepage: https://www.example.com
Expand Down