8 Commits
v1.0 ... v1.1

2 changed files with 224 additions and 16 deletions

4
.gitignore vendored
View File

@@ -1,5 +1,7 @@
# ignore all files in export dir
export/
modules/
# ignore bot settings file
settings.json
settings.json
echobox-db.json

View File

@@ -13,9 +13,15 @@ class Onequestionmark {
static var settings:Dynamic;
static var botInfo:Dynamic;
static var iceRegex:EReg = ~/\bicc?ed?\b/i;
static var hugDB:Array<String>;
static var motdDB:haxe.DynamicAccess<Dynamic> = {};
static var yesnoDB:Dynamic;
static var echoboxDB:haxe.DynamicAccess<Dynamic> = {};
static var saveTimer = Date.now().getMinutes();
static var saveQueue:Array<String> = [];
static function main() {
@@ -54,28 +60,47 @@ class Onequestionmark {
yesnoDB = {"yes": [],"no": []}; // Fallback
}
if (FileSystem.exists("echobox-db.json")) {
Sys.println('[${timestamp()}] Found echobox-db.json, loading');
echoboxDB = 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;
Bot.onMessageCreate = onMessageCreate;
botInfo = Endpoints.getCurrentUser();
MainLoop.add(motd);
Bot.connect();
}
public static function onReady() {
Sys.sleep(1);
Sys.println('[${timestamp()}] Bot is online');
//Endpoints.sendMessage(settings.devchannel, {content:"onequestionmark-chat alpha test is now running"}, null, false);
// Join message from program args
if (Sys.args().length > 0) {
Endpoints.sendMessage(settings.devchannel, {content:Sys.args().join(" ")}, null, false);
}
botInfo = Endpoints.getCurrentUser();
MainLoop.add(motd);
MainLoop.add(saveSystem);
}
public static function onMessageCreate(m:Message) {
// DevMode check
if ((!settings.devmode) || (m.guild_id == settings.devserver)) {
var msg = m.content;
var sender:String;
if (m.getMember().nick == null) {sender = m.author.global_name;} else {sender = m.getMember().nick;}
// Command processor
if (msg.charAt(0) == "?") {
var command = "";
var args:Array<String> = [];
if (msg.indexOf(" ") != -1) {
// Separate command from rest of message, if necessary
@@ -87,11 +112,17 @@ class Onequestionmark {
msg = "";
}
// Separate arguments from command
if (command.indexOf(".") != -1) {
args = command.substring(command.indexOf(".")+1).toLowerCase().split(".");
command = command.substring(0, command.indexOf(".")).toLowerCase();
}
switch (command) {
// Commands that require authorization
case "quit":
if (m.author.id == settings.botowner) {
m.reply({content:"Shutdown command received from botowner, exiting."}, false);
m.react('');
Sys.println('[${timestamp()}] Shutdown command received from botowner, exiting');
shutdown();
}
@@ -114,7 +145,7 @@ class Onequestionmark {
settings.motd.channels.remove(m.channel_id);
m.reply({content:'MOTD has been disabled for <#${m.channel_id}>'}, false);
}
saveSettings();
saveQueue.push("settings");
}
// System for WIP commands that only work in testing server
case "wipcommand":
@@ -123,16 +154,127 @@ class Onequestionmark {
}
// 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`?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);
/*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`?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);*/
m.reply({embeds: [
{
"color": 13733022,
"url": "https://discord.com",
"author": {
"name": "onequestionmark help",
"url": "https://discord.com",
"icon_url": "https://cdn.discordapp.com/avatars/1071524991084015756/d623b45d33e8119599b29c5cf5ed532e.png"
},
"thumbnail": {
"url": "https://cdn.discordapp.com/emojis/1071560243051511808.png"
},
"fields": [
{
"name": "**Bot commands:**",
"value": "`?help`: Posts this help document. :book:\n`?chk`: Replies with \"ack\". :speaking_head:\n`?slap <target>`: The classic mIRC troutslap. :fish:\n`?hug <target (optional)>`: Posts randomized hug image. :people_hugging:\n`?yes`: Posts randomized \"yes\" image or video. :thumbsup:\n`?no`: Posts randomized \"no\" image or video. :no_entry_sign:\n`?mrkrabs`: Similar to `?chk`. :crab:\n`?tufac`: The official Tufac Theme Song. :busts_in_silhouette:\n`?coin <count (optional)>`: Flips between 1 and 100 coins. :coin:\n`?dice.<arg> <num>`: Rolls <arg>-sided die <num> times :game_die:\n`?8ball`: Answers your yes or no questions. :8ball:\n`?echobox`: Posts randomized quote from the quote database. :loudspeaker:\n`?echobox <quote>`: Adds provided quote to the database. :writing_hand:",
"inline": false
},
{
"name": "**Basic Response Aliases:**",
"value": "`?angery`: *I taste a vegetal.* :rage:\n`?subway`: Arin's infamous Subway rant. :sandwich:\n`?illuminati`: Always watching. :eye:\n`?cube`: *The Cube*\n`?coolsville`: Population: Us :sunglasses:\n`?communism`: The old 3D rotating gif.\n`?opinions`: Here they come. :scream:",
"inline": false
},
{
"name": "**Auth-Requiring Commands:**",
"value": "`?motd`: Enables MOTD for current channel\n`?devmode`: Toggles Dev Mode\n`?quit`: Shutdown command",
"inline": true
},
{
"name": "**Non-command bot functions:**",
"value": "**MOTD**: *Meme of the Day* is posted in enabled channels.\n**Icce**: Bot provides users with ice cuboids. :ice_cube:",
"inline": true
}
]
}
]});
case "chk":
m.reply({content:'<@${m.author.id}>: ack'}, false);
case "mrkrabs":
m.reply({content:'<@${m.author.id}>: ack ack ack ack ack'}, false);
case "slap":
if (msg.length != 0) {m.reply({content:'*onequestionmark slaps ${msg} around a bit with a large trout*'}, 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);
// Gaming functions
case "coin":
if ((msg.length == 0) || (Std.parseInt(msg) == null) || (Std.parseInt(msg) == 1)) {
var coin = randInt(0,1);
if (coin == 1) {
m.reply({content:'${sender} flipped a coin and got heads.'}, false);
} else {
m.reply({content:'${sender} flipped a coin and got tails.'}, false);
}
} else {
if ((Std.parseInt(msg) > 1) && (Std.parseInt(msg) < 101)) {
var heads:Int = 0;
for (i in 1...(Std.parseInt(msg)+1)) {
heads += randInt(0,1);
}
m.reply({content:'${sender} flipped ${Std.parseInt(msg)} coins.\n**Heads:** ${heads}\n**Tails:** ${Std.parseInt(msg)-heads}'}, false);
} else {
m.reply({content:'Please enter a value between 1 and 100.'}, false);
}
}
case "dice":
var sides:Int = 6;
// Set number of sides
if (args.length != 0) {
if (Std.parseInt(args[0]) != null) {
sides = Std.parseInt(args[0]);
}
}
// Validate sides
if (sides == 1) {
m.reply({content:'${sender} rolled a ball or something, idk.'}, false);
} else if (sides == 2) {
m.reply({content:'${sender} tried to roll a 2-sided die (this is called a coin btw).'}, false);
} else if ((sides < 1) || (sides > 100)) {
m.reply({content:'Please enter a value between 1 and 100.'}, false);
} else {
// Roll a single die
if ((msg.length == 0) || (Std.parseInt(msg) == null) || (Std.parseInt(msg) == 1)) {
m.reply({content:'${sender} rolled a ${sides}-sided die and got ${randInt(1,sides)}.'}, false);
} else {
// Roll multiple dice
if ((Std.parseInt(msg) > 1) && (Std.parseInt(msg) < 101)) {
var results:Array<Int> = [];
for (i in 1...(Std.parseInt(msg)+1)) {
results.push(randInt(1,sides));
}
// Sort results high-to-low
results.sort((a, b) -> b - a);
m.reply({content:'${sender} rolled a ${sides}-sided die ${Std.parseInt(msg)} times.\n**Results:** ${results.toString()}'}, false);
} else {
m.reply({content:'Please enter a value between 1 and 100.'}, false);
}
}
}
case "8ball":
var response:Array<String> = ["It is certain.","It is decidedly so.","Without a doubt.","Yes definitely.","You may rely on it.","As I see it, yes.","Most likely.","Outlook good.","Yes.","Signs point to yes.","Reply hazy, try again.","Ask again later.","Better not tell you now.","Cannot predict now.","Concentrate and ask again.","Don't count on it.","My reply is no.","My sources say no.","Outlook not so good.","Very doubtful."];
m.reply({content:'🎱 ${response[randInt(0,19)]} 🎱'}, false);
// Basic aliases
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 "communism":
m.reply({content:"https://cdn.discordapp.com/attachments/1071547517847732305/1147590229960691742/communism.gif"}, false);
case "opinions":
m.reply({content:"https://cdn.discordapp.com/attachments/1071547517847732305/1147983375701921892/opinions.jpg"}, false);
// Image database commands
case "hug":
m.reply({content:'🫂 *hugs ${msg}*\n${hugDB[randInt(0, hugDB.length-1)]}'}, false);
@@ -142,12 +284,48 @@ 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<String> = echoboxDB.get('server${m.guild_id}');
var quote:String = "";
// Echobox response
if ((!m.mention_everyone) && (m.mentions.length == 0) && (m.mention_roles.length == 0)) {
if (echobox.length > 0) {
var ebchoice = randInt(0, echobox.length-1);
quote = '**Echobox, quote #${ebchoice+1}:** ${echobox[ebchoice]}';
} else {
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 mentions to the Echobox.'}, 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**?"});}
if ((m.mention_everyone == true) && (msg.charAt(0) != "?")) {m.reply({content:"https://cdn.discordapp.com/attachments/1071547517847732305/1147598637241741343/at_everyone.jpg"}, true);}
}
}
@@ -235,13 +413,17 @@ class Onequestionmark {
if ((settings.motd.posted == false) && (Date.now().getHours() >= settings.motd.time)) {
settings.motd.posted = true;
saveSettings();
saveQueue.push("settings");
var msg = "";
var day:Array<String> = [];
// Check for special date
if (motdDB.exists(monthdate())) {
// 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())) {
day = motdDB.get(monthdate());
}
// Else, check for special day/date combo
@@ -261,18 +443,42 @@ class Onequestionmark {
}
}
// 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;
// Filesystem operations
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
switch (saveQueue.shift()) {
case "settings":
saveSettings();
Sys.println('[${timestamp()}] Saved settings.json');
case "echobox":
saveEchoboxDB();
Sys.println('[${timestamp()}] Saved echobox-db.json');
}
}
saveTimer = Date.now().getMinutes(); // Update timer
}
}
public static function saveSettings() {
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
return Std.random((y+1)-x)+x;
}
public static function shutdown() {
saveSettings();
saveEchoboxDB();
Sys.exit(0);
}
}