parseAndRunCommand method
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;
}