This commit is contained in:
Hepller
2021-09-12 22:32:54 +03:00
parent a8e1b1b2a6
commit 69da2dd68b
6 changed files with 84 additions and 69 deletions

View File

@@ -1,61 +0,0 @@
/** __ __ __ ___ ___
* / ' / \ |__) | |__ \_/
* \__. \__/ | \ | |___ / \
*
* @copyright © 2021 hepller
*/
// Импорт модулей и функций
const {VK} = require('vk-io')
const Markov = require('markov-generator')
/** Функции */
const Utils = require('./utils')
// Конфиг, паттерн и файл проекта
const config = require('../config')
const data = require('../data')
const project = require('../package')
/** VK API */
const vk = new VK({token: config.token, v: 5.131})
// Префиксы для логирования
const succes_prefix = '\u001B[32m>\u001B[0m'
const warn_prefix = '\u001B[33m>\u001B[0m'
const error_prefix = '\u001B[31m>\u001B[0m'
// Сообщения при запуске
console.log(succes_prefix, `Cortex Bot v${project.version}`)
console.log(succes_prefix, 'VK API connection ...')
// Запуск обработчика событий ВК
vk.updates.startPolling().then(() => console.log(succes_prefix, 'Successfully connected'))
// Обработчик сообщений ВК
vk.updates.on('message_new', async ctx => {
// Игнорирование лишних сообщений
if (!ctx.isChat || ctx.isOutbox || ctx.isGroup) return
// Генерация сообщения с определенным шансом
if (Utils.getWithChance(config.chance)) {
// Статус набора текста
await ctx.setActivity()
/** Сгенерированный текст */
const sentence = new Markov({input: data, minLenght: config.min_words}).makeChain()
// Предупреждение при превышении лимита символов
if (sentence.split('').length > config.max_symbols) return console.warn(warn_prefix, `#${ctx.chatId} Symbols limit exceeded (${sentence.split('').length}/${config.max_symbols})`)
// Отправка сообщения
await ctx.send(sentence)
.then(() => console.log(succes_prefix, `#${ctx.chatId} Text generated (${Utils.getTimeString()})`))
}
})
// Обработка возможных ошибок
process.on('uncaughtException', error => console.error(error_prefix, `Error: ${error.message}`))
process.on('unhandledRejection', error => console.error(error_prefix, `Error: ${error.message}`))

View File

@@ -1,35 +0,0 @@
/** __ __ __ ___ ___
* / ' / \ |__) | |__ \_/
* \__. \__/ | \ | |___ / \
*
* @copyright © 2021 hepller
*/
/** Функции */
module.exports = class Utils {
/** Получает текущее время в формате `hh:mm:ss` */
static getTimeString() {
// Переменные
let hours = new Date().getHours()
let minutes = new Date().getMinutes()
let seconds = new Date().getSeconds()
// Исправление одинарных символов
if (hours < 10) hours = `0${hours}`
if (minutes < 10) minutes = `0${minutes}`
if (seconds < 10) seconds = `0${seconds}`
// Возвращение строки
return `${hours}:${minutes}:${seconds}`
}
/**
* Возвращает `true` с определенным шансом
* @param {number} likelihood Вероятность (%)
*/
static getWithChance(likelihood) {
return Math.random() * 100 < likelihood
}
}