-
Notifications
You must be signed in to change notification settings - Fork 2
/
subscription.ts
120 lines (107 loc) · 3.18 KB
/
subscription.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { SolaContext } from "./constants.ts";
import {
mapToString,
sendMessageWithThread,
setToString,
stringToMap,
stringToSet,
} from "./utilities.ts";
export async function handleSubscription(
ctx: SolaContext,
chatId: number,
groupId: number | undefined,
hourInput: string | undefined,
daysInput: string | undefined,
isSubscribe: boolean,
message_thread_id?: number,
is_topic_message = false,
) {
if (groupId) {
let hour: undefined | number = undefined;
if (hourInput) {
hour = parseInt(hourInput);
// 定义允许的最小值和最大值
const minHour = 0;
const maxHour = 23;
// 检查hour是否在范围内
if (hour < minHour || hour > maxHour) {
// hour不在范围内,报错或执行其他操作
await sendMessageWithThread(
chatId,
"❌ Invalid hour. Please enter a valid hour between 0 and 23.",
message_thread_id,
is_topic_message,
);
return;
}
}
let days: undefined | number = undefined;
if (daysInput) {
days = parseInt(daysInput);
// 定义允许的最小值
const minDays = 1;
// 检查days是否在范围内
if (days < minDays) {
// days不在范围内,报错或执行其他操作
await sendMessageWithThread(
chatId,
"❌ Invalid days. Please enter a valid days bigger than 1.",
message_thread_id,
is_topic_message,
);
return;
}
}
if (hour) {
// hour在范围内,继续处理
ctx.session.temporary.hour = hour;
}
if (days) {
ctx.session.temporary.days = days;
}
ctx.session.temporary.groupId = groupId;
ctx.session.temporary.offset = 0;
const keys = stringToMap(ctx.session.global.keys);
const keysSet = keys.get(chatId);
if (isSubscribe) {
if (!keysSet) {
keys.set(chatId, new Set([message_thread_id ? message_thread_id : 0]));
} else {
keysSet.add(message_thread_id ? message_thread_id : 0);
}
ctx.session.global.keys = mapToString(keys);
// const subscriptionSet = stringToSet(
// ctx.session.temporary.subscription,
// );
// subscriptionSet.add(groupId);
const subscriptionSet = new Set([groupId]);
ctx.session.temporary.subscription = setToString(subscriptionSet);
} else {
if (
keysSet && keysSet.delete(message_thread_id ? message_thread_id : 0)
) {
keys.set(chatId, keysSet);
ctx.session.global.keys = mapToString(keys);
}
const subscriptionSet = stringToSet(
ctx.session.temporary.subscription,
);
subscriptionSet.delete(groupId);
ctx.session.temporary.subscription = setToString(subscriptionSet);
}
const action = isSubscribe ? "Subscribed" : "Unsubscribed";
await sendMessageWithThread(
chatId,
`✅ ${action} to daily event updates!`,
message_thread_id,
is_topic_message,
);
} else {
await sendMessageWithThread(
chatId,
"❌ Invalid group ID. Please try again with /subs <groupId> or /unsubs <groupId>.",
message_thread_id,
is_topic_message,
);
}
}