Add /wait command to commands input

This commit is contained in:
Max Leiter 2022-05-01 02:31:33 -07:00
parent e4e57b00c0
commit d9ce50583e
No known key found for this signature in database
GPG key ID: A3512F2F2F17EBDA
2 changed files with 35 additions and 10 deletions

View file

@ -271,7 +271,11 @@
class="tooltipped tooltipped-ne tooltipped-no-delay" class="tooltipped tooltipped-ne tooltipped-no-delay"
aria-label="One /command per line. aria-label="One /command per line.
Each command will be executed in Each command will be executed in
the server tab on every connection." the server tab on every connection.
If you want to wait for a time between
commands, you can use `/wait <seconds>`,
like `/wait 5`."
> >
<button class="extra-help" /> <button class="extra-help" />
</span> </span>

View file

@ -6,6 +6,8 @@ const Msg = require("../../models/msg");
const Chan = require("../../models/chan"); const Chan = require("../../models/chan");
const Helper = require("../../helper"); const Helper = require("../../helper");
const MAX_DELAY_LENGTH_SECONDS = 10;
module.exports = function (irc, network) { module.exports = function (irc, network) {
const client = this; const client = this;
@ -39,15 +41,34 @@ module.exports = function (irc, network) {
let delay = 1000; let delay = 1000;
if (Array.isArray(network.commands)) { if (Array.isArray(network.commands)) {
network.commands.forEach((cmd) => { const commands = network.commands.map(
setTimeout(function () { (cmd) =>
new Promise((resolve) => {
if (cmd.toLowerCase().startsWith("/wait")) {
try {
delay = parseInt(cmd.substring(6), 10) * 1000;
if (delay > MAX_DELAY_LENGTH_SECONDS * 1000) {
delay = MAX_DELAY_LENGTH_SECONDS * 1000;
}
} catch {
delay = 1000;
}
resolve();
} else {
setTimeout(() => {
client.input({ client.input({
target: network.channels[0].id, target: network.channels[0].id,
text: cmd, text: cmd,
}); });
resolve();
}, delay); }, delay);
delay += 1000; }
}); })
);
Promise.all(commands);
} }
network.channels.forEach((chan) => { network.channels.forEach((chan) => {