1
0
Fork 0
mirror of https://github.com/muerwre/vk-tg-bot.git synced 2025-04-29 00:46:41 +07:00

add roll command

This commit is contained in:
Fedor Katurov 2023-12-30 17:02:36 +07:00
parent 6230217741
commit 2a08ec9f86
3 changed files with 150 additions and 2 deletions

View file

@ -8,6 +8,8 @@ import { ExtraReplyMessage } from "telegraf/typings/telegram-types";
// import SocksProxyAgent from 'socks-proxy-agent';
const maxMessageAge = 3 * 60e3; // skip messages older than this seconds
export class TelegramService {
public readonly bot: Telegraf;
public readonly webhook: WebhookConfig = {};
@ -18,7 +20,7 @@ export class TelegramService {
telegram: {
webhookReply: true,
apiMode: "bot",
// agent, // TODO: add proxy support
// agent, // TODO: add proxy support if they block it
},
};
@ -158,4 +160,40 @@ export class TelegramService {
!!username && !!this.props.owners && this.props.owners.includes(username)
);
};
public hears = (
what: string | RegExp,
callback: (
text: string
) => string | Promise<string | undefined> | undefined | void
) =>
this.bot.hears(what, async (ctx) => {
let text: string | void | undefined = "%% not received %%";
try {
const age = Date.now() - ctx.message.date * 1000;
const message = ctx.update.message.text;
if (age > maxMessageAge) {
console.warn(
`skipped message "${message}", since its age ${age / 1000} seconds`
);
return;
}
text = await callback(message);
if (!text) {
return;
}
ctx.reply(text, { parse_mode: "MarkdownV2" });
} catch (error) {
console.warn(
`error replying to ${what} (${ctx.update.message.text}) with message "${text}"`,
error
);
}
});
}