added a default command handler possibility for the client

This commit is contained in:
WolverinDEV 2019-04-18 13:19:08 +02:00
parent 6f3d8306ee
commit cb0b68b4a1
2 changed files with 31 additions and 24 deletions

View file

@ -46,6 +46,35 @@ namespace connection {
this["notifychannelunsubscribed"] = this.handleNotifyChannelUnsubscribed;
}
proxy_command_promise(promise: Promise<CommandResult>, options: connection.CommandOptions) {
if(!options.process_result)
return promise;
return promise.catch(ex => {
if(options.process_result) {
if(ex instanceof CommandResult) {
let res = ex;
if(!res.success) {
if(res.id == 2568) { //Permission error
res.message = tr("Insufficient client permissions. Failed on permission ") + this.connection_handler.permissions.resolveInfo(res.json["failed_permid"] as number).name;
this.connection_handler.chat.serverChat().appendError(tr("Insufficient client permissions. Failed on permission {}"), this.connection_handler.permissions.resolveInfo(res.json["failed_permid"] as number).name);
this.connection_handler.sound.play(Sound.ERROR_INSUFFICIENT_PERMISSIONS);
} else {
this.connection_handler.chat.serverChat().appendError(res.extra_message.length == 0 ? res.message : res.extra_message);
}
}
} else if(typeof(ex) === "string") {
this.connection_handler.chat.serverChat().appendError(tr("Command execution results in ") + ex);
} else {
console.error(tr("Invalid promise result type: %o. Result:"), typeof (ex));
console.error(ex);
}
}
return Promise.reject(ex);
});
}
handle_command(command: ServerCommand) : boolean {
if(this[command.command]) {
this[command.command](command.arguments);

View file

@ -285,30 +285,8 @@ namespace connection {
"flags": options.flagset.filter(entry => entry.length != 0)
}));
});
return new Promise<CommandResult>((resolve, failed) => {
result.then(resolve).catch(ex => {
if(options.process_result) {
if(ex instanceof CommandResult) {
let res = ex;
if(!res.success) {
if(res.id == 2568) { //Permission error
res.message = tr("Insufficient client permissions. Failed on permission ") + this.client.permissions.resolveInfo(res.json["failed_permid"] as number).name;
this.client.chat.serverChat().appendError(tr("Insufficient client permissions. Failed on permission {}"), this.client.permissions.resolveInfo(res.json["failed_permid"] as number).name);
this.client.sound.play(Sound.ERROR_INSUFFICIENT_PERMISSIONS);
} else {
this.client.chat.serverChat().appendError(res.extra_message.length == 0 ? res.message : res.extra_message);
}
}
} else if(typeof(ex) === "string") {
this.client.chat.serverChat().appendError(tr("Command execution results in ") + ex);
} else {
console.error(tr("Invalid promise result type: %o. Result:"), typeof (ex));
console.error(ex);
}
}
failed(ex);
})
});
return this._command_handler_default.proxy_command_promise(result, options);
}
connected() : boolean {