From 27dd8cadc9668a5973f979aa1492a04acb7c55d6 Mon Sep 17 00:00:00 2001 From: Izwzyzx <184772711+Izwzyzx@users.noreply.github.com> Date: Sun, 3 Sep 2023 00:00:11 -0500 Subject: [PATCH] Added Echobox --- .gitignore | 3 ++- Onequestionmark.hx | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5cefe34..1b33c05 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ export/ modules/ # ignore bot settings file -settings.json \ No newline at end of file +settings.json +echobox-db.json \ No newline at end of file diff --git a/Onequestionmark.hx b/Onequestionmark.hx index a077a9b..d88d7de 100644 --- a/Onequestionmark.hx +++ b/Onequestionmark.hx @@ -13,9 +13,11 @@ class Onequestionmark { static var settings:Dynamic; static var botInfo:Dynamic; static var iceRegex:EReg = ~/\bicc?ed?\b/i; + static var hugDB:Array; static var motdDB:haxe.DynamicAccess = {}; static var yesnoDB:Dynamic; + static var echoboxDB:haxe.DynamicAccess = {}; static var saveTimer = Date.now().getMinutes(); static var saveQueue:Array = []; @@ -58,6 +60,14 @@ class Onequestionmark { yesnoDB = {"yes": [],"no": []}; // Fallback } + if (FileSystem.exists("echobox-db.json")) { + Sys.println('[${timestamp()}] Found echobox-db.json, loading'); + yesnoDB = Json.parse(File.getContent("echobox-db.json")); + } else { + Sys.println('[${timestamp()}] Did not find echobox-db.json, generating'); + File.saveContent("echobox-db.json", Json.stringify(echoboxDB, "\t")); + } + // Start the bot Bot = new DiscordClient(settings.token, [3276799], settings.debug); Bot.onReady = onReady; @@ -161,6 +171,43 @@ class Onequestionmark { case "no": var no = yesnoDB.no; m.reply({content:'${no[randInt(0, no.length-1)]}'}, false); + // Echobox + case "echobox": + // Create Echobox array for the server if missing + if (!echoboxDB.exists('server${m.guild_id}')) { + echoboxDB.set('server${m.guild_id}', []); + } + + var echobox:Array = echoboxDB.get('server${m.guild_id}'); + var quote:String = ""; + + // Echobox response + if (!m.mention_everyone) { + if (echobox.length > 0) { + var ebchoice = randInt(0, echobox.length-1); + //m.reply({content:'**Echobox, quote #${ebchoice+1}:** ${echobox[ebchoice]}'}, false); + quote = '**Echobox, quote #${ebchoice+1}:** ${echobox[ebchoice]}'; + } else { + //m.reply({content:'**Echobox:** There are no quotes in this server\'s Echobox yet.'}, false); + quote = '**Echobox:** There are no quotes in this server\'s Echobox yet.'; + } + + // Add quote to Echobox + if (msg.length != 0) { + if (!echobox.contains(msg)) { + echobox.push(msg); + echoboxDB.set('server${m.guild_id}', echobox); + saveQueue.push("echobox"); + m.reply({content:quote}, false); + } else { + m.reply({content:'**Echobox:** This quote is already in the database.'}, false); + } + } else { + m.reply({content:quote}, false); + } + } else { + m.reply({content:'**Echobox:** You cannot add `@everyone` to the Echobox.'}, false); + } } } @@ -294,6 +341,9 @@ class Onequestionmark { case "settings": saveSettings(); Sys.println('[${timestamp()}] Saved settings.json'); + case "echobox": + saveEchoboxDB(); + Sys.println('[${timestamp()}] Saved echobox-db.json'); } } @@ -305,6 +355,10 @@ class Onequestionmark { File.saveContent("settings.json", Json.stringify(settings, "\t")); } + public static function saveEchoboxDB() { + File.saveContent("echobox-db.json", Json.stringify(echoboxDB, "\t")); + } + // General functions public static function randInt(x, y) { // Return a random integer between x and y, both inclusive @@ -313,6 +367,7 @@ class Onequestionmark { public static function shutdown() { saveSettings(); + saveEchoboxDB(); Sys.exit(0); } } \ No newline at end of file