forked from LendaDeAfonselio/Alarm_Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_alarms.js
218 lines (205 loc) · 9.2 KB
/
load_alarms.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const Alarm_model = require('./models/alarm_model');
const Private_alarm_model = require('./models/private_alarm_model');
const One_Time_Alarm_model = require('./models/one_time_alarm_model');
const alarm_db = require('./data_access/alarm_index');
const logging = require('./Utils/logging');
const utility_functions = require('./Utils/utility_functions');
async function fetchAlarmsforGuild(cron_list, cron, guild, guild_id, client) {
let shard_guilds = Array.from(client.guilds.cache.keys());
if (shard_guilds.includes(guild_id)) {
let alarms = await Alarm_model.find({ guild: guild_id });
for (alarm of alarms) {
let message_stg = alarm.message;
let crono = alarm.alarm_args;
let alarm_id = alarm.alarm_id;
let channel_id = alarm.channel;
let scheduledMessage = new cron(crono, async () => {
try {
let channel = await guild.channels.cache.get(channel_id);
if (!utility_functions.can_send_messages_to_ch_using_guild(guild, channel)) {
utility_functions.send_message_to_default_channel(guild,`Cannot setup the alarm in channel ${channel_id} because the bot does not have permission to send messages to it.`);
return false;
}
if (channel !== undefined) {
channel.send(message_stg);
} else {
logging.logger.info(`${alarm_id} from the DB is not usable because the channel ${channel_id} was not found`);
return false;
}
}
catch (err) {
logging.logger.error(`Alarm with id ${alarm_id} failed to go off. Error: ${err}`);
}
}, {
scheduled: true
});
scheduledMessage.start();
if (!alarm.isActive) {
// it is not active
scheduledMessage.stop();
}
cron_list[alarm_id] = scheduledMessage;
}
return alarms;
}
return [];
}
async function fetchPrivateAlarms(cron_list, cron, client, shardid) {
let alarms = await Private_alarm_model.find();
for (alarm of alarms) {
let message_stg = alarm.message;
let crono = alarm.alarm_args;
let alarm_id = alarm.alarm_id;
let user_id = alarm.user_id;
let user_from_shard = await client.users.fetch(user_id);
if (user_from_shard !== undefined) {
let scheduledMessage = new cron(crono, async () => {
try {
let member = await client.users.fetch(user_id);
if (member !== undefined) {
member.send(message_stg).catch(() => {
logging.logger.info(`Shard number ${shardid} does not have permission to send message to ${user_id}`);
cron_list[alarm_id].stop();
delete cron_list[alarm_id];
});
} else {
logging.logger.info(`${alarm_id} from the DB is not usable because the user ${user_id} was not found in shard ${shardid}`);
}
} catch (err) {
logging.logger.error(`Alarm with id ${alarm_id} failed to go off. Error: ${err}. ${err.stack}`);
}
}, {
scheduled: true
});
scheduledMessage.start();
if (!alarm.isActive) {
// it is not active
scheduledMessage.stop();
}
cron_list[alarm_id] = scheduledMessage;
} else {
logging.logger.info(`${alarm_id} from the DB is not usable because the user ${user_id} was not found in shard ${shardid}`);
}
}
}
async function fetchOTAsforGuild(cron_list, cron, guild, guild_id, client) {
let shard_guilds = Array.from(client.guilds.cache.keys());
if (shard_guilds.includes(guild_id)) {
let current = new Date();
let alarms = await One_Time_Alarm_model.find({ guild: guild_id, isPrivate: false });
for (alarm of alarms) {
let alarm_id = alarm.alarm_id;
let crono = alarm.alarm_date;
if (current > crono) {
logging.logger.error(`${alarm_id} is not usable since the date has already expired`);
continue;
}
let message_stg = alarm.message;
let channel_id = alarm.channel;
let channel = await guild.channels.cache.get(channel_id);
if (!utility_functions.can_send_messages_to_ch_using_guild(guild, channel)) {
utility_functions.send_message_to_default_channel(guild,`Cannot setup the alarm in channel ${channel_id} because the bot does not have permission to send messages to it.`);
return false;
}
let scheduledMessage = new cron(crono, async () => {
try {
if (channel !== undefined) {
channel.send(message_stg);
scheduledMessage.stop();
delete cron_list[alarm_id];
} else {
logging.logger.info(`${alarm_id} from the DB is not usable because the channel ${channel_id} was not found`);
return false;
}
}
catch (err) {
logging.logger.error(`Alarm with id ${alarm_id} failed to off. Error: ${err}. ${err.stack}`);
}
});
scheduledMessage.start();
cron_list[alarm_id] = scheduledMessage;
}
return alarms;
}
return [];
}
async function fetchPrivateOTAs(cron_list, cron, client, shardid) {
let current = new Date();
let alarms = await One_Time_Alarm_model.find({ isPrivate: true });
for (alarm of alarms) {
let alarm_id = alarm.alarm_id;
let crono = alarm.alarm_date;
if (current > crono) {
logging.logger.error(`${alarm_id} is not usable since the date has already expired`);
continue;
}
let message_stg = alarm.message;
let user_id = alarm.user_id;
let user_from_shard = await client.users.fetch(user_id);
if (user_from_shard !== undefined) {
let scheduledMessage = new cron(crono, async () => {
try {
user_from_shard.send(message_stg)
.catch(logging.logger.info(`Shard with number ${shardid} does not have permission to send message to ${user_id}`));
scheduledMessage.stop();
delete cron_list[alarm_id];
} catch (err) {
logging.logger.error(`Alarm with id ${alarm_id} failed to off. Error: ${err}. ${err.stack}`);
}
});
scheduledMessage.start();
cron_list[alarm_id] = scheduledMessage;
} else {
logging.logger.info(`${alarm_id} from the DB is not usable because the user ${user_id} was not found in shard ${shardid}`);
}
}
}
async function fetchTTSAlarms(cron_list, cron, guild, guild_id, client) {
let shard_guilds = Array.from(client.guilds.cache.keys());
if (shard_guilds.includes(guild_id)) {
let alarms = await alarm_db.get_all_ttsAlarms_for_guild(guild_id);
for (alarm of alarms) {
let message_stg = alarm.message;
let crono = alarm.alarm_args;
let alarm_id = alarm.alarm_id;
let channel_id = alarm.channel;
let scheduledMessage = new cron(crono, async () => {
try {
let channel = await guild.channels.cache.get(channel_id);
if (!utility_functions.can_send_messages_to_ch_using_guild(guild, channel)) {
utility_functions.send_message_to_default_channel(guild,`Cannot setup the alarm in channel ${channel_id} because the bot does not have permission to send messages to it.`);
return false;
}
if (channel !== undefined) {
channel.send(message_stg, {
tts: true
});
} else {
logging.logger.info(`${alarm_id} from the DB is not usable because the channel ${channel_id} was not found`);
return false;
}
}
catch (err) {
logging.logger.error(`Alarm with id ${alarm_id} failed to go off. Error: ${err}`);
}
}, {
scheduled: true
});
scheduledMessage.start();
if (!alarm.isActive) {
// it is not active
scheduledMessage.stop();
}
cron_list[alarm_id] = scheduledMessage;
}
return alarms;
}
return [];
}
module.exports = {
fetchAlarmsforGuild: fetchAlarmsforGuild,
fetchPrivateAlarms: fetchPrivateAlarms,
fetchPrivateOTAs: fetchPrivateOTAs,
fetchOTAsforGuild: fetchOTAsforGuild,
fetchTTSAlarms: fetchTTSAlarms
}