Merge branch 'kegan/slash-command-tab-complete' into matthew/roomsettings2
This commit is contained in:
commit
ffaea371ac
10 changed files with 310 additions and 172 deletions
|
@ -20,6 +20,31 @@ var dis = require("./dispatcher");
|
|||
var encryption = require("./encryption");
|
||||
var Tinter = require("./Tinter");
|
||||
|
||||
|
||||
class Command {
|
||||
constructor(name, paramArgs, runFn) {
|
||||
this.name = name;
|
||||
this.paramArgs = paramArgs;
|
||||
this.runFn = runFn;
|
||||
}
|
||||
|
||||
getCommand() {
|
||||
return "/" + this.name;
|
||||
}
|
||||
|
||||
getCommandWithArgs() {
|
||||
return this.getCommand() + " " + this.paramArgs;
|
||||
}
|
||||
|
||||
run(roomId, args) {
|
||||
return this.runFn.bind(this)(roomId, args);
|
||||
}
|
||||
|
||||
getUsage() {
|
||||
return "Usage: " + this.getCommandWithArgs()
|
||||
}
|
||||
}
|
||||
|
||||
var reject = function(msg) {
|
||||
return {
|
||||
error: msg
|
||||
|
@ -34,18 +59,17 @@ var success = function(promise) {
|
|||
|
||||
var commands = {
|
||||
// Change your nickname
|
||||
nick: function(room_id, args) {
|
||||
nick: new Command("nick", "<display_name>", function(room_id, args) {
|
||||
if (args) {
|
||||
return success(
|
||||
MatrixClientPeg.get().setDisplayName(args)
|
||||
);
|
||||
}
|
||||
return reject("Usage: /nick <display_name>");
|
||||
},
|
||||
return reject(this.getUsage());
|
||||
}),
|
||||
|
||||
// Changes the colorscheme of your current room
|
||||
tint: function(room_id, args) {
|
||||
|
||||
tint: new Command("tint", "<primaryColor> [<secondaryColor>]", function(room_id, args) {
|
||||
if (args) {
|
||||
var matches = args.match(/^(#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}))( +(#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})))?$/);
|
||||
if (matches) {
|
||||
|
@ -62,10 +86,10 @@ var commands = {
|
|||
);
|
||||
}
|
||||
}
|
||||
return reject("Usage: /tint <primaryColor> [<secondaryColor>]");
|
||||
},
|
||||
return reject(this.getUsage());
|
||||
}),
|
||||
|
||||
encrypt: function(room_id, args) {
|
||||
encrypt: new Command("encrypt", "<on/off>", function(room_id, args) {
|
||||
if (args == "on") {
|
||||
var client = MatrixClientPeg.get();
|
||||
var members = client.getRoom(room_id).currentState.members;
|
||||
|
@ -81,21 +105,21 @@ var commands = {
|
|||
);
|
||||
|
||||
}
|
||||
return reject("Usage: encrypt <on/off>");
|
||||
},
|
||||
return reject(this.getUsage());
|
||||
}),
|
||||
|
||||
// Change the room topic
|
||||
topic: function(room_id, args) {
|
||||
topic: new Command("topic", "<topic>", function(room_id, args) {
|
||||
if (args) {
|
||||
return success(
|
||||
MatrixClientPeg.get().setRoomTopic(room_id, args)
|
||||
);
|
||||
}
|
||||
return reject("Usage: /topic <topic>");
|
||||
},
|
||||
return reject(this.getUsage());
|
||||
}),
|
||||
|
||||
// Invite a user
|
||||
invite: function(room_id, args) {
|
||||
invite: new Command("invite", "<userId>", function(room_id, args) {
|
||||
if (args) {
|
||||
var matches = args.match(/^(\S+)$/);
|
||||
if (matches) {
|
||||
|
@ -104,11 +128,11 @@ var commands = {
|
|||
);
|
||||
}
|
||||
}
|
||||
return reject("Usage: /invite <userId>");
|
||||
},
|
||||
return reject(this.getUsage());
|
||||
}),
|
||||
|
||||
// Join a room
|
||||
join: function(room_id, args) {
|
||||
join: new Command("join", "<room_alias>", function(room_id, args) {
|
||||
if (args) {
|
||||
var matches = args.match(/^(\S+)$/);
|
||||
if (matches) {
|
||||
|
@ -150,17 +174,17 @@ var commands = {
|
|||
);
|
||||
}
|
||||
}
|
||||
return reject("Usage: /join <room_alias>");
|
||||
},
|
||||
return reject(this.getUsage());
|
||||
}),
|
||||
|
||||
part: function(room_id, args) {
|
||||
part: new Command("part", "[#alias:domain]", function(room_id, args) {
|
||||
var targetRoomId;
|
||||
if (args) {
|
||||
var matches = args.match(/^(\S+)$/);
|
||||
if (matches) {
|
||||
var room_alias = matches[1];
|
||||
if (room_alias[0] !== '#') {
|
||||
return reject("Usage: /part [#alias:domain]");
|
||||
return reject(this.getUsage());
|
||||
}
|
||||
if (!room_alias.match(/:/)) {
|
||||
room_alias += ':' + MatrixClientPeg.get().getDomain();
|
||||
|
@ -196,10 +220,10 @@ var commands = {
|
|||
dis.dispatch({action: 'view_next_room'});
|
||||
})
|
||||
);
|
||||
},
|
||||
}),
|
||||
|
||||
// Kick a user from the room with an optional reason
|
||||
kick: function(room_id, args) {
|
||||
kick: new Command("kick", "<userId> [<reason>]", function(room_id, args) {
|
||||
if (args) {
|
||||
var matches = args.match(/^(\S+?)( +(.*))?$/);
|
||||
if (matches) {
|
||||
|
@ -208,11 +232,11 @@ var commands = {
|
|||
);
|
||||
}
|
||||
}
|
||||
return reject("Usage: /kick <userId> [<reason>]");
|
||||
},
|
||||
return reject(this.getUsage());
|
||||
}),
|
||||
|
||||
// Ban a user from the room with an optional reason
|
||||
ban: function(room_id, args) {
|
||||
ban: new Command("ban", "<userId> [<reason>]", function(room_id, args) {
|
||||
if (args) {
|
||||
var matches = args.match(/^(\S+?)( +(.*))?$/);
|
||||
if (matches) {
|
||||
|
@ -221,11 +245,11 @@ var commands = {
|
|||
);
|
||||
}
|
||||
}
|
||||
return reject("Usage: /ban <userId> [<reason>]");
|
||||
},
|
||||
return reject(this.getUsage());
|
||||
}),
|
||||
|
||||
// Unban a user from the room
|
||||
unban: function(room_id, args) {
|
||||
unban: new Command("unban", "<userId>", function(room_id, args) {
|
||||
if (args) {
|
||||
var matches = args.match(/^(\S+)$/);
|
||||
if (matches) {
|
||||
|
@ -235,11 +259,11 @@ var commands = {
|
|||
);
|
||||
}
|
||||
}
|
||||
return reject("Usage: /unban <userId>");
|
||||
},
|
||||
return reject(this.getUsage());
|
||||
}),
|
||||
|
||||
// Define the power level of a user
|
||||
op: function(room_id, args) {
|
||||
op: new Command("op", "<userId> [<power level>]", function(room_id, args) {
|
||||
if (args) {
|
||||
var matches = args.match(/^(\S+?)( +(\d+))?$/);
|
||||
var powerLevel = 50; // default power level for op
|
||||
|
@ -264,11 +288,11 @@ var commands = {
|
|||
}
|
||||
}
|
||||
}
|
||||
return reject("Usage: /op <userId> [<power level>]");
|
||||
},
|
||||
return reject(this.getUsage());
|
||||
}),
|
||||
|
||||
// Reset the power level of a user
|
||||
deop: function(room_id, args) {
|
||||
deop: new Command("deop", "<userId>", function(room_id, args) {
|
||||
if (args) {
|
||||
var matches = args.match(/^(\S+)$/);
|
||||
if (matches) {
|
||||
|
@ -287,8 +311,8 @@ var commands = {
|
|||
);
|
||||
}
|
||||
}
|
||||
return reject("Usage: /deop <userId>");
|
||||
}
|
||||
return reject(this.getUsage());
|
||||
})
|
||||
};
|
||||
|
||||
// helpful aliases
|
||||
|
@ -313,7 +337,7 @@ module.exports = {
|
|||
var args = bits[3];
|
||||
if (cmd === "me") return null;
|
||||
if (commands[cmd]) {
|
||||
return commands[cmd](roomId, args);
|
||||
return commands[cmd].run(roomId, args);
|
||||
}
|
||||
else {
|
||||
return reject("Unrecognised command: " + input);
|
||||
|
@ -323,8 +347,8 @@ module.exports = {
|
|||
},
|
||||
|
||||
getCommandList: function() {
|
||||
return Object.keys(commands).map(function(cmd) {
|
||||
return "/" + cmd;
|
||||
return Object.keys(commands).map(function(cmdKey) {
|
||||
return commands[cmdKey];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue