3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00

Allow overriding default unit for delay strings; use seconds by default for slowmodes

This commit is contained in:
Dragory 2019-04-14 14:05:16 +03:00
parent 7cc4687e87
commit 1ead037b8a
2 changed files with 11 additions and 10 deletions

View file

@ -235,7 +235,7 @@ export class SlowmodePlugin extends ZeppelinPlugin<ISlowmodePluginConfig> {
return;
}
const seconds = Math.ceil(convertDelayStringToMS(args.time) / 1000);
const seconds = Math.ceil(convertDelayStringToMS(args.time, "s") / 1000);
const useNativeSlowmode = this.getConfigForChannel(channel).use_native_slowmode && seconds <= NATIVE_SLOWMODE_LIMIT;
if (useNativeSlowmode) {

View file

@ -10,12 +10,18 @@ import https from "https";
import tmp from "tmp";
import { logger } from "knub";
const delayStringMultipliers = {
w: 1000 * 60 * 60 * 24 * 7,
d: 1000 * 60 * 60 * 24,
h: 1000 * 60 * 60,
m: 1000 * 60,
s: 1000,
};
/**
* Turns a "delay string" such as "1h30m" to milliseconds
* @param {String} str
* @returns {Number}
*/
export function convertDelayStringToMS(str) {
export function convertDelayStringToMS(str, defaultUnit = "m"): number {
const regex = /^([0-9]+)\s*([wdhms])?[a-z]*\s*/;
let match;
let ms = 0;
@ -24,12 +30,7 @@ export function convertDelayStringToMS(str) {
// tslint:disable-next-line
while (str !== "" && (match = str.match(regex)) !== null) {
if (match[2] === "w") ms += match[1] * 1000 * 60 * 60 * 24 * 7;
else if (match[2] === "d") ms += match[1] * 1000 * 60 * 60 * 24;
else if (match[2] === "h") ms += match[1] * 1000 * 60 * 60;
else if (match[2] === "s") ms += match[1] * 1000;
else if (match[2] === "m" || !match[2]) ms += match[1] * 1000 * 60;
ms += match[1] * ((match[2] && delayStringMultipliers[match[2]]) || delayStringMultipliers[defaultUnit]);
str = str.slice(match[0].length);
}