onequestionmark-chat/Onequestionmark.hx

296 lines
9.9 KiB
Haxe
Raw Normal View History

2023-02-26 14:46:18 -06:00
import hxdiscord.DiscordClient;
import hxdiscord.types.*;
import hxdiscord.endpoints.Endpoints;
2023-02-27 20:28:19 -06:00
import haxe.MainLoop;
2023-02-26 14:46:18 -06:00
import haxe.Json;
import sys.io.File;
import sys.FileSystem;
2023-02-26 17:53:53 -06:00
using StringTools;
2023-02-26 14:46:18 -06:00
class Onequestionmark {
2023-03-02 12:57:23 -06:00
// Initialize vars
static var Bot:DiscordClient;
2023-02-26 14:46:18 -06:00
static var settings:Dynamic;
2023-02-26 19:00:02 -06:00
static var botInfo:Dynamic;
static var iceRegex:EReg = ~/\bicc?ed?\b/i;
2023-03-02 12:09:41 -06:00
static var hugDB:Array<String>;
2023-04-17 21:55:46 -05:00
static var motdDB:haxe.DynamicAccess<Dynamic> = {};
2023-03-02 22:14:13 -06:00
static var yesnoDB:Dynamic;
2023-02-26 14:46:18 -06:00
static function main() {
2023-02-26 17:53:53 -06:00
Sys.println('[${timestamp()}] Starting onequestionmark-chat');
2023-02-26 14:46:18 -06:00
2023-03-02 12:57:23 -06:00
// Load files
2023-02-26 14:46:18 -06:00
if (FileSystem.exists("settings.json")) {
2023-02-26 17:53:53 -06:00
Sys.println('[${timestamp()}] Found settings.json, loading');
2023-02-26 14:46:18 -06:00
settings = Json.parse(File.getContent("settings.json"));
} else {
2023-02-26 17:53:53 -06:00
Sys.println('[${timestamp()}] Did not find settings.json, exiting');
2023-02-26 14:46:18 -06:00
Sys.exit(0);
}
2023-03-02 12:09:41 -06:00
if (FileSystem.exists("hug-db.json")) {
Sys.println('[${timestamp()}] Found hug-db.json, loading');
hugDB = Json.parse(File.getContent("hug-db.json"));
} else {
Sys.println('[${timestamp()}] Did not find hug-db.json, skipping');
hugDB = ["https://cdn.discordapp.com/attachments/270113422232911883/445026464623099907/brohugbump.gif"]; // Fallback
}
2023-03-02 12:57:23 -06:00
if (FileSystem.exists("motd-db.json")) {
Sys.println('[${timestamp()}] Found motd-db.json, loading');
motdDB = Json.parse(File.getContent("motd-db.json"));
} else {
Sys.println('[${timestamp()}] Did not find motd-db.json, skipping');
2023-04-17 21:55:46 -05:00
motdDB = Json.parse('{"sunday": [],"monday": [],"tuesday": [],"wednesday": [],"thursday": [],"friday": [],"saturday": []}'); // Fallback
2023-03-02 12:57:23 -06:00
}
2023-03-02 22:14:13 -06:00
if (FileSystem.exists("yesno-db.json")) {
Sys.println('[${timestamp()}] Found yesno-db.json, loading');
yesnoDB = Json.parse(File.getContent("yesno-db.json"));
} else {
Sys.println('[${timestamp()}] Did not find yesno-db.json, skipping');
yesnoDB = {"yes": [],"no": []}; // Fallback
}
2023-02-26 14:46:18 -06:00
// Start the bot
Bot = new DiscordClient(settings.token, [3276799], settings.debug);
Bot.onReady = onReady;
Bot.onMessageCreate = onMessageCreate;
2023-02-26 19:00:02 -06:00
botInfo = Endpoints.getCurrentUser();
2023-02-27 20:28:19 -06:00
MainLoop.add(motd);
2023-02-26 14:46:18 -06:00
}
public static function onReady() {
2023-02-26 17:53:53 -06:00
Sys.println('[${timestamp()}] Bot is online');
2023-02-26 16:11:00 -06:00
//Endpoints.sendMessage(settings.devchannel, {content:"onequestionmark-chat alpha test is now running"}, null, false);
2023-02-26 14:46:18 -06:00
}
public static function onMessageCreate(m:Message) {
2023-02-26 16:11:00 -06:00
// DevMode check
if ((!settings.devmode) || (m.guild_id == settings.devserver)) {
var msg = m.content;
// Command processor
if (msg.charAt(0) == "?") {
var command = "";
if (msg.indexOf(" ") != -1) {
// Separate command from rest of message, if necessary
command = msg.substring(1, msg.indexOf(" ")).toLowerCase();
msg = msg.substring(msg.indexOf(" ")+1);
} else {
// Otherwise, remove msg
command = msg.substring(1).toLowerCase();
msg = "";
}
switch (command) {
// Commands that require authorization
case "quit":
if (m.author.id == settings.botowner) {
m.react('');
2023-02-26 17:53:53 -06:00
Sys.println('[${timestamp()}] Shutdown command received from botowner, exiting');
2023-02-26 17:41:40 -06:00
shutdown();
2023-02-26 16:11:00 -06:00
}
2023-02-26 17:43:36 -06:00
case "devmode":
if (m.author.id == settings.botowner) {
if (!settings.devmode) {
settings.devmode = true;
m.reply({content:"DevMode enabled."}, false);
} else {
settings.devmode = false;
m.reply({content:"DevMode disabled."}, false);
}
}
case "motd":
if (m.author.id == settings.botowner) {
if (!settings.motd.channels.contains(m.channel_id)) {
settings.motd.channels.push(m.channel_id);
m.reply({content:'MOTD has been enabled for <#${m.channel_id}>'}, false);
} else {
settings.motd.channels.remove(m.channel_id);
m.reply({content:'MOTD has been disabled for <#${m.channel_id}>'}, false);
}
2023-03-02 12:09:41 -06:00
saveSettings();
2023-02-26 17:43:36 -06:00
}
2023-02-26 16:11:00 -06:00
// System for WIP commands that only work in testing server
case "wipcommand":
if (m.guild_id == settings.devserver) {
m.reply({content:"Dev Server response."}, true);
}
// Basic response commands
2023-03-01 14:26:52 -06:00
case "help":
m.reply({content:'
2023-03-02 22:14:13 -06:00
**onequestionmark bot commands**\n`?chk`: ack\n`?slap <target>`: The classic mIRC troutslap.\n`?hug <target (optional)>`: Posts randomized hug image.\n`?yes` and `?no`: Posts randomized yes/no.\n`?angery`\n`?subway`\n`?motd`: Enables MOTD for current channel (requires auth)\n`?devmode`: Toggles Dev Mode (requires auth)\n`?quit`: Shutdown command (requires auth)\n**Non-command bot functions:**\n*MOTD*: Bot will post a randomized *Meme of the Day* in enabled channels.\n*Icce*: Bot provides users with ice cuboids.\n*Meteor*: Bot reacts to falling rocks in the chat.'}, false);
2023-02-26 16:11:00 -06:00
case "chk":
m.reply({content:'<@${m.author.id}>: ack'}, false);
case "mrkrabs":
m.reply({content:'<@${m.author.id}>: ack ack ack ack ack'}, false);
2023-02-26 16:11:00 -06:00
case "slap":
if (msg.length != 0) {m.reply({content:'*onequestionmark slaps ${msg} around a bit with a large trout*'}, false);}
// Basic aliases
2023-02-26 16:11:00 -06:00
case "angery":
m.reply({content:"https://cdn.discordapp.com/attachments/1071547517847732305/1079518504413311108/angery.jpg"}, false);
case "subway":
m.reply({content:"https://www.youtube.com/watch?v=y3VRXVvr6XU"}, false);
case "illuminati":
m.reply({content:"https://cdn.discordapp.com/attachments/1071547517847732305/1147204253039984671/illuminati.gif"}, false);
case "cube":
m.reply({content:"https://cdn.discordapp.com/attachments/270113422232911883/502690458779123722/the_cube.jpg"}, false);
case "coolsville":
m.reply({content:"https://cdn.discordapp.com/attachments/1071547517847732305/1147583765212835921/coolsville.gif"}, false);
case "tufac":
m.reply({content:"*Not Teh Face, but better,*\n*Tufac to the letter!*\n*Always two faces, never one,*\n*Tufac has you on the run!*"}, false);
case "communism":
m.reply({content:"https://cdn.discordapp.com/attachments/1071547517847732305/1147590229960691742/communism.gif"}, false);
2023-02-26 17:41:40 -06:00
// Image database commands
case "hug":
2023-03-02 12:09:41 -06:00
m.reply({content:'🫂 *hugs ${msg}*\n${hugDB[randInt(0, hugDB.length-1)]}'}, false);
2023-03-02 22:14:13 -06:00
case "yes":
var yes = yesnoDB.yes;
m.reply({content:'${yes[randInt(0, yes.length-1)]}'}, false);
case "no":
var no = yesnoDB.no;
m.reply({content:'${no[randInt(0, no.length-1)]}'}, false);
2023-02-26 16:11:00 -06:00
}
}
// Non-command responses
if ((msg.charAt(0) == ".") && (msg.length == 1)) {m.reply({content:"omg a meteor"}, true);}
2023-02-26 19:00:02 -06:00
if ((iceRegex.match(msg)) && (m.author.id != botInfo.id)) {m.reply({content:"Did some carbon-based lifeform just say **I C E**?"});}
if ((m.mention_everyone == true) && (msg.charAt(0) != "?")) {m.reply({content:"https://cdn.discordapp.com/attachments/1071547517847732305/1147598637241741343/at_everyone.jpg"}, true);}
2023-02-26 16:11:00 -06:00
}
2023-02-26 14:46:18 -06:00
}
2023-02-26 17:41:40 -06:00
2023-02-27 20:28:19 -06:00
// Time-related functions
2023-02-26 17:53:53 -06:00
public static function timestamp() {
return '${StringTools.lpad(Std.string(Date.now().getHours()), "0", 2)}:${StringTools.lpad(Std.string(Date.now().getMinutes()), "0", 2)}';
}
2023-02-27 20:28:19 -06:00
public static function datestamp() {
return '${StringTools.lpad(Std.string(Date.now().getMonth()+1), "0", 2)}-${StringTools.lpad(Std.string(Date.now().getDate()), "0", 2)}';
}
2023-04-17 21:55:46 -05:00
// Clean print day of week
public static function checkDay() {
var day = "";
switch (Date.now().getDay()+1) {
case 1:
day = "sun";
case 2:
day = "mon";
case 3:
day = "tues";
case 4:
day = "wed";
case 5:
day = "thurs";
case 6:
day = "fri";
case 7:
day = "sat";
}
return day;
}
// Clean print month
public static function checkMonth() {
var month = "";
switch (Date.now().getMonth()+1) {
case 1:
month = "jan";
case 2:
month = "feb";
case 3:
month = "mar";
case 4:
month = "apr";
case 5:
month = "may";
case 6:
month = "jun";
case 7:
month = "jul";
case 8:
month = "aug";
case 9:
month = "sep";
case 10:
month = "oct";
case 11:
month = "nov";
case 12:
month = "dec";
}
return month;
}
//Get current day of month, padded
public static function checkDate() {
return StringTools.lpad(Std.string(Date.now().getDate()), "0", 2);
}
public static function monthdate() {
return '${checkMonth()}${checkDate()}';
}
public static function weekdate() {
return '${checkDay()}${checkDate()}';
}
2023-02-27 20:28:19 -06:00
public static function motd() {
if (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;
2023-03-02 12:09:41 -06:00
saveSettings();
2023-02-27 20:28:19 -06:00
var msg = "";
2023-03-02 12:57:23 -06:00
var day:Array<String> = [];
2023-04-17 21:55:46 -05:00
// Check for exact date
if (motdDB.exists('${monthdate()}-${Date.now().getFullYear()}')) {
day = motdDB.get('${monthdate()}-${Date.now().getFullYear()}');
}
// Else, check for special date
else if (motdDB.exists(monthdate())) {
2023-04-17 21:55:46 -05:00
day = motdDB.get(monthdate());
}
// Else, check for special day/date combo
else if (motdDB.exists(weekdate())) {
day = motdDB.get(weekdate());
}
// Otherwise, post daily meme
else if (motdDB.exists(checkDay())) {
day = motdDB.get(checkDay());
2023-02-27 20:28:19 -06:00
}
2023-03-02 12:57:23 -06:00
if (day.length > 0) {msg = day[randInt(0, day.length-1)];}
2023-02-27 20:28:19 -06:00
var channels:Array<String> = settings.motd.channels; // Because it won't let me do this directly
2023-03-02 12:57:23 -06:00
if (channels.length > 0) {for (i in channels) {if (msg.length > 0) {Endpoints.sendMessage(i, {content:msg}, null, false);}}}
2023-02-27 20:28:19 -06:00
}
}
// General functions
public static function randInt(x, y) {
// Return a random integer between x and y, both inclusive
return Std.random((y+1)-x)+x;
}
2023-02-26 17:41:40 -06:00
public static function saveSettings() {
File.saveContent("settings.json", Json.stringify(settings, "\t"));
}
public static function shutdown() {
saveSettings();
Sys.exit(0);
}
2023-02-26 14:46:18 -06:00
}