Files
tg-chat-bot/src/commands/ae.ts
T
2026-05-13 10:18:54 +03:00

62 lines
1.9 KiB
TypeScript

import {Command} from "../base/command";
import {Message} from "typescript-telegram-bot-api";
import {errorPlaceholder, logError, oldSendMessage} from "../util/utils";
import {Requirements} from "../base/requirements";
import {Requirement} from "../base/requirement";
import {Environment} from "../common/environment";
export class Ae extends Command {
argsMode = "required" as const;
command = ["ae"];
title = Environment.commandTitles.ae;
description = Environment.commandDescriptions.ae;
requirements = Requirements.Build(Requirement.BOT_CREATOR);
async execute(msg: Message, params?: RegExpExecArray) {
const match = params?.[3] || "";
try {
let result = this.executeEvaluation(match);
await oldSendMessage(msg, result).catch(async () => await errorPlaceholder(msg));
} catch (e: unknown) {
const error = e instanceof Error ? e : new Error(String(e));
const text = error.message.toString();
if (text.includes("is not defined")) {
await oldSendMessage(msg, Environment.variableNotDefinedText).catch(logError);
return;
}
logError(`${text}
* Stacktrace: ${error.stack}`);
await oldSendMessage(msg, text).catch(logError);
}
}
executeEvaluation(evaluation: string): string {
try {
let e = eval(evaluation);
e = ((typeof e == "string") ? e : JSON.stringify(e));
return e;
} catch (e: unknown) {
const error = e instanceof Error ? e : new Error(String(e));
const text = error.message.toString();
if (text.includes("is not defined")) {
return Environment.evaluationVariableNotDefinedText;
}
logError(`${text}
* Stacktrace: ${error.stack}`);
return text;
}
}
}