Files
cortex-bot/core/utils.js
Hepller 4e21d32bd3 v0.9.0
2022-01-02 14:30:47 +03:00

53 lines
1.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** __ __ __ ___ ___
* / / \ |__) | |__ \_/
* \__ \__/ | \ | |___ / \
*
* @copyright © 2021-2022 hepller
*/
/** Функции */
export default 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) {
// Замена некорректных значений
if (likelihood < 0) likelihood = 0
if (likelihood > 100) likelihood = 100
// Возвращение значения
return Math.random() * 100 < likelihood
}
/**
* Останавливает выполнение функции на указанное время
* @param {number} ms Время остановки (в миллисекундах)
*/
static sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
static formatString(text) {
return text.toLowerCase().charAt(0).toUpperCase() + text.slice(1) + '.'
}
}