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

feat(composer): match any command #733

Open
wants to merge 5 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ export class Composer<C extends Context> implements MiddlewareObj<C> {
* bot.command('start', ctx => { ... })
* // Reacts to /help commands
* bot.command('help', ctx => { ... })
* // Reacts to any command
* bot.command(undefined, ctx => { ... })
* bot.command().use(ctx => { ... })
* ```
*
* The rest of the message (excluding the command, and trimmed) is provided
Expand Down Expand Up @@ -377,7 +380,7 @@ export class Composer<C extends Context> implements MiddlewareObj<C> {
* @param middleware The middleware to register
*/
command(
command: MaybeArray<StringWithCommandSuggestions>,
command?: MaybeArray<StringWithCommandSuggestions>,
...middleware: Array<CommandMiddleware<C>>
): Composer<CommandContext<C>> {
return this.filter(Context.has.command(command), ...middleware);
Expand Down
45 changes: 20 additions & 25 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ interface StaticHas {
* @param command The command to match
*/
command(
command: MaybeArray<StringWithCommandSuggestions>,
command?: MaybeArray<StringWithCommandSuggestions>,
): <C extends Context>(ctx: C) => ctx is CommandContext<C>;
/**
* Generates a predicate function that can test context objects for
Expand Down Expand Up @@ -186,43 +186,38 @@ const checker: StaticHas = {
},
command(command) {
const hasEntities = checker.filterQuery(":entities:bot_command");
const atCommands = new Set<string>();
const noAtCommands = new Set<string>();
toArray(command).forEach((cmd) => {
const noAtCommands = new Set<string>(toArray(command ?? []));
for (const cmd of noAtCommands) {
if (cmd.startsWith("/")) {
throw new Error(
`Do not include '/' when registering command handlers (use '${
cmd.substring(1)
}' not '${cmd}')`,
);
}
const set = cmd.includes("@") ? atCommands : noAtCommands;
set.add(cmd);
});
}
return <C extends Context>(ctx: C): ctx is CommandContext<C> => {
if (!hasEntities(ctx)) return false;
const msg = ctx.message ?? ctx.channelPost;
const txt = msg.text ?? msg.caption;
return msg.entities.some((e) => {
if (e.type !== "bot_command") return false;
if (e.offset !== 0) return false;
const cmd = txt.substring(1, e.length);
if (noAtCommands.has(cmd) || atCommands.has(cmd)) {
ctx.match = txt.substring(cmd.length + 1).trimStart();
return true;
}
const index = cmd.indexOf("@");
if (index === -1) return false;
const e: MessageEntity = msg.entities[0];
carafelix marked this conversation as resolved.
Show resolved Hide resolved
if (e.type !== "bot_command") return false;
if (e.offset !== 0) return false;
const cmd = txt.substring(1, e.length);
let index = cmd.indexOf("@");
if (index === -1) {
index = Infinity;
} else {
const atTarget = cmd.substring(index + 1).toLowerCase();
const username = ctx.me.username.toLowerCase();
if (atTarget !== username) return false;
const atCommand = cmd.substring(0, index);
if (noAtCommands.has(atCommand)) {
ctx.match = txt.substring(cmd.length + 1).trimStart();
return true;
}
return false;
});
}
const atCommand = cmd.substring(0, index);
if (command === undefined || noAtCommands.has(atCommand)) {
ctx.match = txt.substring(cmd.length + 1).trimStart();
return true;
}
return false;
};
},
reaction(reaction) {
Expand Down Expand Up @@ -853,7 +848,7 @@ export class Context implements RenamedUpdate {
* @param command The command to match
*/
hasCommand(
command: MaybeArray<StringWithCommandSuggestions>,
command?: MaybeArray<StringWithCommandSuggestions>,
): this is CommandContextCore {
return Context.has.command(command)(this);
}
Expand Down
5 changes: 5 additions & 0 deletions test/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ describe("Context", () => {
assert(ctx.hasCommand("start"));
assert(Context.has.command(["help", "start"])(ctx));
assert(ctx.hasCommand(["help", "start"]));
assert(ctx.hasCommand());
assertEquals(ctx.match, "args");
assertFalse(Context.has.command("help")(ctx));
assertFalse(ctx.hasCommand("help"));
Expand All @@ -420,6 +421,7 @@ describe("Context", () => {
ctx = new Context(up, api, me);
assertFalse(Context.has.command("start")(ctx));
assertFalse(ctx.hasCommand("start"));
assertFalse(ctx.hasCommand());

up = {
message: {
Expand All @@ -434,6 +436,7 @@ describe("Context", () => {
ctx = new Context(up, api, me);
assert(Context.has.command("start")(ctx));
assert(ctx.hasCommand("start"));
assert(ctx.hasCommand());

up = {
message: {
Expand All @@ -448,11 +451,13 @@ describe("Context", () => {
ctx = new Context(up, api, me);
assertFalse(Context.has.command("start")(ctx));
assertFalse(ctx.hasCommand("start"));
assertFalse(ctx.hasCommand());

up = { message: { text: "/start" } } as Update;
ctx = new Context(up, api, me);
assertFalse(Context.has.command("start")(ctx));
assertFalse(ctx.hasCommand("start"));
assertFalse(ctx.hasCommand());
});

it("should be able to check for game queries", () => {
Expand Down