Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow commands to configure their argument parsing #69

Open
ylt opened this issue Aug 17, 2015 · 0 comments
Open

Allow commands to configure their argument parsing #69

ylt opened this issue Aug 17, 2015 · 0 comments

Comments

@ylt
Copy link
Member

ylt commented Aug 17, 2015

The argument parser has been implemented to be flexible and has options in order to adjust the parsing.

parseMessage(message, options) {
if (message == "") {
return []; // :)
}
if (!options) {
options = {};
}
options.keepquotes = options.keepquotes || false;
options.quotes = options.quotes || true;
options.users = options.users || true;
function matchName(query, i, users) {
let matches = [];
for(let iuser in users) {
if (!users.hasOwnProperty(iuser)) {
continue;
}
let user = users[iuser];
let cmpname = user.username.split(" ").slice(0, i).join(" ");
if (cmpname === query) {
matches.push(user);
}
}
return matches;
}
let users = this.plugin.plug.getUsers();
users.sort(function(a, b) {
return b.username.length - a.username.length;
});
let parts = message.split(" ");
for (let i = 0; i < parts.length; i++) {
let part = parts[i];
if (options.quotes && part[0] === "\"") {
for (let j = 1; j <= 10 && i + j < parts.length; j++) {
console.log(part);
if (part[part.length - 1] === "\"") {
parts.splice(i + 1, j - 1);
if (options.keepquotes === true) {
parts[i] = part;
}
else {
parts[i] = part.substring(1, part.length - 1);
}
break;
}
part = part + " " + parts[i + j];
}
}
else if (options.users && part[0] === "@") {
part = part.slice(1);
let matches = users;
for (let j = 1; j <= 10 && i + j < parts.length; j++) {
matches = matchName(part, j, matches);
if (matches.length > 0) {
if (matches.length === 1) { //is this the name we're looking for?
if (matches[0].username === part) {
parts.splice(i + 1, j - 1);
parts[i] = "@" + part;
}
}
}
else {
break;
}
part = part + " " + parts[i + j];
}
}
}
return parts;
}

As part of the latest changes there is now a decorator for commands, which allows them to register the minimum user rank (and potentially other things), so this could allow for specifying options for the argument parser.

@CommandHandler("unban", {rank: "bouncer", argparser: {
    users: false
}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant