mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-16 22:21:51 +00:00
Allow overriding default unit for delay strings; use seconds by default for slowmodes
This commit is contained in:
parent
7cc4687e87
commit
1ead037b8a
2 changed files with 11 additions and 10 deletions
|
@ -235,7 +235,7 @@ export class SlowmodePlugin extends ZeppelinPlugin<ISlowmodePluginConfig> {
|
||||||
return;
|
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;
|
const useNativeSlowmode = this.getConfigForChannel(channel).use_native_slowmode && seconds <= NATIVE_SLOWMODE_LIMIT;
|
||||||
|
|
||||||
if (useNativeSlowmode) {
|
if (useNativeSlowmode) {
|
||||||
|
|
19
src/utils.ts
19
src/utils.ts
|
@ -10,12 +10,18 @@ import https from "https";
|
||||||
import tmp from "tmp";
|
import tmp from "tmp";
|
||||||
import { logger } from "knub";
|
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
|
* 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*/;
|
const regex = /^([0-9]+)\s*([wdhms])?[a-z]*\s*/;
|
||||||
let match;
|
let match;
|
||||||
let ms = 0;
|
let ms = 0;
|
||||||
|
@ -24,12 +30,7 @@ export function convertDelayStringToMS(str) {
|
||||||
|
|
||||||
// tslint:disable-next-line
|
// tslint:disable-next-line
|
||||||
while (str !== "" && (match = str.match(regex)) !== null) {
|
while (str !== "" && (match = str.match(regex)) !== null) {
|
||||||
if (match[2] === "w") ms += match[1] * 1000 * 60 * 60 * 24 * 7;
|
ms += match[1] * ((match[2] && delayStringMultipliers[match[2]]) || delayStringMultipliers[defaultUnit]);
|
||||||
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;
|
|
||||||
|
|
||||||
str = str.slice(match[0].length);
|
str = str.slice(match[0].length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue