Skip to content

Commit c3f907f

Browse files
committed
feat: added settings to turn on/off react preference
1 parent a0cc1de commit c3f907f

4 files changed

Lines changed: 92 additions & 37 deletions

File tree

src/commands/autoreact.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Message } from "../../types/message";
2+
import { saveSetting } from "../components/services/settings";
3+
import log from "../components/utils/log";
4+
5+
export const info = {
6+
command: "autoreact",
7+
description: "Enable/disable automatically react on messages based on keywords.",
8+
usage: "autoreact <on|off>",
9+
example: "autoreact on",
10+
role: "admin",
11+
cooldown: 5000,
12+
};
13+
14+
export default async function (msg: Message) {
15+
const query = msg.body.replace(/^autoreact\b\s*/i, "").trim();
16+
if (query.length === 0 && !/(on|off)/.test(query)) {
17+
await msg.reply("Please provide a value its either on or off.");
18+
return;
19+
}
20+
21+
await saveSetting("auto_react", query);
22+
await msg.reply(`Auto React successfuly set \`${query}\`.`);
23+
}

src/commands/reactrepeater.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Message } from "../../types/message";
2+
import { saveSetting } from "../components/services/settings";
3+
import log from "../components/utils/log";
4+
5+
export const info = {
6+
command: "reactrepeater",
7+
description: "Enable/disable automatically repeating react on messages.",
8+
usage: "reactrepeater <on|off>",
9+
example: "reactrepeater on",
10+
role: "admin",
11+
cooldown: 5000,
12+
};
13+
14+
export default async function (msg: Message) {
15+
const query = msg.body.replace(/^reactrepeater\b\s*/i, "").trim();
16+
if (query.length === 0 && !/(on|off)/.test(query)) {
17+
await msg.reply("Please provide a value its either on or off.");
18+
return;
19+
}
20+
21+
await saveSetting("react_repeater", query);
22+
await msg.reply(`Reaction Repeater successfuly set \`${query}\`.`);
23+
}

src/components/events/message.ts

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import emojiRegex from "emoji-regex";
1717
import { funD, happyEE, sadEE, loveEE } from "../../data/reaction";
1818
import { containsAny } from "../utils/string";
1919
import { checkMessage } from "../utils/phishtank";
20+
import { getSetting } from "../services/settings";
2021

2122
const regex = emojiRegex();
2223
const commandPrefix = process.env.COMMAND_PREFIX || "!";
@@ -101,49 +102,54 @@ export default async function (msg: Message, type: string) {
101102
* Override the default function
102103
* react(reaction: string) Promise<void>
103104
*/
104-
const originalReact = msg.react.bind(msg);
105-
msg.react = async (reaction: string): Promise<void> => {
106-
// add delay for more
107-
// humanly like interaction
108-
const min = 2000;
109-
const max = 6000;
110-
const randomMs = Math.floor(Math.random() * (max - min + 1)) + min;
111-
112-
await sleep(randomMs);
113-
const isEmoji = /.*[A-Za-z0-9].*/.test(reaction);
114-
log.info("AutoReact", lid, reaction);
115-
116-
if (Math.random() < 0.1 && !isEmoji)
117-
if (Math.random() < 0.2)
118-
await client.sendMessage(msg.id.remote, reaction);
119-
else await msg.reply(reaction);
120-
else await originalReact(reaction);
121-
};
105+
const [isMustautoReact] = await Promise.all([getSetting("auto_react")]);
106+
if (isMustautoReact && isMustautoReact == "on") {
107+
const originalReact = msg.react.bind(msg);
108+
msg.react = async (reaction: string): Promise<void> => {
109+
// add delay for more
110+
// humanly like interaction
111+
const min = 2000;
112+
const max = 6000;
113+
const randomMs = Math.floor(Math.random() * (max - min + 1)) + min;
114+
115+
await sleep(randomMs);
116+
const isEmoji = /.*[A-Za-z0-9].*/.test(reaction);
117+
log.info("AutoReact", lid, reaction);
118+
119+
if (Math.random() < 0.1 && !isEmoji)
120+
if (Math.random() < 0.2)
121+
await client.sendMessage(msg.id.remote, reaction);
122+
else await msg.reply(reaction);
123+
else await originalReact(reaction);
124+
};
125+
}
122126

123127
/*
124128
*
125129
* Process msg reaction
126130
*/
127-
Promise.resolve().then(async () => {
128-
if (!msg.fromMe) {
129-
const emojis = [
130-
...new Set([...msg.body.matchAll(regex)].map((m) => m[0])),
131-
];
132-
if (emojis.length > 0) {
133-
const react = emojis[Math.floor(Math.random() * emojis.length)];
134-
135-
await msg.react(react);
136-
} else if (containsAny(msg.body, funD)) {
137-
await msg.react("🤣");
138-
} else if (containsAny(msg.body, happyEE)) {
139-
await msg.reply(funD[Math.floor(Math.random() * funD.length)]);
140-
} else if (containsAny(msg.body, sadEE)) {
141-
await msg.react("😭");
142-
} else if (containsAny(msg.body, loveEE)) {
143-
await msg.react("❤️");
131+
if (isMustautoReact && isMustautoReact == "on") {
132+
Promise.resolve().then(async () => {
133+
if (!msg.fromMe) {
134+
const emojis = [
135+
...new Set([...msg.body.matchAll(regex)].map((m) => m[0])),
136+
];
137+
if (emojis.length > 0) {
138+
const react = emojis[Math.floor(Math.random() * emojis.length)];
139+
140+
await msg.react(react);
141+
} else if (containsAny(msg.body, funD)) {
142+
await msg.react("🤣");
143+
} else if (containsAny(msg.body, happyEE)) {
144+
await msg.reply(funD[Math.floor(Math.random() * funD.length)]);
145+
} else if (containsAny(msg.body, sadEE)) {
146+
await msg.react("😭");
147+
} else if (containsAny(msg.body, loveEE)) {
148+
await msg.react("❤️");
149+
}
144150
}
145-
}
146-
});
151+
});
152+
}
147153

148154
/*
149155
* Check if the message starts with the command prefix.

src/components/events/reaction.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import { Client, Reaction } from "whatsapp-web.js";
22
import log from "../utils/log";
33
import sleep from "../utils/sleep";
44
import { isBlocked } from "../services/user";
5+
import { getSetting } from "../services/settings";
56

67
export default async function (client: Client, react: Reaction) {
78
if (react.msgId.fromMe || react.id.fromMe) return;
89
if (!react.reaction?.trim()) return;
910
// ignore react if it is older than 10 seconds
1011
if (react.timestamp < Date.now() / 1000 - 10) return;
12+
const isMustRepeatReact = await getSetting("react_repeater");
13+
if (!isMustRepeatReact || isMustRepeatReact == "off") return;
1114

1215
const senderId = react.senderId.split("@")[0];
1316
const isBlockedUser = await isBlocked(senderId);

0 commit comments

Comments
 (0)