-
Notifications
You must be signed in to change notification settings - Fork 72
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
Bot command processing #363
base: develop
Are you sure you want to change the base?
Conversation
8414fd7
to
2c53fbe
Compare
N.B it would be nice if the help command supported categories ala the irc bridge. |
I'm porting Slack command handling to this to see how it fares in practice – will come back with proper review and feedback once I'm done with that – I don't want it to be a component that we ship and then don't use ;) |
const botCommands: {[prefix: string]: BotCommandEntry<R>} = {}; | ||
const proto = Object.getPrototypeOf(instance); | ||
Object.getOwnPropertyNames(proto).forEach(propetyKey => { | ||
const b = Reflect.getMetadata(botCommandSymbol, instance, propetyKey) as BotCommandMetadata; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this? Metadata or a BotCommand?
Please pick a better variable name.
Object.getOwnPropertyNames(proto).forEach(propetyKey => { | ||
const b = Reflect.getMetadata(botCommandSymbol, instance, propetyKey) as BotCommandMetadata; | ||
if (!b) { | ||
// Not a bot command function. | ||
return; | ||
} | ||
const optionalArgs = b.optionalArgs?.map((arg: string) => `[${arg}]`) || []; | ||
const args = [...(b.requiredArgs || []), ...optionalArgs].join(" "); | ||
|
||
content += ` - \`${this.prefix || ""}${b.name}\`${args && " "}${args} - ${b.help}\n`; | ||
// We know that this is safe. | ||
const fn = instance[propetyKey]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo
Object.getOwnPropertyNames(proto).forEach(propetyKey => { | |
const b = Reflect.getMetadata(botCommandSymbol, instance, propetyKey) as BotCommandMetadata; | |
if (!b) { | |
// Not a bot command function. | |
return; | |
} | |
const optionalArgs = b.optionalArgs?.map((arg: string) => `[${arg}]`) || []; | |
const args = [...(b.requiredArgs || []), ...optionalArgs].join(" "); | |
content += ` - \`${this.prefix || ""}${b.name}\`${args && " "}${args} - ${b.help}\n`; | |
// We know that this is safe. | |
const fn = instance[propetyKey]; | |
Object.getOwnPropertyNames(proto).forEach(propertyKey => { | |
const b = Reflect.getMetadata(botCommandSymbol, instance, propertyKey) as BotCommandMetadata; | |
if (!b) { | |
// Not a bot command function. | |
return; | |
} | |
const optionalArgs = b.optionalArgs?.map((arg: string) => `[${arg}]`) || []; | |
const args = [...(b.requiredArgs || []), ...optionalArgs].join(" "); | |
content += ` - \`${this.prefix || ""}${b.name}\`${args && " "}${args} - ${b.help}\n`; | |
// We know that this is safe. | |
const fn = instance[propertyKey]; |
* It should contain at least one `BotCommand`. | ||
* @param instance The instance of the above prototype to bind to for function calls. | ||
* @param prefix A prefix to be stripped from commands (useful if using multiple handlers). The prefix | ||
* should **include** any whitspace E.g. `!irc `. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* should **include** any whitspace E.g. `!irc `. | |
* should **include** any whitespace E.g. `!irc `. |
Implemented this in Slack for test run here, and a few things stand out:
Slack is a bit of an extreme case due to being yargs-based, and its commands could definitely be simplified – but perhaps employing a readymade command line parser is not that bad of an idea? Yargs was not fun to work with, but I've had good experiences with |
This PR is largely a repurposing of https://github.com/Half-Shot/matrix-github/blob/master/src/BotCommands.ts. Notably, it's a class which you bind onto another class that offers command handlers. The idea being that it's then relatively trivial to add new commands, you just add metadata to a function and the rest works.
As an example: https://github.com/Half-Shot/matrix-github/blob/master/src/Connections/GithubRepo.ts#L208-L231