Skip to content

Commit 5a6bef5

Browse files
committed
feat: reduce user points when they get banned
1 parent 0ced4d7 commit 5a6bef5

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/commands/fbdl.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,21 @@ export default async function (msg: Message) {
3131
if (!result.url)
3232
return await msg.reply("No video found at the provided URL.");
3333

34-
const response = await axios.get(result.hd, { responseType: "arraybuffer" });
34+
const response = await axios.get(result.hd || result.sd, {
35+
responseType: "arraybuffer",
36+
});
3537

3638
const tempDir = "./.temp";
37-
await fs.mkdirSync(tempDir, { recursive: true });
39+
fs.mkdirSync(tempDir, { recursive: true });
3840

3941
const tempPath = `${tempDir}/fbdl_${Date.now()}.mp4`;
40-
await fs.writeFileSync(tempPath, response.data);
42+
fs.writeFileSync(tempPath, response.data);
4143

4244
const audioBuffer = fs.readFileSync(tempPath);
4345
const media = new MessageMedia(
4446
"audio/mpeg",
4547
audioBuffer.toString("base64"),
46-
`${result.title}.mp4`
48+
`${result.title}.mp4`,
4749
);
4850

4951
await msg.reply(media, msg.from);

src/components/events/message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export default async function (msg: Message) {
7979
* Rate limit commands to prevent abuse.
8080
*/
8181
if (!msg.fromMe) {
82-
const rate = await rateLimiter(msg.from);
82+
const rate = await rateLimiter(msg);
8383
if (rate) return;
8484
if (rate === null) {
8585
msg.reply("Please wait a minute or so.");

src/components/utils/rateLimiter.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { Message } from "whatsapp-web.js";
12
import redis from "../redis";
23
import log from "./log";
4+
import { prisma } from "../prisma";
35

46
const LIMIT = 5;
57
const BASE_WINDOW_MS = 30 * 1000;
@@ -10,8 +12,9 @@ function getKey(number: string) {
1012
}
1113

1214
export default async function rateLimiter(
13-
number: string,
15+
msg: Message,
1416
): Promise<boolean | null> {
17+
const number = msg.from;
1518
const now = Date.now();
1619
const key = getKey(number);
1720

@@ -47,7 +50,19 @@ export default async function rateLimiter(
4750
`User ${number} blocked until ${new Date(entry.penaltyUntil).toLocaleTimeString()}`,
4851
);
4952

50-
await redis.set(key, JSON.stringify(entry));
53+
await Promise.all([
54+
redis.set(key, JSON.stringify(entry)),
55+
prisma.user.update({
56+
where: {
57+
lid: msg.author ? msg.author.split("@")[0] : msg.from.split("@")[0],
58+
},
59+
data: {
60+
quizAnswered: {
61+
decrement: 10,
62+
},
63+
},
64+
}),
65+
]);
5166
if (entry.penaltyCount === 1) return null;
5267
return true;
5368
}

0 commit comments

Comments
 (0)