diff --git a/index.js b/index.js index 57fbcbb..a77bb91 100644 --- a/index.js +++ b/index.js @@ -4,43 +4,59 @@ require('child_process').execSync('node init.js', { stdio: 'inherit' }); const { inspect } = require('util'); - -const print = value => - console.log(inspect(value, { colors: true, depth: null })); - const Telegraf = require('telegraf'); -const { loadJSON } = require('./utils/json'); +// Utils const { link } = require('./utils/tg'); +const { loadJSON } = require('./utils/json'); +const { print, logError } = require('./utils/log'); +// DBs const bans = require('./bans'); const warns = require('./warns'); const admins = require('./admins'); +const replyOptions = { + parse_mode: 'HTML', + disable_web_page_preview: true +}; + const config = loadJSON('config.json'); const bot = new Telegraf(config.token); bot.use((ctx, next) => - (print(ctx.update), next())); + (print(ctx.update), + next())); -bot.command('warn', ({ message, reply }) => { - admins.isAdmin(message.from).then(isAdmin => { - if (isAdmin) { - if (message.reply_to_message) { - const userToWarn = message.reply_to_message.from; - warns.warn(userToWarn, message.text) - .then(warns => - reply( - `${link(userToWarn)} warned! (${warns}/3)`, - { - parse_mode: 'HTML', - disable_web_page_preview: true, - reply_to: message.reply_to_message.message_id - })); - } - } - }); +bot.command('adminme', ctx => + (admins.admin(ctx.from), + ctx.reply('Admined!'))); + +bot.command('warn', async ({ message, chat, reply }) => { + if (!await admins.isAdmin(message.from)) { + return; + } + if (!message.reply_to_message) { + return; + } + + const messageToWarn = message.reply_to_message; + const userToWarn = messageToWarn.from; + const reason = message.text.split(' ').slice(1).join(' ').trim(); + + if (reason.length === 0) { + return reply('Need a reason'); + } + + const warnCount = await warns.warn(userToWarn, reason); + return Promise.all([ + bot.telegram.deleteMessage(chat.id, messageToWarn.message_id), + bot.telegram.deleteMessage(chat.id, message.message_id), + reply( + `${link(userToWarn)} warned! (${warnCount}/3)\nReason: ${reason}`, + replyOptions) + ]).catch(logError); }); bot.startPolling(); diff --git a/utils/log.js b/utils/log.js new file mode 100644 index 0000000..04510b3 --- /dev/null +++ b/utils/log.js @@ -0,0 +1,12 @@ +'use strict'; + +const logError = err => + console.error(`${err.name}: ${err.message}`); + +const print = value => + console.log(inspect(value, { colors: true, depth: null })); + +module.exports = { + logError, + print +}; \ No newline at end of file