NyxxRest constructor

NyxxRest(
  1. String _token,
  2. int intents, {
  3. ClientOptions? options,
  4. CacheOptions? cacheOptions,
  5. bool ignoreExceptions = true,
  6. bool useDefaultLogger = true,
})

Creates and logs in a new client. If ignoreExceptions is true (by default is) isolate will ignore all exceptions and continue to work.

Implementation

NyxxRest(this._token, this.intents,
    {ClientOptions? options,
      CacheOptions? cacheOptions,
      bool ignoreExceptions = true,
      bool useDefaultLogger = true}) {
  this._logger.fine("Staring Nyxx: intents: [$intents]; ignoreExceptions: [$ignoreExceptions]; useDefaultLogger: [$useDefaultLogger]");

  if (_token.isEmpty) {
    throw MissingTokenError();
  }

  if (useDefaultLogger) {
    Logger.root.onRecord.listen((LogRecord rec) {
      print("[${rec.time}] [${rec.level.name}] [${rec.loggerName}] ${rec.message}");
    });
  }

  this._logger.info("Starting bot with pid: $pid. To stop the bot gracefully send SIGTERM or SIGKILL");

  if (!Platform.isWindows) {
    ProcessSignal.sigterm.watch().forEach((event) async {
      await this.dispose();
    });
  }

  ProcessSignal.sigint.watch().forEach((event) async {
    await this.dispose();
  });

  if (ignoreExceptions) {
    Isolate.current.setErrorsFatal(false);

    final errorsPort = ReceivePort();
    errorsPort.listen((err) {
      final stackTrace = err[1] != null
        ? ". Stacktrace: \n${err[1]}"
        : "";

      _logger.shout("Got Error: Message: [${err[0]}]$stackTrace");
    });
    Isolate.current.addErrorListener(errorsPort.sendPort);
  }

  this._options = options ?? ClientOptions();
  this._cacheOptions = cacheOptions ?? CacheOptions();

  this.guilds = _SnowflakeCache();
  this.channels = ChannelCache._new();
  this.users = _SnowflakeCache();

  this._http = _HttpHandler._new(this);
  this._httpEndpoints = _HttpEndpoints._new(this);

  this._onHttpError = StreamController.broadcast();
  this.onHttpError = _onHttpError.stream;

  this._onHttpResponse = StreamController.broadcast();
  this.onHttpResponse = _onHttpResponse.stream;

  this._onRateLimited = StreamController.broadcast();
  this.onRateLimited = _onRateLimited.stream;
}