nyxx-discord / nyxx

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

Null check operator used on a null value when running an UserCommand or a ChatCommand #669

Closed shalien closed 3 months ago

shalien commented 3 months ago

Describe the bug When running an UserCommand or a ChatCommand an un handled exception is triggered on a null check operator used on a null value.

To Reproduce

// avatar command
import 'dart:io';

import 'package:http/http.dart';
import 'package:nyxx/nyxx.dart';
import 'package:nyxx_commands/nyxx_commands.dart';

UserCommand avatarCommand = UserCommand(
    'avatar',
    id('avatar', (UserContext context) async {
      User user = context.targetUser;

      Uri avatarUrl = user.avatar.url;

      Response? response;

      try {
        response = await get(avatarUrl);
      } catch (ex) {
        await context.respond(
            MessageBuilder(
                content:
                    'An issue happened while fetching the avatar for user ${user.username}.'),
            level: ResponseLevel.private);
        return;
      }

      if (response.statusCode != HttpStatus.ok) {
        await context.respond(
            MessageBuilder(
                content:
                    'An issue happened while fetching the avatar for user ${user.username}.'
                    'Status code: ${response.statusCode}'
                    'Response: ${response.body}'),
            level: ResponseLevel.private);
        return;
      }

      await context.respond(
          MessageBuilder(
              content: 'Avatar for user ${user.username}:',
              attachments: [
                AttachmentBuilder(
                    data: response.bodyBytes,
                    fileName: avatarUrl.pathSegments.last)
              ]),
          level: ResponseLevel.public);
    }));
// client

Future<void> main(List<String> arguments) async {
  CommandsPlugin commandsPlugin = CommandsPlugin(prefix: mentionOr((_) => '§'));

  commandsPlugin.addCommand(avatarCommand);

  var client = await Nyxx.connectGateway(
      '',
      GatewayIntents.messageContent |
          GatewayIntents.directMessages |
          GatewayIntents.all,
      options: GatewayClientOptions(plugins: [commandsPlugin, logging]));

Expected behavior No exception should occur since the command execute under the 3 seconds theresold

Screenshots

Desktop (please complete the following information):

Additional context Stacktrace :

[2024-07-06 10:04:53.348667] [INFO] [Nyxx] Connecting to the Gateway API [2024-07-06 10:04:54.021023] [INFO] [Nyxx.Gateway] Connecting 1/1 shards [2024-07-06 10:04:54.225853] [INFO] [Nyxx.Shards[0]] Connecting to Gateway [2024-07-06 10:04:54.286923] [INFO] [Commands] Synced 3 commands to Discord [2024-07-06 10:04:54.477115] [INFO] [Nyxx.Shards[0]] Connected to Gateway Unhandled exception: Null check operator used on a null value

0 MessageManager.parseMessageInteractionMetadata (package:nyxx/src/http/managers/message_manager.dart:327:45)

1 maybeParse (package:nyxx/src/utils/parsing_helpers.dart:17:15)

2 MessageManager.parse (package:nyxx/src/http/managers/message_manager.dart:84:28)

3 Gateway.parseMessageCreate (package:nyxx/src/gateway/gateway.dart:810:7)

4 Gateway.parseDispatchEvent (package:nyxx/src/gateway/gateway.dart:286:31)

5 Gateway.events. (package:nyxx/src/gateway/gateway.dart:82:27)

#6 _AsBroadcastStreamController.add (dart:async/broadcast_stream_controller.dart:469:3)
Rapougnac commented 3 months ago

Already known, there's a fix incoming #668. In the meantime you can point to the fix branch in your dependency_overrides