parseAndRunCommand method

Future<String?> parseAndRunCommand(
  1. Room room,
  2. String msg, {
  3. Event? inReplyTo,
  4. String? editEventId,
  5. String? txid,
  6. String? threadRootEventId,
  7. String? threadLastEventId,
})

Parse and execute a string, msg is the input. Optionally inReplyTo is the event being replied to and editEventId is the eventId of the event being replied to

Implementation

Future<String?> parseAndRunCommand(
  Room room,
  String msg, {
  Event? inReplyTo,
  String? editEventId,
  String? txid,
  String? threadRootEventId,
  String? threadLastEventId,
}) async {
  final args = CommandArgs(
    inReplyTo: inReplyTo,
    editEventId: editEventId,
    msg: '',
    room: room,
    txid: txid,
    threadRootEventId: threadRootEventId,
    threadLastEventId: threadLastEventId,
  );
  if (!msg.startsWith('/')) {
    final sendCommand = commands['send'];
    if (sendCommand != null) {
      args.msg = msg;
      return await sendCommand(args);
    }
    return null;
  }
  // remove the /
  msg = msg.substring(1);
  var command = msg;
  if (msg.contains(' ')) {
    final idx = msg.indexOf(' ');
    command = msg.substring(0, idx).toLowerCase();
    args.msg = msg.substring(idx + 1);
  } else {
    command = msg.toLowerCase();
  }
  final commandOp = commands[command];
  if (commandOp != null) {
    return await commandOp(args);
  }
  if (msg.startsWith('/') && commands.containsKey('send')) {
    // re-set to include the "command"
    final sendCommand = commands['send'];
    if (sendCommand != null) {
      args.msg = msg;
      return await sendCommand(args);
    }
  }
  return null;
}