stalkee
Version:
a Telegram bot who can send voice messages via inline mode added by admin with sorting them by numbers of uses
102 lines (101 loc) • 4.33 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Bot = void 0;
const telegraf_1 = require("telegraf");
const utils_1 = require("../utils");
const middlewares = __importStar(require("./middlewares"));
const filters = __importStar(require("./filters"));
const commands = __importStar(require("./commands"));
const handlers = __importStar(require("./handlers"));
const actions = __importStar(require("./actions"));
class Bot {
constructor(token) {
this.options = {};
this.bot = new telegraf_1.Telegraf(token);
this.bot.catch((err) => utils_1.logger.error(err.message, 'bot'));
this.bot.use(middlewares.update, middlewares.setLocale);
this.bot.command('start', commands.start);
this.bot.command('info', filters.replyToVoice, commands.info);
this.bot
.command('add', filters.admin, filters.replyToVoiceOrAudio, commands.add)
.command('remove', filters.admin, filters.replyToVoice, commands.remove)
.command('quote', filters.admin, filters.replyToVoice, commands.quote)
.command('actor', filters.admin, filters.replyToVoice, commands.actor)
.command('location', filters.admin, filters.replyToVoice, commands.location);
this.bot
.action(/^lang:([^:]+)$/, actions.lang)
.action(/^remove:([^:]+)$/, actions.remove);
this.bot
.on('inline_query', handlers.inlineQuery)
.on('chosen_inline_result', handlers.chosenInlineResult);
}
static _buildCommands(category, locale) {
return Bot.commands[category].map(command => ({
command: command,
description: utils_1.i18n[locale].commands[command].descr
}));
}
async getMe() {
this.botInfo = await this.bot.telegram.getMe();
return this.botInfo;
}
async start(options) {
await this.bot.launch(options);
this.options = options;
this.botInfo = await this.bot.telegram.getMe();
await this.setMyCommands();
}
async stop() {
await this.bot.stop();
}
async reload() {
await this.bot.stop();
await this.start(this.options);
}
/**
* @deprecated with Bot API 6
*/
async setMode(mode) {
utils_1.logger.warn('Setting mode is deprecated!', 'bot.set_mode');
}
async setMyCommands() {
await this.bot.telegram.setMyCommands(Bot._buildCommands('regular', utils_1.config.bot.default_locale));
for (const locale of utils_1.config.bot.locales) {
await this.bot.telegram.setMyCommands(Bot._buildCommands('regular', locale), {
language_code: utils_1.i18n[locale].iso_639_1_code
});
}
const adminCommands = Bot._buildCommands('admin', utils_1.config.bot.default_locale);
for (const userId of utils_1.config.bot.admins) {
await this.bot.telegram.setMyCommands(adminCommands, { scope: { type: 'chat', chat_id: userId } });
}
}
}
exports.Bot = Bot;
Bot.commands = {
regular: ['start', 'info'],
admin: ['start', 'info', 'add', 'remove', 'quote', 'actor', 'location'],
};