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

Do not handle commands from channels #147

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
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
18 changes: 8 additions & 10 deletions src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function generateBotErrorMessage(error: unknown) {
msg += `: ${error}`;
break;
case "string":
msg += `: ${String(error).substr(0, 50)}`;
msg += `: ${String(error).substring(0, 50)}`;
break;
default:
msg += "!";
Expand Down Expand Up @@ -384,24 +384,22 @@ export class Composer<C extends Context> implements MiddlewareObj<C> {
if (cmd.startsWith("/")) {
throw new Error(
`Do not include '/' when registering command handlers (use '${
cmd.substr(1)
cmd.substring(1)
}' not '${cmd}')`,
);
}
const set = cmd.indexOf("@") === -1 ? noAtCommands : atCommands;
set.add(cmd);
});
return this.on(":entities:bot_command").filter(
return this.on("message:entities:bot_command").filter(
(ctx): ctx is CommandContext<C> => {
const msg = ctx.message ?? ctx.channelPost;
const txt = msg.text ?? msg.caption;
const entities = msg.entities ?? msg.caption_entities;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This not only removes support for commands in channels but also for commands on media messages with captions?

Not sure if that even works normally, never tried that.

Copy link
Member Author

@KnorpelSenf KnorpelSenf Dec 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grammY used to support commands in captions in its early days, but a lot of people in the community complained that it is not intuitive for command handlers to trigger when e.g. an image caption contains a command. Hence, the support was remove by changing the filter query from ::bot_command to :entities:bot_command.

The code that was removed here was essentially dead code.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can mix it, it's perfect!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mix? Do you mean merge?

return entities.some((e) => {
const txt = ctx.message.text;
return ctx.message.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.substr(cmd.length + 1).trimStart();
ctx.match = txt.substring(cmd.length + 1).trimStart();
return true;
}
const index = cmd.indexOf("@");
Expand All @@ -410,7 +408,7 @@ export class Composer<C extends Context> implements MiddlewareObj<C> {
if (atTarget !== ctx.me.username) return false;
const atCommand = cmd.substring(0, index);
if (noAtCommands.has(atCommand)) {
ctx.match = txt.substr(cmd.length + 1).trimStart();
ctx.match = txt.substring(cmd.length + 1).trimStart();
return true;
}
return false;
Expand Down Expand Up @@ -833,7 +831,7 @@ type HearsContext<C extends Context> = Filter<
>;
type CommandContext<C extends Context> = Filter<
C & { match: string },
":entities:bot_command"
"message:entities:bot_command"
>;

function match<C extends Context>(
Expand Down
2 changes: 1 addition & 1 deletion src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class ApiClient<R extends RawApi> {
if (this.options.apiRoot.endsWith("/")) {
throw new Error(
`Remove the trailing '/' from the 'apiRoot' option (use '${
this.options.apiRoot.substr(
this.options.apiRoot.substring(
0,
this.options.apiRoot.length - 1,
)
Expand Down