mineral-dart / core

🧡 The neuralgic heart of the application, this module gathers all the functionalities of the framework.
https://mineral-foundation.org
MIT License
27 stars 8 forks source link

Create access to discord resources from client #216

Open LeadcodeDev opened 3 weeks ago

NathaelB commented 3 weeks ago

The aim is to be able to access server, channel and other objects in any class.

For example, in the context of an external call (HTTP, AMQP, Redis, etc.), to be able to retrieve a channel/server and interact with it. For example, how to retrieve a server, a channel and send a message on the channel:

Future<void> main(_, port) async {
  final client = Client().setCache((e) => MemoryProvider()).setHmrDevPort(port).build();
  client.events.ready((Bot bot) {
    client.logger.info('${bot.username} is ready ! 🚀');
  });

  final nats = Nats();
  await nats.connect();

  final natsClient = nats.client;

  final sub = natsClient.sub('messages');

  client.events.server.serverCreate((Server server) {
    final channel = server.channels.getOrFail('1302764876120195143');
    sub.stream.listen((msg) async {
      final str = String.fromCharCodes(msg.data);
      client.logger.info('Received message: ${str}');

      if (channel case ServerTextChannel channel) {

        final embed = MessageEmbedBuilder()
          .setTitle('Test')
          .setDescription("Message reçu: $str")
          .setColor(Color.cyan_200)
          .build();

        await channel.send(embeds: [embed], content: 'Message contenant un embed');
      }
    }); 
  });
  await client.init();
}

We could imagine something like that:

final class TestMessage with InjectClient {
  void handle() {
     final server = client.getServer('...')
     server.channels.getOrFail('...')
    // Code ...
  }
}