onequestionmark-chat/Onequestionmark.hx
2023-03-02 12:57:23 -06:00

200 lines
7.0 KiB
Haxe

import hxdiscord.DiscordClient;
import hxdiscord.types.*;
import hxdiscord.endpoints.Endpoints;
import haxe.MainLoop;
import haxe.Json;
import sys.io.File;
import sys.FileSystem;
using StringTools;
class Onequestionmark {
// Initialize vars
static var Bot:DiscordClient;
static var settings:Dynamic;
static var botInfo:Dynamic;
static var iceRegex:EReg = ~/\bicc?ed?\b/i;
static var hugDB:Array<String>;
static var motdDB:Dynamic;
static function main() {
Sys.println('[${timestamp()}] Starting onequestionmark-chat');
// Load files
if (FileSystem.exists("settings.json")) {
Sys.println('[${timestamp()}] Found settings.json, loading');
settings = Json.parse(File.getContent("settings.json"));
} else {
Sys.println('[${timestamp()}] Did not find settings.json, exiting');
Sys.exit(0);
}
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
}
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');
motdDB = {"sunday": [],"monday": [],"tuesday": [],"wednesday": [],"thursday": [],"friday": [],"saturday": []}; // Fallback
}
// Start the bot
Bot = new DiscordClient(settings.token, [3276799], settings.debug);
Bot.onReady = onReady;
Bot.onMessageCreate = onMessageCreate;
botInfo = Endpoints.getCurrentUser();
MainLoop.add(motd);
}
public static function onReady() {
Sys.println('[${timestamp()}] Bot is online');
//Endpoints.sendMessage(settings.devchannel, {content:"onequestionmark-chat alpha test is now running"}, null, false);
}
public static function onMessageCreate(m:Message) {
// 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.reply({content:"Shutdown command received from botowner, exiting."}, false);
Sys.println('[${timestamp()}] Shutdown command received from botowner, exiting');
shutdown();
}
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);
}
saveSettings();
}
// 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
case "help":
m.reply({content:'
**onequestionmark bot commands**\n`?chk`: ack\n`?slap <target>`: The classic mIRC troutslap.\n`?hug <target (optional)>`: Posts randomized hug image.\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);
case "chk":
m.reply({content:'<@${m.author.id}>: ack'}, false);
case "slap":
if (msg.length != 0) {m.reply({content:'*onequestionmark slaps ${msg} around a bit with a large trout*'}, false);}
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);
// Image database commands
case "hug":
m.reply({content:'🫂 *hugs ${msg}*\n${hugDB[randInt(0, hugDB.length-1)]}'}, false);
}
}
// Non-command responses
if ((msg.charAt(0) == ".") && (msg.length == 1)) {m.reply({content:"omg a meteor"}, true);}
if ((iceRegex.match(msg)) && (m.author.id != botInfo.id)) {m.reply({content:"Did some carbon-based lifeform just say **I C E**?"});}
}
}
// Time-related functions
public static function timestamp() {
return '${StringTools.lpad(Std.string(Date.now().getHours()), "0", 2)}:${StringTools.lpad(Std.string(Date.now().getMinutes()), "0", 2)}';
}
public static function datestamp() {
return '${StringTools.lpad(Std.string(Date.now().getMonth()+1), "0", 2)}-${StringTools.lpad(Std.string(Date.now().getDate()), "0", 2)}';
}
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;
saveSettings();
var msg = "";
var day:Array<String> = [];
//events
switch (Date.now().getDay()) {
case 0: // Sunday
day = motdDB.sunday;
case 1: // Monday
day = motdDB.monday;
case 2: // Tuesday
day = motdDB.tuesday;
case 3: // Wednesday
day = motdDB.wednesday;
case 4: // Thursday
day = motdDB.thursday;
case 5: // Friday
day = motdDB.friday;
case 6: // Saturday
day = motdDB.saturday;
}
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
if (channels.length > 0) {for (i in channels) {if (msg.length > 0) {Endpoints.sendMessage(i, {content:msg}, null, false);}}}
}
}
// 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;
}
public static function saveSettings() {
File.saveContent("settings.json", Json.stringify(settings, "\t"));
}
public static function shutdown() {
saveSettings();
Sys.exit(0);
}
}