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;
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
19
src/utils.ts
19
src/utils.ts
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue