From a8e1b1b2a66a4761a5cfc0d1d90d7297541962ab Mon Sep 17 00:00:00 2001 From: Hepller Date: Thu, 9 Sep 2021 14:48:52 +0300 Subject: [PATCH] v0.7.2 --- bot.js => core/core.js | 17 ++++++++++------- core/utils.js | 35 +++++++++++++++++++++++++++++++++++ package.json | 4 ++-- 3 files changed, 47 insertions(+), 9 deletions(-) rename bot.js => core/core.js (86%) create mode 100644 core/utils.js diff --git a/bot.js b/core/core.js similarity index 86% rename from bot.js rename to core/core.js index 7536661..b84350c 100644 --- a/bot.js +++ b/core/core.js @@ -9,10 +9,13 @@ 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') +const config = require('../config') +const data = require('../data') +const project = require('../package') /** VK API */ const vk = new VK({token: config.token, v: 5.131}) @@ -36,10 +39,10 @@ vk.updates.on('message_new', async ctx => { if (!ctx.isChat || ctx.isOutbox || ctx.isGroup) return // Генерация сообщения с определенным шансом - if (Math.random() * 100 < config.chance) { + if (Utils.getWithChance(config.chance)) { // Статус набора текста - ctx.setActivity() + await ctx.setActivity() /** Сгенерированный текст */ const sentence = new Markov({input: data, minLenght: config.min_words}).makeChain() @@ -48,8 +51,8 @@ vk.updates.on('message_new', async ctx => { if (sentence.split('').length > config.max_symbols) return console.warn(warn_prefix, `#${ctx.chatId} Symbols limit exceeded (${sentence.split('').length}/${config.max_symbols})`) // Отправка сообщения - ctx.send(sentence) - .then(() => console.log(succes_prefix, `#${ctx.chatId} Text generated`)) + await ctx.send(sentence) + .then(() => console.log(succes_prefix, `#${ctx.chatId} Text generated (${Utils.getTimeString()})`)) } }) diff --git a/core/utils.js b/core/utils.js new file mode 100644 index 0000000..5430141 --- /dev/null +++ b/core/utils.js @@ -0,0 +1,35 @@ +/** __ __ __ ___ ___ + * / ' / \ |__) | |__ \_/ + * \__. \__/ | \ | |___ / \ + * + * @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 + } +} \ No newline at end of file diff --git a/package.json b/package.json index ea8f70c..f124edc 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "cortex-bot", - "version": "0.7.1", + "version": "0.7.2", "description": "vk text generator bot", - "main": "bot.js", + "main": "core/core.js", "repository": { "type": "git", "url": "git+https://github.com/hepller/cortex-bot.git"