Complete rewrite of the MOTD and Save System loops

This commit is contained in:
2024-10-18 23:49:35 -05:00
parent 8da2c94dcd
commit 7be7970194
3 changed files with 85 additions and 52 deletions

View File

@@ -1,7 +1,7 @@
import hxdiscord.DiscordClient; import hxdiscord.DiscordClient;
import hxdiscord.types.*; import hxdiscord.types.*;
import hxdiscord.endpoints.Endpoints; import hxdiscord.endpoints.Endpoints;
import haxe.MainLoop; import haxe.Timer;
import haxe.Json; import haxe.Json;
import sys.io.File; import sys.io.File;
import sys.FileSystem; import sys.FileSystem;
@@ -20,7 +20,8 @@ class Onequestionmark {
static var yesnoDB:Dynamic; static var yesnoDB:Dynamic;
static var echoboxDB:haxe.DynamicAccess<Dynamic> = {}; static var echoboxDB:haxe.DynamicAccess<Dynamic> = {};
static var saveTimer = Date.now().getMinutes(); static var motdTimer:Timer;
static var saveTimer:Timer;
static var saveQueue:Array<String> = []; static var saveQueue:Array<String> = [];
@@ -91,8 +92,8 @@ class Onequestionmark {
botInfo = Endpoints.getCurrentUser(); botInfo = Endpoints.getCurrentUser();
MainLoop.add(motd); startMotd();
MainLoop.add(saveSystem); saveSystem();
} }
@@ -344,44 +345,77 @@ class Onequestionmark {
/** /**
* This function controls the "Meme of the Day" system. * This function starts the "Meme of the Day" system. A temporary timer is set that counts down to the next MOTD event, at which point the main timer is started on a 24 hour loop.
*/ */
public static function motd() { public static function startMotd() {
if (settings.motd.date != datestamp()) { var now = Date.now();
var nextPost = new Date(now.getFullYear(), now.getMonth(), now.getDate(), settings.motd.time, 0, 0);
if (now.getHours() >= settings.motd.time) {
if (settings.motd.date != datestamp()) {
settings.motd.date = datestamp();
saveQueue.push("settings");
postMotd();
}
nextPost = DateTools.delta(nextPost, 86400000); // Add one day in milliseconds
}
// Calculate delay for timers
var delay = Std.int(nextPost.getTime() - now.getTime());
Timer.delay(function(){
settings.motd.date = datestamp(); settings.motd.date = datestamp();
settings.motd.posted = false;
}
if ((settings.motd.posted == false) && (Date.now().getHours() >= settings.motd.time)) {
settings.motd.posted = true;
saveQueue.push("settings"); saveQueue.push("settings");
postMotd();
var msg = ""; // Start the looped MOTD timer
var day:Array<String> = []; motdTimer = new Timer(86400000);
motdTimer.run = function() {
// Check for exact date settings.motd.date = datestamp();
if (motdDB.exists('${datestamp()}')) { saveQueue.push("settings");
day = motdDB.get('${datestamp()}'); postMotd();
}
// Else, check for special date
else if (motdDB.exists('${printMonth().toLowerCase().substr(0,3)}${printDate()}')) {
day = motdDB.get('${printMonth().toLowerCase().substr(0,3)}${printDate()}');
}
// Else, check for special day/date combo
else if (motdDB.exists('${printDay().toLowerCase().substr(0,3)}${printDate()}')) {
day = motdDB.get('${printDay().toLowerCase().substr(0,3)}${printDate()}');
}
// Otherwise, post daily meme
else if (motdDB.exists(printDay().toLowerCase().substr(0,3))) {
day = motdDB.get(printDay().toLowerCase().substr(0,3));
} }
}, delay);
}
if (day.length > 0) {msg = day[randInt(0, day.length-1)];}
var channels:Array<String> = settings.motd.channels; // Because it won't let me do this directly /**
* Sends the MOTD to the channels specified in the bot settings. May just move this code into the places it's needed instead of keeping this function.
*/
public static function postMotd() {
var msg = getMotd();
var channels:Array<String> = settings.motd.channels; // Because it won't let me do this directly
if (channels.length > 0) {for (i in channels) {if (msg.length > 0) {Endpoints.sendMessage(i, {content:msg}, null, false);}}} if (channels.length > 0) {for (i in channels) {if (msg.length > 0) {Endpoints.sendMessage(i, {content:msg}, null, false);}}}
}
/**
* This function checks the MOTD database to find the entry most relevant to the current date and returns the appropriate data.
*/
public static function getMotd() {
var msg = "";
var day:Array<String> = [];
// Check for exact date
if (motdDB.exists('${datestamp()}')) {
day = motdDB.get('${datestamp()}');
} }
// Else, check for special date
else if (motdDB.exists('${printMonth().substr(0,3)}${printDate()}')) {
day = motdDB.get('${printMonth().substr(0,3)}${printDate()}');
}
// Else, check for special day/date combo
else if (motdDB.exists('${printDay().substr(0,3)}${printDate()}')) {
day = motdDB.get('${printDay().substr(0,3)}${printDate()}');
}
// Otherwise, post daily meme
else if (motdDB.exists(printDay().substr(0,3))) {
day = motdDB.get(printDay().substr(0,3));
}
if (day.length > 0) {msg = day[randInt(0, day.length-1)];}
return msg;
} }
@@ -391,9 +425,10 @@ class Onequestionmark {
* This throttles filesystem access, to prevent multiple commands from writing to the drive at the same time. * This throttles filesystem access, to prevent multiple commands from writing to the drive at the same time.
*/ */
public static function saveSystem() { public static function saveSystem() {
if (saveTimer != Date.now().getMinutes()) { // We only want to run this once a minute Timer.delay(function() {
saveTimer = new Timer(60*1000);
if (saveQueue.length > 0) { // See if there's anything in the queue saveTimer.run = function() {
if (saveQueue.length > 0) { // See if there's anything in the queue
switch (saveQueue.shift()) { switch (saveQueue.shift()) {
case "settings": case "settings":
saveSettings(); saveSettings();
@@ -401,11 +436,10 @@ class Onequestionmark {
case "echobox": case "echobox":
saveEchoboxDB(); saveEchoboxDB();
Sys.println('[${timestamp()}] Saved echobox-db.json'); Sys.println('[${timestamp()}] Saved echobox-db.json');
}
} }
} }
}, (60-Date.now().getSeconds())*1000);
saveTimer = Date.now().getMinutes(); // Update timer
}
} }

View File

@@ -14,8 +14,7 @@ A `settings.json` file is required to run the bot. An example is provided below:
"motd": { "motd": {
"date": "", "date": "",
"time": 8, "time": 8,
"channels": [], "channels": []
"posted": true
}, },
"debug": false, "debug": false,
"devmode": false, "devmode": false,
@@ -26,8 +25,8 @@ A `settings.json` file is required to run the bot. An example is provided below:
} }
``` ```
- onequestionmark will handle most of the `motd` values itself. - onequestionmark will handle most of the `motd` values itself.
- `date` and `posted` are modified by the bot during normal operations. - `date` is modified by the bot during normal operations.
- `time` determines the hour at which the MOTD is posted (8AM by default). - `time` determines the hour at which the MOTD is posted (24-hour notation, local time).
- Using the `?motd` command adds the current channel to the `channels` array. - Using the `?motd` command adds the current channel to the `channels` array.
- `debug` determines whether HxDiscord's console debug messages are enabled. - `debug` determines whether HxDiscord's console debug messages are enabled.
- `devmode` restricts the bot's responses to the specified dev server. - `devmode` restricts the bot's responses to the specified dev server.

View File

@@ -1,30 +1,30 @@
{ {
"sun": [ "Sun": [
"Sunday Event", "Sunday Event",
"For any day, the script will randomly choose between multiple entries, if provided." "For any day, the script will randomly choose between multiple entries, if provided."
], ],
"mon": [ "Mon": [
"Monday Event" "Monday Event"
], ],
"tue": [ "Tue": [
"Tuesday Event" "Tuesday Event"
], ],
"wed": [ "Wed": [
"Wednesday Event" "Wednesday Event"
], ],
"thu": [ "Thu": [
"Thursday Event" "Thursday Event"
], ],
"fri": [ "Fri": [
"Friday Event" "Friday Event"
], ],
"sat": [ "Sat": [
"Saturday Event" "Saturday Event"
], ],
"mon01": [ "Mon01": [
"Example event to be triggered on any Monday that is also the 1st." "Example event to be triggered on any Monday that is also the 1st."
], ],
"jan01": [ "Jan01": [
"Example event to be triggered on January 1st." "Example event to be triggered on January 1st."
], ],
"2000-01-01": [ "2000-01-01": [