From 6bb4dce37ad0b84eeb860cfffabfc3194425081f Mon Sep 17 00:00:00 2001 From: KnorpelSenf Date: Thu, 30 Dec 2021 00:50:34 +0100 Subject: [PATCH 1/3] Prefer substring over substr --- src/composer.ts | 8 ++++---- src/core/client.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/composer.ts b/src/composer.ts index 319cbed9..b335c666 100644 --- a/src/composer.ts +++ b/src/composer.ts @@ -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 += "!"; @@ -384,7 +384,7 @@ export class Composer implements MiddlewareObj { if (cmd.startsWith("/")) { throw new Error( `Do not include '/' when registering command handlers (use '${ - cmd.substr(1) + cmd.substring(1) }' not '${cmd}')`, ); } @@ -401,7 +401,7 @@ export class Composer implements MiddlewareObj { 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("@"); @@ -410,7 +410,7 @@ export class Composer implements MiddlewareObj { 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; diff --git a/src/core/client.ts b/src/core/client.ts index 7a55a192..30ad2c48 100644 --- a/src/core/client.ts +++ b/src/core/client.ts @@ -216,7 +216,7 @@ class ApiClient { 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, ) From c67e6eaf36478ddf380017183fd21567c6e8a442 Mon Sep 17 00:00:00 2001 From: KnorpelSenf Date: Thu, 30 Dec 2021 00:52:37 +0100 Subject: [PATCH 2/3] Do not handle commands in channels --- src/composer.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/composer.ts b/src/composer.ts index b335c666..9eaeef44 100644 --- a/src/composer.ts +++ b/src/composer.ts @@ -391,9 +391,9 @@ export class Composer implements MiddlewareObj { 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 => { - const msg = ctx.message ?? ctx.channelPost; + const msg = ctx.message; const txt = msg.text ?? msg.caption; const entities = msg.entities ?? msg.caption_entities; return entities.some((e) => { @@ -833,7 +833,7 @@ type HearsContext = Filter< >; type CommandContext = Filter< C & { match: string }, - ":entities:bot_command" + "message:entities:bot_command" >; function match( From 21d308b7e2830bef01e6d00ed71f7772db6bc397 Mon Sep 17 00:00:00 2001 From: KnorpelSenf Date: Thu, 30 Dec 2021 00:58:46 +0100 Subject: [PATCH 3/3] Do not regard caption entities --- src/composer.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/composer.ts b/src/composer.ts index 9eaeef44..5cf1f0ca 100644 --- a/src/composer.ts +++ b/src/composer.ts @@ -393,10 +393,8 @@ export class Composer implements MiddlewareObj { }); return this.on("message:entities:bot_command").filter( (ctx): ctx is CommandContext => { - const msg = ctx.message; - const txt = msg.text ?? msg.caption; - const entities = msg.entities ?? msg.caption_entities; - 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);