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: allow using different prefixes to trigger commands #233

Merged
merged 5 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// Internal dependencies
export { config } from "https://deno.land/[email protected]/dotenv/mod.ts";
export { cleanEnv, num, str } from "https://deno.land/x/[email protected]/mod.ts";
export {
cleanEnv,
makeValidator,
num,
str,
} from "https://deno.land/x/[email protected]/mod.ts";
export {
dirname,
fromFileUrl,
Expand Down
27 changes: 26 additions & 1 deletion env.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
import { cleanEnv, config, num, str } from "./deps.ts";
import { cleanEnv, config, makeValidator, num, str } from "./deps.ts";

await config({ export: true });

// https://stackoverflow.com/a/54256858/12250600
const PREFIX_REGEX = /^[^\p{L}\d\s@#\$]$/u;

const cmdPrefix = makeValidator((input) => {
if (PREFIX_REGEX.test(input)) {
return input;
}
console.warn(
"Falling back to '\\' for COMMAND_PREFIX, a single symbol excluding @, #, $ was expected",
);
return "\\";
});

const inputPrefix = makeValidator((input) => {
if (PREFIX_REGEX.test(input) && input !== Deno.env.get("COMMAND_PREFIX")) {
return input;
}
console.warn(
"Falling back to '>' for INPUT_PREFIX, a single symbol excluding @, #, $ and COMMAND_PREFIX was expected",
);
return "\\";
});

export default cleanEnv(Deno.env.toObject(), {
STRING_SESSION: str(),
APP_ID: num(),
APP_HASH: str(),
COMMAND_PREFIX: cmdPrefix({ default: "\\" }),
INPUT_PREFIX: inputPrefix({ default: ">" }),
});
7 changes: 4 additions & 3 deletions handlers/command_handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HandlerFuncParams } from "./handler.ts";
import { MessageHandler } from "./message_handler.ts";
import env from "../env.ts";

export interface CommandHandlerFuncParams {
args: string[];
Expand Down Expand Up @@ -37,7 +38,7 @@ export class CommandHandler extends MessageHandler<CommandHandlerFuncParams> {
return false;
}
const { text } = event.message;
if (!["\\", ">"].includes(text[0])) {
if (![env.COMMAND_PREFIX, env.INPUT_PREFIX].includes(text[0])) {
return false;
}
const command = text.split(/\s/)[0].slice(1);
Expand All @@ -54,14 +55,14 @@ export class CommandHandler extends MessageHandler<CommandHandlerFuncParams> {
const inputType = message[0];
const reply = await event.message.getReplyMessage();
switch (inputType) {
case "\\":
case env.COMMAND_PREFIX:
input = (this.opts?.rawInput ? message : text)
.split("\n")
.slice(1)
.join("\n")
.trim();
break;
case ">":
case env.INPUT_PREFIX:
if (reply && reply.text) {
input = this.opts?.rawInput ? reply.message : reply.text;
}
Expand Down