-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathscheduledactiontestinhell.js
More file actions
162 lines (151 loc) · 8 KB
/
scheduledactiontestinhell.js
File metadata and controls
162 lines (151 loc) · 8 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//------------------------------------------
//------------------------------------------
// !!! Maybe make a separate commands() function that just takes the command content and returns whatever it's output woulda been.
// !!! Then send that with <client>.channels.cache.get('1234567890').send('Hello world.');
// !!! idk dude, I'm fukin cracked rn and this is kinda ass. we need a fukin message object, but it be mega gay and can't save.
// !!! the other option we have would be to call constructors up tha ass and create new objects with the client to generate a message object
// !!! that we can pass over to the normal commands processor. This would mean a chain of constructors for message(), guild(), author() and so on
// !!! So in summary, the 4am me says to you: go fuck yourself and good luck ;)
// Scheduled commands handler
async function scheduledCommandsEngine(message, action, frequency, channel) {
if (action) {
// Need to check for admin perms here
// Also need to pull existing jobs for the guild to count them and make a new job ID
// Useful later: <client>.channels.cache.get('1234567890').send('Hello world.');
console.log("bru")
action = action.toLowerCase();
switch (action) {
case "create":
let helpText = "Enter your interval in HH:MM format. For example," + " every 30 minutes = 00:30 and every day = 24:00. The shortest allowed interval is 5 minutes (00:05)";
let jobID = uniqid();
let timeframeMS, channelID;
let lastFired = 0;
let command = message.content.match(/"([^']+)"/)[1];
// Validate and process input
if (command && frequency && channel) {
// Validate the timeframe
if (validateTime(frequency)) {
let a = frequency.split(":");
timeframeMS = ((+a[0]) * 60 * 60 + (+a[1]) * 60) * 1000;
// Validate the command
if (command) {
if (message.mentions.channels.first()) {
channelID = message.mentions.channels.first().id;
}
else {
channelID = channel;
}
console.log(channelID);
// Validate the channel
if (channelID) {
// Yay, all checks have passed! Let's get this confirmed with the user now
let filter = m => m.author.id === message.author.id;
message.channel.send(`Just to confirm, you want to run the command \`${command}\` in <#${channelID}> every ${(a[0] > 0) ? +a[0] + " hours " : ""}` +
`${(a[1] > 0) ? +a[1] + " minutes" : ""}. Does this look correct?`).then(() => {
message.channel.awaitMessages(filter, {
max: 1,
time: 60000,
errors: ['time']
})
.then(message => {
message = message.first();
if (message.content.toUpperCase() == 'YES' || message.content.toUpperCase() == 'Y') {
message.channel.send('Got it, your scheduled command task has been created! You can view all of your scheduled commands with `.tb schedule show`.');
// Send to the db
connp.none('INSERT INTO tsukibot.scheduled_actions(job_id, creation_ts, command, schedule, last_fired_ts, msg_object, channel_id) ' +
'VALUES(${jobID}, ${creationTS}, ${command}, ${scheduleMS}, ${lastFire}, ${msgObject}, ${channelID})', {
jobID: jobID,
creationTS: Date.now(),
command: command,
scheduleMS: timeframeMS,
lastFire: lastFired,
msgObject: JSON.stringify(message),
channelID: channelID
}).catch(err => {
console.log(err + "----schedule action creation db insertion error");
message.channel.send("OOPS nevermind. I ran into an issue when saving the scheduled command. Please try again later or notify us in the support server if this persists!");
});
} else if (message.content.toUpperCase() == 'NO' || message.content.toUpperCase() == 'N') {
message.channel.send("Alright, I won't save it. You can start over by running this command again.");
} else {
message.channel.send("Aborted: Invalid response. Expecting yes or no input.");
}
})
.catch(collected => {
console.log(collected);
message.channel.send("I didn't hear any response from you so I\'ll cancel this action. You can try again by re-running this command.");
});
});
}
else {
message.reply("Invalid channel provided. Tag the channel like #Channel or provide the ID number you get by right clicking on it.");
}
}
else {
message.reply("Invalid command entered. Remember to enter the command just as you would normally in chat, but surround it with quotes like \"this\". Example: \".tb hmap\"");
}
}
else {
message.reply("Invalid timeframe entered. " + helpText);
return;
}
}
else {
message.reply("Missing some arguments! Here\'s how to use this command:\n" + sendHelp());
}
break;
case "delete":
// First we gather all tasks, then find the ones that are due to run
let res = await connp.any('SELECT * FROM tsukibot.scheduled_actions');
console.log(res);
let queuedToRun = [];
res.forEach((item, index, array) => {
if (Date.now() - item.last_fired_ts >= item.schedule) {
// Let's grab the message object from the db
let msg = JSON.parse(item.msg_object);
// Update the channel propery with correct channel
msg.channel = item.channel_id;
// Forward the message to commands processor for normal handling
commands(msg);
// Now lets update the "last fired" timestamp of this job in the db so we can track this run
connp.query('INSERT INTO tsukibot.scheduled_actions(last_fired_ts) VALUES($1) WHERE job_id = $2)', Date.now(), item.job_id);
}
});
break;
case "show":
console.log("wttfff")
let gay = await connp.any('SELECT * FROM tsukibot.scheduled_actions');
console.log(gay);
client.channels.cache.get('567970908451635202').send('Hello here!');
break;
}
}
// Do the normal tasks if no user action is provided (function called without user input)
else {
// First we gather all tasks, then find the ones that are due to run
let res = await connp.any('SELECT * FROM tsukibot.scheduled_actions');
console.log(res);
let queuedToRun = [];
res.forEach((item, index, array) => {
if (Date.now() - item.last_fired_ts >= item.schedule) {
//queuedToRun.push(item);
// Let's build a message object based on the job so we can send it to the commands processor
let msg = new Discord.Message(client, {
id: 848465205919088650,
type: 'DEFAULT',
content: item.command,
author: item.author_id,
pinned: null,
tts: null,
embeds: null,
attachments: null,
nonce: "123" // idfk
}, client.channels.cache.get(item.channel_id));
// Send the generated message to the commands processor
commands(msg);
// Now lets update the "last fired" timestamp of this job in the db so we can track this run
connp.query('INSERT INTO tsukibot.scheduled_actions(last_fired_ts) VALUES($1) WHERE job_id = $2)', Date.now(), item.job_id);
}
});
}
}