Skip to content

Commit a94b7c2

Browse files
committed
fix: address issues with riddle and replace deprecated ex: with expiration in redis
1 parent 1db4d52 commit a94b7c2

10 files changed

Lines changed: 62 additions & 39 deletions

File tree

src/commands/quiz.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ export default async function (msg: Message): Promise<void> {
3030
}
3131

3232
const messageReturn = await msg.reply(text);
33-
redis.set(
33+
await redis.set(
3434
`quiz:${messageReturn.id.id}`,
3535
JSON.stringify({ quiz_id: id.toString() }),
3636
{
37-
EX: 3600, // 1 hour
37+
expiration: {
38+
type: "EX",
39+
value: 3600, // 1 hour
40+
},
3841
},
3942
);
40-
log.info("quiz", `Quiz question sent: ${response.question}`);
4143
}

src/commands/riddle.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ export default async function (msg: Message): Promise<void> {
2121
`;
2222

2323
const messageReturn = await msg.reply(text);
24-
redis.set(
24+
await redis.set(
2525
`riddle:${messageReturn.id.id}`,
2626
JSON.stringify({ riddle_id: id.toString() }),
2727
{
28-
EX: 3600, // 1 hour
28+
expiration: {
29+
type: "EX",
30+
value: 3600,
31+
},
2932
},
3033
);
31-
log.info("riddle", `Riddle sent: ${response.question}`);
3234
}

src/components/ai/agentHandler.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ export default async function (
113113
}
114114

115115
if (isQueryCachingEnabled && result) {
116-
await redis.set(cacheKey, result, { EX: queryCachingTTL });
116+
await redis.set(cacheKey, result, {
117+
expiration: {
118+
type: "EX",
119+
value: queryCachingTTL,
120+
},
121+
});
117122
}
118123
return result;
119124
}

src/components/events/message.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { findOrCreateUser, getBlockUser } from "../services/user";
1212
import { client } from "../client";
1313
import Font from "../utils/font";
1414
import quiz from "../utils/quiz";
15-
import riddle from "../utils/quiz";
15+
import riddle from "../utils/riddle";
1616
import { getSetting } from "../services/settings";
1717
import { errors, mentionResponses } from "../utils/data";
1818
import { funD, happyEE, sadEE, loveEE } from "../../data/reaction";
@@ -110,15 +110,12 @@ export default async function (msg: Message, type: string): Promise<void> {
110110
return;
111111

112112
if (!handler) {
113-
if (!msg.hasQuotedMsg) return;
114-
115-
const quoted = await msg.getQuotedMessage();
116113
if (rateLimitResult.status || rateLimitResult.value.timestamps.length > 5)
117114
return;
118115

119116
await Promise.allSettled([
120-
quiz(msg, quoted),
121-
riddle(msg, quoted),
117+
quiz(msg),
118+
riddle(msg),
122119
queue.add(() => InstantDownloader(msg)),
123120
(async () => {
124121
// override the msg!

src/components/events/reaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import * as Sentry from "@sentry/node";
99
export default async function (client: Client, react: Reaction): Promise<void> {
1010
if (react.msgId.fromMe || react.id.fromMe) return;
1111
if (!react.reaction?.trim()) return;
12-
// ignore react if it is older than 10 seconds
13-
if (react.timestamp < Date.now() / 1000 - 10) return;
12+
// ignore react if it is older than 60 seconds
13+
if (react.timestamp < Date.now() / 1000 - 60) return;
1414

1515
const senderId = react.senderId.split("@")[0];
1616
/*

src/components/utils/instantdl/downloader.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ export async function InstantDownloader(msg: Message): Promise<void> {
5252
return await YoutubeShortsInstantDownloader(query);
5353
}
5454
})(),
55-
redis.set(key, "1", { EX: 3600 }),
55+
redis.set(key, "1", {
56+
expiration: {
57+
type: "EX",
58+
value: 3600, // 1 hour
59+
},
60+
}),
5661
]);
5762

5863
if (!video) {

src/components/utils/quiz.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import { quiz, done, wrong } from "../utils/data";
44
import log from "../utils/log";
55
import redis from "../redis";
66

7-
export default async function (
8-
msg: Message,
9-
quoted: Message,
10-
): Promise<boolean> {
7+
export default async function (msg: Message): Promise<void> {
118
try {
12-
if (!quoted.body) return false;
9+
if (!msg.hasQuotedMsg) return;
10+
11+
const quoted: Message = await msg.getQuotedMessage();
12+
if (!quoted.body) return;
1313

1414
const key = `quiz:${quoted.id.id}`;
1515
const result = await redis.get(key);
16-
if (!result) return false;
16+
if (!result) return;
1717

1818
const quizAttempt = JSON.parse(result);
1919
const question = quiz[parseInt(quizAttempt.quiz_id)];
@@ -46,8 +46,5 @@ export default async function (
4646
log.info("QuizAnswered", "Wrong", quoted.body),
4747
]);
4848
}
49-
return true;
5049
} catch (error: any) {}
51-
52-
return false;
5350
}

src/components/utils/rateLimiter.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ export async function resetRateLimit(lid: string): Promise<void> {
1919
`rate:${lid}`,
2020
JSON.stringify({ timestamps: [], penaltyCount: 0, penaltyUntil: 0 }),
2121
{
22-
EX: 3600, // 1 hour
22+
expiration: {
23+
type: "EX",
24+
value: 3600, // 1 hour
25+
},
2326
},
2427
);
2528
} catch (error) {
@@ -49,7 +52,12 @@ export async function penalizeUser(
4952

5053
const ttl = Math.max(1, Math.floor((entry.penaltyUntil - now) / 1000));
5154
await Promise.all([
52-
redis.set(`rate:${lid}`, JSON.stringify(entry), { EX: ttl }),
55+
redis.set(`rate:${lid}`, JSON.stringify(entry), {
56+
expiration: {
57+
type: "EX",
58+
value: ttl,
59+
},
60+
},),
5361
prisma.user.update({
5462
where: { lid },
5563
data: { points: { decrement: 10 } },
@@ -94,7 +102,12 @@ export async function rateLimiter(
94102

95103
entry.timestamps.push(now);
96104
const ttl = Math.max(1, Math.floor((entry.penaltyUntil - now) / 1000));
97-
await redis.set(key, JSON.stringify(entry), { EX: ttl });
105+
await redis.set(key, JSON.stringify(entry), {
106+
expiration: {
107+
type: "EX",
108+
value: ttl,
109+
},
110+
},);
98111

99112
return {
100113
value: entry,

src/components/utils/riddle.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import { riddles, done, wrong } from "../utils/data";
44
import log from "../utils/log";
55
import redis from "../redis";
66

7-
export default async function (
8-
msg: Message,
9-
quoted: Message,
10-
): Promise<boolean> {
7+
export default async function (msg: Message): Promise<void> {
118
try {
12-
if (!quoted.body) return false;
9+
if (!msg.hasQuotedMsg) return;
10+
11+
const quoted: Message = await msg.getQuotedMessage();
12+
if (!quoted.body) return;
1313

1414
const key = `riddle:${quoted.id.id}`;
1515
const result = await redis.get(key);
16-
if (!result) return false;
16+
if (!result) return;
1717

1818
const riddleAttempt = JSON.parse(result);
1919
const riddle = riddles[parseInt(riddleAttempt.riddle_id)];
@@ -50,8 +50,5 @@ export default async function (
5050
log.info("RiddleAnsweredWrong", "Wrong", quoted.body),
5151
]);
5252
}
53-
return true;
5453
} catch (error: any) {}
55-
56-
return false;
5754
}

src/jobs/speedtest.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const info = {
1515
export default async function () {
1616
try {
1717
const results = await speedTest({ acceptLicense: true, acceptGdpr: true });
18-
console.log("")// to create new line
18+
console.log(""); // to create new line
1919
log.info(
2020
"SpeedTestJob",
2121
`Download: ${results.download.bandwidth / 125000} Mbps`,
@@ -25,7 +25,12 @@ export default async function () {
2525
`Upload: ${results.upload.bandwidth / 125000} Mbps`,
2626
);
2727
log.info("SpeedTestJob", `Ping: ${results.ping.latency} ms`);
28-
await redis.set(CACHE_KEY, JSON.stringify(results), { EX: CACHE_TTL });
28+
await redis.set(CACHE_KEY, JSON.stringify(results), {
29+
expiration: {
30+
type: "EX",
31+
value: CACHE_TTL,
32+
},
33+
});
2934
} catch (err) {
3035
log.error("SpeedTestJob", err);
3136
}

0 commit comments

Comments
 (0)