Complete rewrite of the MOTD and Save System loops
This commit is contained in:
parent
8da2c94dcd
commit
7be7970194
@ -1,7 +1,7 @@
|
||||
import hxdiscord.DiscordClient;
|
||||
import hxdiscord.types.*;
|
||||
import hxdiscord.endpoints.Endpoints;
|
||||
import haxe.MainLoop;
|
||||
import haxe.Timer;
|
||||
import haxe.Json;
|
||||
import sys.io.File;
|
||||
import sys.FileSystem;
|
||||
@ -20,7 +20,8 @@ class Onequestionmark {
|
||||
static var yesnoDB: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> = [];
|
||||
|
||||
|
||||
@ -91,8 +92,8 @@ class Onequestionmark {
|
||||
|
||||
botInfo = Endpoints.getCurrentUser();
|
||||
|
||||
MainLoop.add(motd);
|
||||
MainLoop.add(saveSystem);
|
||||
startMotd();
|
||||
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() {
|
||||
if (settings.motd.date != datestamp()) {
|
||||
public static function startMotd() {
|
||||
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.posted = false;
|
||||
}
|
||||
|
||||
if ((settings.motd.posted == false) && (Date.now().getHours() >= settings.motd.time)) {
|
||||
settings.motd.posted = true;
|
||||
saveQueue.push("settings");
|
||||
postMotd();
|
||||
|
||||
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().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));
|
||||
// Start the looped MOTD timer
|
||||
motdTimer = new Timer(86400000);
|
||||
motdTimer.run = function() {
|
||||
settings.motd.date = datestamp();
|
||||
saveQueue.push("settings");
|
||||
postMotd();
|
||||
}
|
||||
}, 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.
|
||||
*/
|
||||
public static function saveSystem() {
|
||||
if (saveTimer != Date.now().getMinutes()) { // We only want to run this once a minute
|
||||
|
||||
if (saveQueue.length > 0) { // See if there's anything in the queue
|
||||
Timer.delay(function() {
|
||||
saveTimer = new Timer(60*1000);
|
||||
saveTimer.run = function() {
|
||||
if (saveQueue.length > 0) { // See if there's anything in the queue
|
||||
switch (saveQueue.shift()) {
|
||||
case "settings":
|
||||
saveSettings();
|
||||
@ -401,11 +436,10 @@ class Onequestionmark {
|
||||
case "echobox":
|
||||
saveEchoboxDB();
|
||||
Sys.println('[${timestamp()}] Saved echobox-db.json');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
saveTimer = Date.now().getMinutes(); // Update timer
|
||||
}
|
||||
}, (60-Date.now().getSeconds())*1000);
|
||||
}
|
||||
|
||||
|
||||
|
@ -14,8 +14,7 @@ A `settings.json` file is required to run the bot. An example is provided below:
|
||||
"motd": {
|
||||
"date": "",
|
||||
"time": 8,
|
||||
"channels": [],
|
||||
"posted": true
|
||||
"channels": []
|
||||
},
|
||||
"debug": 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.
|
||||
- `date` and `posted` are modified by the bot during normal operations.
|
||||
- `time` determines the hour at which the MOTD is posted (8AM by default).
|
||||
- `date` is modified by the bot during normal operations.
|
||||
- `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.
|
||||
- `debug` determines whether HxDiscord's console debug messages are enabled.
|
||||
- `devmode` restricts the bot's responses to the specified dev server.
|
||||
|
18
motd-db.json
18
motd-db.json
@ -1,30 +1,30 @@
|
||||
{
|
||||
"sun": [
|
||||
"Sun": [
|
||||
"Sunday Event",
|
||||
"For any day, the script will randomly choose between multiple entries, if provided."
|
||||
],
|
||||
"mon": [
|
||||
"Mon": [
|
||||
"Monday Event"
|
||||
],
|
||||
"tue": [
|
||||
"Tue": [
|
||||
"Tuesday Event"
|
||||
],
|
||||
"wed": [
|
||||
"Wed": [
|
||||
"Wednesday Event"
|
||||
],
|
||||
"thu": [
|
||||
"Thu": [
|
||||
"Thursday Event"
|
||||
],
|
||||
"fri": [
|
||||
"Fri": [
|
||||
"Friday Event"
|
||||
],
|
||||
"sat": [
|
||||
"Sat": [
|
||||
"Saturday Event"
|
||||
],
|
||||
"mon01": [
|
||||
"Mon01": [
|
||||
"Example event to be triggered on any Monday that is also the 1st."
|
||||
],
|
||||
"jan01": [
|
||||
"Jan01": [
|
||||
"Example event to be triggered on January 1st."
|
||||
],
|
||||
"2000-01-01": [
|
||||
|
Loading…
x
Reference in New Issue
Block a user