-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·148 lines (125 loc) · 6.56 KB
/
index.js
File metadata and controls
executable file
·148 lines (125 loc) · 6.56 KB
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
138
139
140
141
142
143
144
145
146
147
148
require("dotenv/config");
const { Client, GatewayIntentBits, EmbedBuilder, AttachmentBuilder } = require('discord.js');
const axios = require('axios');
const cron = require('node-cron');
const ImageCreator = require("./image.js")
const client = new Client({
intents: [GatewayIntentBits.Guilds],
});
const PRAYER_CHANNEL_ID = '1330707555562946590';
function parseTime(timeStr) {
const [hourStr, minuteStr] = timeStr.split(':');
return { hour: parseInt(hourStr, 10), minute: parseInt(minuteStr, 10) };
};
const hadiths = {
Fajr: [
["«من صلى الفجر في جماعة ثم قعد يذكر الله حتى تطلع الشمس ثم صلى ركعتين كانت له كأجر حجة وعمرة تامة تامة تامة» (الترمذي)", "«ركعتا الفجر خير من الدنيا وما فيها» (مسلم)"],
["«أثقل الصلاة على المنافقين صلاة العشاء وصلاة الفجر، ولو يعلمون ما فيهما لأتوهما ولو حبوا» (البخاري)", "«بين الرجل وبين الشرك والكفر ترك الصلاة» (مسلم)"],
"الفجر"
],
Dhuhr: [
["«من حافظ عليها (صلاة الظهر) وقتها بني له بيت في الجنة» (مسلم)", "«أول ما يحاسب عليه العبد يوم القيامة من عمله الصلاة، فإن صلحت فقد أفلح ونجح، وإن فسدت فقد خاب وخسر» (الترمذي)"],
["«من ترك الصلاة فقد كفر» (الترمذي)", "«حافظوا على الصلوات والصلاة الوسطى وقوموا لله قانتين» (البقرة: 238)"],
"الظهر"
],
Asr: [
["«من صلى البردين دخل الجنة» (البخاري)", "«لن يلج النار من صلى قبل طلوع الشمس وقبل غروبها» (مسلم)"],
["«من ترك صلاة العصر فقد حبط عمله» (البخاري)", "«إن الصلاة كانت على المؤمنين كتابًا موقوتًا» (النساء: 103)"],
"العصر"
],
Maghrib: [
["«كان النبي صلى الله عليه وسلم يستغفر ثلاثاً بعد المغرب ويقول: اللهم أنت السلام ومنك السلام تباركت يا ذا الجلال والإكرام» (مسلم)", "«من صلى المغرب والعشاء في جماعة فكأنما قام نصف الليل» (الترمذي)"],
["«فويل للمصلين الذين هم عن صلاتهم ساهون» (الماعون: 4-5)", "«وأقم الصلاة طرفي النهار وزلفًا من الليل إن الحسنات يذهبن السيئات» (هود: 114)"],
"المغرب"
],
Isha: [
["«من صلى العشاء في جماعة فكأنما قام نصف الليل» (مسلم)", "«بشر المشائين في الظلم إلى المساجد بالنور التام يوم القيامة» (أبو داود)"],
["«أثقل الصلاة على المنافقين صلاة العشاء وصلاة الفجر» (البخاري)", "«لا حظ في الإسلام لمن ترك الصلاة» (ابن ماجه)"],
"العشاء"
],
Isha: [
["«من صلى العشاء في جماعة فكأنما قام نصف الليل» (مسلم)", "«بشر المشائين في الظلم إلى المساجد بالنور التام يوم القيامة» (أبو داود)"],
["«أثقل الصلاة على المنافقين صلاة العشاء وصلاة الفجر» (البخاري)", "«لا حظ في الإسلام لمن ترك الصلاة» (ابن ماجه)"],
"العشاء"
],
};
function getRandomHadiths(prayer) {
const hadithArray = hadiths[prayer];
if (!hadithArray) return { positive: null, warning: null };
const positiveHadiths = hadithArray[0];
const warningHadiths = hadithArray[1];
const name = hadithArray[2];
const positive = positiveHadiths[Math.floor(Math.random() * positiveHadiths.length)];
const warning = warningHadiths[Math.floor(Math.random() * warningHadiths.length)];
return { positive, warning, name };
};
async function fetchPrayerTimings() {
try {
const response = await axios.get('https://api.aladhan.com/v1/timingsByCity', {
params: {
city: 'Cairo',
country: 'Egypt',
method: 5
},
});
return response.data.data.timings;
} catch (error) {
console.error('Error fetching prayer timings:', error);
throw new Error('Could not fetch prayer timings.');
};
};
async function sendPrayerReminder(prayer, timings) {
const channel = client.channels.cache.get(PRAYER_CHANNEL_ID);
if (!channel) return console.error('Channel not found!');
const { positive, warning, name } = getRandomHadiths(prayer);
const resultEmbed = new EmbedBuilder()
.setColor('#3B5B85')
.setTitle('Prayer Reminder')
.setImage('attachment://reminder.png')
.setTimestamp()
.setFooter({
text: channel.guild.name,
iconURL: channel.guild.iconURL({dynamic: true}),
});
const Image = await ImageCreator({
prayerName: name,
prayerTime: timings[prayer],
positiveHadith: positive,
warningHadith: warning,
})
await channel.send({
embeds: [resultEmbed],
files: [
{
attachment: Image,
name: "reminder.png",
},
],
});
};
async function schedulePrayerReminders() {
try {
const timings = await fetchPrayerTimings();
const prayerNames = Object.keys(hadiths);
sendPrayerReminder('Isha', timings);
// for (const prayer of prayerNames) {
// const timeStr = timings[prayer];
// const { hour, minute } = parseTime(timeStr);
// cron.schedule(`${minute} ${hour} * * *`, () => {
// sendPrayerReminder(prayer, timings);
// }, { timezone: "Africa/Cairo" });
// console.log(`Scheduled reminder for ${prayer} at ${hour}:${minute}`);
// };
} catch (error) {
console.error('Error scheduling prayer reminders:', error);
};
};
client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
await schedulePrayerReminders();
cron.schedule('0 0 * * *', async () => {
console.log('Refreshing prayer times...');
await schedulePrayerReminders();
});
});
client.login(process.env.DISCORDTOKEN);