registerDefaultCommands method

void registerDefaultCommands()

Register all default commands

Implementation

void registerDefaultCommands() {
  addCommand('send', (CommandArgs args) async {
    return await args.room.sendTextEvent(
      args.msg,
      inReplyTo: args.inReplyTo,
      editEventId: args.editEventId,
      parseCommands: false,
      txid: args.txid,
      threadRootEventId: args.threadRootEventId,
      threadLastEventId: args.threadLastEventId,
    );
  });
  addCommand('me', (CommandArgs args) async {
    return await args.room.sendTextEvent(
      args.msg,
      inReplyTo: args.inReplyTo,
      editEventId: args.editEventId,
      msgtype: MessageTypes.Emote,
      parseCommands: false,
      txid: args.txid,
      threadRootEventId: args.threadRootEventId,
      threadLastEventId: args.threadLastEventId,
    );
  });
  addCommand('dm', (CommandArgs args) async {
    final parts = args.msg.split(' ');
    return await args.room.client.startDirectChat(
      parts.first,
      enableEncryption: !parts.any((part) => part == '--no-encryption'),
    );
  });
  addCommand('create', (CommandArgs args) async {
    final parts = args.msg.split(' ');
    return await args.room.client.createGroupChat(
      enableEncryption: !parts.any((part) => part == '--no-encryption'),
    );
  });
  addCommand('plain', (CommandArgs args) async {
    return await args.room.sendTextEvent(
      args.msg,
      inReplyTo: args.inReplyTo,
      editEventId: args.editEventId,
      parseMarkdown: false,
      parseCommands: false,
      txid: args.txid,
      threadRootEventId: args.threadRootEventId,
      threadLastEventId: args.threadLastEventId,
    );
  });
  addCommand('html', (CommandArgs args) async {
    final event = <String, dynamic>{
      'msgtype': 'm.text',
      'body': args.msg,
      'format': 'org.sdn.custom.html',
      'formatted_body': args.msg,
    };
    return await args.room.sendEvent(
      event,
      inReplyTo: args.inReplyTo,
      editEventId: args.editEventId,
      txid: args.txid,
    );
  });
  addCommand('react', (CommandArgs args) async {
    final inReplyTo = args.inReplyTo;
    if (inReplyTo == null) {
      return null;
    }
    return await args.room.sendReaction(inReplyTo.eventId, args.msg);
  });
  addCommand('join', (CommandArgs args) async {
    await args.room.client.joinRoom(args.msg);
    return null;
  });
  addCommand('leave', (CommandArgs args) async {
    await args.room.leave();
    return '';
  });
  addCommand('op', (CommandArgs args) async {
    final parts = args.msg.split(' ');
    if (parts.isEmpty) {
      return null;
    }
    int? pl;
    if (parts.length >= 2) {
      pl = int.tryParse(parts[1]);
    }
    final mxid = parts.first;
    return await args.room.setPower(mxid, pl ?? 50);
  });
  addCommand('kick', (CommandArgs args) async {
    final parts = args.msg.split(' ');
    await args.room.kick(parts.first);
    return '';
  });
  addCommand('ban', (CommandArgs args) async {
    final parts = args.msg.split(' ');
    await args.room.ban(parts.first);
    return '';
  });
  addCommand('unban', (CommandArgs args) async {
    final parts = args.msg.split(' ');
    await args.room.unban(parts.first);
    return '';
  });
  addCommand('invite', (CommandArgs args) async {
    final parts = args.msg.split(' ');
    await args.room.invite(parts.first);
    return '';
  });
  addCommand('myroomnick', (CommandArgs args) async {
    final currentEventJson = args.room
            .getState(EventTypes.RoomMember, args.room.client.userID!)
            ?.content
            .copy() ??
        {};
    currentEventJson['displayname'] = args.msg;
    return await args.room.client.setRoomStateWithKey(
      args.room.id,
      EventTypes.RoomMember,
      args.room.client.userID!,
      currentEventJson,
    );
  });
  addCommand('myroomavatar', (CommandArgs args) async {
    final currentEventJson = args.room
            .getState(EventTypes.RoomMember, args.room.client.userID!)
            ?.content
            .copy() ??
        {};
    currentEventJson['avatar_url'] = args.msg;
    return await args.room.client.setRoomStateWithKey(
      args.room.id,
      EventTypes.RoomMember,
      args.room.client.userID!,
      currentEventJson,
    );
  });
  addCommand('discardsession', (CommandArgs args) async {
    await encryption?.keyManager
        .clearOrUseOutboundGroupSession(args.room.id, wipe: true);
    return '';
  });
  addCommand('clearcache', (CommandArgs args) async {
    await clearCache();
    return '';
  });
  addCommand('markasdm', (CommandArgs args) async {
    final mxid = args.msg;
    if (!mxid.isValidSDNId) {
      throw Exception('You must enter a valid mxid when using /maskasdm');
    }
    if (await args.room.requestUser(mxid, requestProfile: false) == null) {
      throw Exception('User $mxid is not in this room');
    }
    await args.room.addToDirectChat(args.msg);
    return;
  });
  addCommand('markasgroup', (CommandArgs args) async {
    await args.room.removeFromDirectChat();
    return;
  });
  addCommand('hug', (CommandArgs args) async {
    final content = CuteEventContent.hug;
    return await args.room.sendEvent(
      content,
      inReplyTo: args.inReplyTo,
      editEventId: args.editEventId,
      txid: args.txid,
    );
  });
  addCommand('googly', (CommandArgs args) async {
    final content = CuteEventContent.googlyEyes;
    return await args.room.sendEvent(
      content,
      inReplyTo: args.inReplyTo,
      editEventId: args.editEventId,
      txid: args.txid,
    );
  });
  addCommand('cuddle', (CommandArgs args) async {
    final content = CuteEventContent.cuddle;
    return await args.room.sendEvent(
      content,
      inReplyTo: args.inReplyTo,
      editEventId: args.editEventId,
      txid: args.txid,
    );
  });
}