nyxx-discord / nyxx

Wrapper around Discord API for Dart
Apache License 2.0
327 stars 49 forks source link

Discord Bot Presence is not settable #355

Closed kNoAPP closed 2 years ago

kNoAPP commented 2 years ago

Describe the bug The call to set a Discord bot's presence does not work.

To Reproduce Steps to reproduce the behavior:

  final bot = NyxxFactory.createNyxxWebsocket(
      discordToken, GatewayIntents.allUnprivileged)
    ..registerPlugin(Logging())
    ..registerPlugin(CliIntegration())
    ..registerPlugin(IgnoreExceptions());

  await bot.connect();

  bot.setPresence(PresenceBuilder.of(
    status: UserStatus.online,
    activity: ActivityBuilder.game('Apex Legends'),
  ));

Expected behavior This code should display "Playing Apex Legends" under the bot's name.

Actual behavior No errors in console. No effect on the bot.

Desktop (please complete the following information):

kNoAPP commented 2 years ago

This however, works:

  final bot = NyxxFactory.createNyxxWebsocket(
      discordToken, GatewayIntents.allUnprivileged)
    ..registerPlugin(Logging())
    ..registerPlugin(CliIntegration())
    ..registerPlugin(IgnoreExceptions());

  await bot.connect();
  // Let the bot's connection warm up. This shouldn't be necessary, but it ensures
  // future calls to Discord don't outright fail.
  await Future.delayed(Duration(seconds: 2));

  bot.setPresence(PresenceBuilder.of(
    status: UserStatus.online,
    activity: ActivityBuilder.game('Apex Legends'),
  ));

There's a race condition in the package somewhere.

abitofevrything commented 2 years ago

There is no race condition, however it is true that the client does not wait for all shards to be connected before returning from the connect methods.

If you want to perform operations that require the shards to be connected, act on each individual shard by listening to the client.eventsWs.onReady event streams which fires when shard connects.