Skip to content

Commit

Permalink
don't save timeouts until message sent properly
Browse files Browse the repository at this point in the history
  • Loading branch information
meadowsys committed Nov 20, 2023
1 parent 0888f5b commit 48ea1dd
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 35 deletions.
23 changes: 20 additions & 3 deletions lib/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,26 @@ const Database = {
/**
* @param { string } userID
* @param { string } type
* @deprecated use set_timeout_with_commit instead
*/
setTimeout(userID, type) {
(cooldowns[type] = cooldowns[type] || []).includes(userID) || cooldowns[type].push(userID);
logger.warn("deprecated method Database.setTimeout called");
Database.set_timeout_with_commit(userID, type)();
},

/**
* returns a function that can be called to "commit" a timeout, timeout will not
* be added before it is called
*
* @param { string } user_id
* @param { string } type
*/
set_timeout_with_commit(user_id, type) {
return () => {
logger.debug(`adding timeout "${type}" for user id "${user_id}"`);
(cooldowns[type] = cooldowns[type] || []).includes(user_id) || cooldowns[type].push(user_id);
return Promise.resolve();
}
}
};

Expand All @@ -49,13 +66,13 @@ function scheduleClearDBTask() {
logger.debug(`clear task scheduled, delay is ${delay}ms`)

setTimeout(() => {
logger.debug("clearing db");
for (const key in cooldowns) {
delete cooldowns[key];
}
logger.debug("cleared db");

// delay to ensure its next day
setTimeout(scheduleClearDBTask, 10000).unref();
setTimeout(scheduleClearDBTask, 10_000).unref();
}, delay).unref();

}
Expand Down
35 changes: 21 additions & 14 deletions lib/Tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,12 +515,17 @@ const Tools = {
* | import("discord.js").DMChannel
* | import("discord.js").NewsChannel
* } target
* @param { Array<string> } data
* @param { Array<number> } delay_in
* @param { Array<string> } parse
* @param {{ inter: boolean }} interrupt
*/
listSender(target, data, delay_in, parse = [], interrupt = { inter:false }) {
return new Promise(resolve => {

/** @type { number } */
let delay;
if (delay_in.length === 1) {
// @ts-expect-error this is kinda weird, but it works
delay_in = delay_in[0];
}
if (Array.isArray(delay_in)) {
Expand All @@ -531,27 +536,29 @@ const Tools = {

const pre = data[0];
const count = (pre.match(/%s/g) || []).length;
const content = Tools.parseReply(pre, parse.slice(0, count));
const content = Tools.parseReply(pre, ...parse.slice(0, count));
if (!interrupt.inter) {
if (content === "") {
if (data.length > 1) {
setTimeout(function() {
Tools.listSender(target, data.slice(1), delay_in, parse.slice(count), interrupt);
resolve();
setTimeout(() => {
Tools.listSender(target, data.slice(1), delay_in, parse.slice(count), interrupt)
.then(resolve);
}, delay);
} else {
resolve();
}
} else {
target.send(content).then(function() {
if (data.length > 1) {
setTimeout(function() {
Tools.listSender(target, data.slice(1), delay_in, parse.slice(count), interrupt).then(resolve);
}, delay);
} else {
resolve();
}
});
target.send(content)
.then(() => {
if (data.length > 1) {
setTimeout(() => {
Tools.listSender(target, data.slice(1), delay_in, parse.slice(count), interrupt)
.then(resolve);
}, delay);
} else {
resolve();
}
});
}
} else {
resolve();
Expand Down
39 changes: 25 additions & 14 deletions modules/Boop/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ module.exports = class Boop extends Module {

Database.getTimeout(msg.author.id, "megaboop").then((results) => {
if (results.length == 0) {
Database.setTimeout(msg.author.id, "megaboop");
return this.megaBoopLoader(msg, user);
let commit = Database.set_timeout_with_commit(msg.author.id, "megaboop");
return this.megaBoopLoader(msg, user, commit);
} else {
const cooldownMessage = Tools.parseReply(this.config.cooldownMessageMegaBoop, msg.author.toString());
msg.channel.send(cooldownMessage);
Expand Down Expand Up @@ -217,27 +217,34 @@ module.exports = class Boop extends Module {
/**
* @param { import("discord.js").Message } msg
* @param { import("discord.js").User } user
* @param { () => Promise<void> } commit
*/
megaBoopLoader(msg, user) {
megaBoopLoader(msg, user, commit) {
const roll = Tools.getRandomIntFromInterval(0, 100);

if (roll === 100) {
this.hyperBoop(msg, user);
this.hyperBoop(msg, user, commit);
} else if (roll >= 0 && roll <= 5) {
this.megaBoop(msg, user, "miss");
this.megaBoop(msg, user, "miss", commit);
} else if (roll >= 90 && roll <= 99) {
this.megaBoop(msg, user, "crit");
this.megaBoop(msg, user, "crit", commit);
} else {
this.megaBoop(msg, user);
this.megaBoop(msg, user, "hit", commit);
}
}

/**
* @param { import("discord.js").Message } msg
* @param { import("discord.js").User } user
* @param { "hit" | "miss" | "crit" } type
* @param { () => Promise<void> } commit
*/
megaBoop(msg, user, type = "hit") {
let random, damage, answer = "", limit;
megaBoop(msg, user, type, commit) {
let random;
let damage;
/** @type { string | Array<string> } */
let answer = "";
let limit;
this.interrupt.inter = false;
switch (type) {
case "miss":
Expand All @@ -251,7 +258,7 @@ module.exports = class Boop extends Module {
answer = this.config.megaBoopCritAnswer[random];
limit = 90;
break;
default:
case "hit":
random = Tools.getRandomIntFromInterval(0, this.config.megaBoopAnswer.length - 1);
damage = Tools.getRandomIntFromInterval(9000, 12000);
answer = this.config.megaBoopAnswer[random];
Expand All @@ -272,9 +279,11 @@ module.exports = class Boop extends Module {
this.counterWindow(delay + init_delay);
setTimeout(() => {
if (Array.isArray(answer)) {
Tools.listSender(msg.channel, answer, [delay, 2000, 1000], [user, damage], this.interrupt);
Tools.listSender(msg.channel, answer, [delay, 2000, 1000], [user, damage], this.interrupt)
.then(commit)
} else {
msg.channel.send(Tools.parseReply(answer, user.toString(), damage));
msg.channel.send(Tools.parseReply(answer, user.toString(), damage))
.then(commit);
}
}, init_delay);

Expand All @@ -299,14 +308,16 @@ module.exports = class Boop extends Module {
/**
* @param { import("discord.js").Message } msg
* @param { import("discord.js").User } user
* @param { () => Promise<void> } commit
*/
hyperBoop(msg, user) {
hyperBoop(msg, user, commit) {
const random = Tools.getRandomIntFromInterval(0, this.config.hyperBoopAnswer.length - 1);
const ans = this.config.hyperBoopAnswer[random];
if (Array.isArray(ans)) {
Tools.listSender(msg.channel, ans, [1000, 2000, 4000, 4000, 2000, 2000, 2000, 2000, 3000], [user]);
} else {
msg.channel.send(Tools.parseReply(ans, user.toString()));
msg.channel.send(Tools.parseReply(ans, user.toString()))
.then(commit);
}

Application.modules.Discord.setMessageSent();
Expand Down
13 changes: 9 additions & 4 deletions modules/Hug/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ module.exports = class Hug extends Module {

Database.getTimeout(msg.author.id, "megahug").then((results) => {
if (results.length == 0) {
Database.setTimeout(msg.author.id, "megahug");
return this.megaHug(msg, this.config.megaHugAnswer, user);
let commit = Database.set_timeout_with_commit(msg.author.id, "megahug");
return this.megaHug(msg, this.config.megaHugAnswer, user, commit);
} else {
const cooldownMessage = Tools.parseReply(this.config.cooldownMessageMegaHug, [msg.author]);
msg.channel.send(cooldownMessage);
Expand All @@ -99,11 +99,16 @@ module.exports = class Hug extends Module {
}
}

megaHug(msg, answerType, target = "") {
/**
* @param { import("discord.js").Message } msg
* @param { () => Promise<void> } commit
*/
megaHug(msg, answerType, target = "", commit) {
const random = Tools.getRandomIntFromInterval(0, answerType.length - 1);
const answer = Tools.parseReply(answerType[random], [target, this.hugEmoji]);

msg.channel.send(answer);
msg.channel.send(answer)
.then(commit);

Application.modules.Overload.overload("hug");
Application.modules.Discord.setMessageSent();
Expand Down

0 comments on commit 48ea1dd

Please sign in to comment.