-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
137 lines (103 loc) · 4.38 KB
/
index.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require("dotenv").config();
const { Telegraf } = require("telegraf");
const cron = require("node-cron");
const Keep_alive= require("./Keep_alive.js")
const { insertSubscription } = require("./helpers/insertSubscription");
const { addDaysToNow } = require("./helpers/addDaysToNow");
const { getAllSubscriptions } = require("./helpers/getAllSubscriptions");
const { removeExpiredMembers } = require("./helpers/removeExpiredMembers");
const { quitMessageMember } = require("./helpers/quitMember");
const bot = new Telegraf(process.env.BOT_TOKEN);
const GreetingMessage =
"حيّا هلًا بك🌻\nفي بيئة الإنجاز البحثي التحفيزية والتفاعلية، نسير معاً في ركب واحد، عزيمة الفرد منا وقود المجتمع، وإذا تراخت همة أحدنا تداعت له سائر الأعضاء، فخذت بيده وشدّدت من عزيمته.";
bot.start(async (ctx) => {
const first_name = ctx.from.first_name || "غير معرف";
const last_name = ctx.from.last_name || "غير معرف";
const user_name = ctx.from.username || "غير معرف";
const user_id = ctx.from.id;
let isActive = await getAllSubscriptions.getIsActive(user_id);
// Check if the user is in a group or supergroup
if (ctx.chat.type === "group" || ctx.chat.type === "supergroup") {
ctx.reply("يمكنك استخدام البوت في الدردشة الخاصة");
ctx.reply("اضغط هنا---> t.me/enjaz_nadeem_bot");
return;
}
if (isActive === true) {
const count = await getAllSubscriptions.getUserCount(user_id);
ctx.reply(`رقمك الشخصي هو: <b>${count}</b>`, { parse_mode: "HTML" });
return;
}
const lastCount = await getAllSubscriptions.getLastCount();
const isUserExist = await getAllSubscriptions.isUserExist(user_id);
const subscription = await getAllSubscriptions.IsSubscriptionEnd(user_id);
if (isActive === false && isUserExist === true && subscription === true) {
ctx.reply("لقد انتهى اشتراكك السابق، يمكنك تجديد اشتراكك الحالي");
ctx.reply("اضغط هنا---> /renew");
return;
}
if (isActive === false) {
// Check if the user is already subscribed
await insertSubscription.insertSubNumber(user_id, lastCount + 1);
console.log("The last count is:", lastCount + 1);
ctx.reply(`هذا هو رقمك الشخصي: ${lastCount + 1} \n\nرابط الهدية من الإنجاز البحثي:\nhttps://drive.google.com/file/d/1K-pZbE9YHr_GAJsoi-3XGclzpzJnR-5J/view?usp=share_link`);
ctx.reply(GreetingMessage);
// Increment the count for the next user
isActive = true;
// Insert the subscription details in the background
setTimeout(async () => {
try {
const subscription_end = addDaysToNow();
await insertSubscription.insertSubscription(
user_id,
user_name,
subscription_end,
isActive,
first_name,
last_name
);
} catch (error) {
console.error("Failed to insert subscription:", error);
}
}, 0); // Insert the details immediately after sending the response
}
});
// __________________________________________________RemoveMember_______________________________________
cron.schedule("0 0 * * * *", () => {
console.log("Running removeExpiredMembers at midnight...");
quitMessageMember().then((error) => {
if (error) {
console.error("Failed to process Send a message to members:", error);
return;
}
console.log("Completed processing Send a message to members.");
})
.catch((err) => {
console.error(
"An error occurred during the execution of removeExpiredMembers:",
err
);
});
removeExpiredMembers()
.then((error) => {
if (error) {
console.error("Failed to process expired members:", error);
return;
}
insertSubscription.updateIsActive(user_id, false);
console.log("Completed processing expired members.");
})
.catch((err) => {
console.error(
"An error occurred during the execution of removeExpiredMembers:",
err
);
});
});
bot.command("renew", async (ctx) => {
ctx.reply("contact us : 00966556673350");
});
bot
.launch()
.then(() => console.log("Bot is running..."))
.catch((err) => console.error("Failed to start bot:", err));
console.log("Bot is running...");