Compare commits

...

4 Commits

Author SHA1 Message Date
c71789e949 Added Pirate vs. Ninja commands 2024-11-14 19:07:33 -06:00
384dcc6416 Added ?trace command 2024-11-14 18:48:27 -06:00
d3801b48e0 Added new aliases 2024-11-10 22:19:47 -06:00
978f6bc932 Rewrote ytlookup to fix edge case issues 2024-11-10 13:14:17 -06:00
2 changed files with 158 additions and 40 deletions

View File

@ -1,6 +1,9 @@
# Changelog
## Release Version 1.2
- Added [Pirate vs. Ninja](https://majcher.com/project/pvn/pvn.cgi) commands: `?pirate`, `?ninja`, `?pvn`, `?nvp`
- Added `?trace` debug tool
- New aliases: `?fineart`, `?artishard`, `?indeed`, `?florida`, `?stop`
- Added YouTube search command
- Fixed reconnection starting duplicate timers
- Added system to prevent MOTD from chosing the same daily result consecutively

View File

@ -10,7 +10,7 @@ import izzcomlib.IzzComLib.*;
using StringTools;
class Onequestionmark {
// Initialize vars
// Initialize vars
static var Bot:DiscordClient;
static var settings:Dynamic;
static var botInfo:Dynamic;
@ -27,7 +27,7 @@ class Onequestionmark {
static var timerInit:Bool = false;
static function main() {
static function main() {
Sys.println('[${timestamp()}] Starting onequestionmark-chat');
@ -71,21 +71,24 @@ class Onequestionmark {
Sys.println('[${timestamp()}] Did not find echobox-db.json, generating');
File.saveContent("echobox-db.json", Json.stringify(echoboxDB, "\t"));
}
if (!FileSystem.exists("export")) {
FileSystem.createDirectory("export");
}
saveSystem();
// Start the bot
Bot = new DiscordClient(settings.token, [3276799], settings.debug);
Bot.onReady = onReady;
Bot.onMessageCreate = onMessageCreate;
Bot = new DiscordClient(settings.token, [3276799], settings.debug);
Bot.onReady = onReady;
Bot.onMessageCreate = onMessageCreate;
Bot.connect();
}
}
/**
* The `onReady()` function is run upon a successful `Bot.connect()`
*/
public static function onReady() {
public static function onReady() {
Sys.sleep(1);
Sys.println('[${timestamp()}] Bot is online');
@ -100,16 +103,16 @@ class Onequestionmark {
timerInit = true;
startMotd();
}
}
}
/**
* The `onMessageCreate()` event provides the bulk of onequestionmark's functionality.
* This is what triggers when a new Discord message is received, and contains the bot's command processor.
*/
public static function onMessageCreate(m:Message) {
public static function onMessageCreate(m:Message) {
// DevMode check
if ((!settings.devmode) || (m.guild_id == settings.devserver)) {
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;}
@ -223,6 +226,31 @@ class Onequestionmark {
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 "youtube":
if (msg.length > 0) {ytlookup(m,msg);}
case "ytdebug":
if (FileSystem.exists("export/ytlookup_result.txt")) {
var debugRename = 'export/${datestamp()}_${StringTools.lpad(Std.string(Date.now().getHours()), "0", 2)}-${StringTools.lpad(Std.string(Date.now().getMinutes()), "0", 2)}-${StringTools.lpad(Std.string(Date.now().getSeconds()), "0", 2)}_ytdebug.txt';
FileSystem.rename("export/ytlookup_result.txt", debugRename);
Sys.println('[${timestamp()}] ytdebug: Created debug file ${debugRename}');
m.react('');
} else {
Sys.println('[${timestamp()}] ytdebug: No result to debug');
m.reply({content:"ytdebug: No result to debug"}, false);
}
case "trace":
if (m.referenced_message == null) {
Sys.println('[${timestamp()}] trace: ${Json.stringify(getMessage(m.channel_id, m.id))}');
} else {
Sys.println('[${timestamp()}] trace: ${Json.stringify(getMessage(m.referenced_message.channel_id, m.referenced_message.id))}');
}
m.react('');
case "pirate":
if (msg.length > 0) {pvn("p",msg,m);}
case "ninja":
if (msg.length > 0) {pvn("n",msg,m);}
case "pvn":
if (msg.length > 0) {pvn("pvn",msg,m);}
case "nvp":
if (msg.length > 0) {pvn("nvp",msg,m);}
// Gaming functions
case "coin":
if ((msg.length == 0) || (Std.parseInt(msg) == null) || (Std.parseInt(msg) == 1)) {
@ -298,6 +326,16 @@ class Onequestionmark {
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);
case "fineart":
m.reply({content:"https://cdn.discordapp.com/attachments/270113422232911883/423323629116325898/unknown.png"}, false);
case "artishard":
m.reply({content:"https://cdn.discordapp.com/attachments/270113422232911883/356638565834424330/tumblr_nrxhzoF0KM1t75ioqo2_250.jpg"}, false);
case "indeed":
m.reply({content:"https://cdn.discordapp.com/attachments/1071547517847732305/1305252892491649145/indeed.gif"}, false);
case "florida":
m.reply({content:"https://cdn.discordapp.com/attachments/1071547517847732305/1305253566348529696/florida.gif"}, false);
case "stop":
m.reply({content:"https://cdn.discordapp.com/attachments/1071547517847732305/1305253985036275712/stop.jpg"}, false);
// Image database commands
case "hug":
if (hugDB.length > 0) {
@ -356,7 +394,7 @@ class Onequestionmark {
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);}
}
}
}
/**
@ -385,7 +423,7 @@ class Onequestionmark {
// Start the looped MOTD timer
motdTimer = new Timer(86400000);
motdTimer.run = function() {
motdTimer.run = function() {
settings.motd.date = datestamp();
postMotd();
saveQueue.push("settings");
@ -457,16 +495,66 @@ class Onequestionmark {
return msg;
}
/**
* This function scrapes PVN and replies with the appropriate results.
* @param choice `p`, `n`, `pvn`, or `nvp`.
* @param query The string to search.
*/
public static function pvn(choice:String,query:String,m:Message) {
if (choice.length > 1) {
if (query.contains(" | ")) {
var splitQuery = query.split(" | ");
query = '${splitQuery[0].urlEncode()}&n2=${splitQuery[1].urlEncode()}';
} else {
m.reply({content:'$choice: Must choose two combatants.'}, false);
return;
}
} else {
query = query.urlEncode();
}
var req = new haxe.Http('https://majcher.com/project/pvn/pvn.cgi?a=${choice.urlEncode()}&n1=${query}');
req.onData = function (request) {
var data = new HtmlDocument(request);
var search = data.find("table>tr>td");
try {
m.reply({content:'## ${search[0].innerText}\n${search[2].innerText} ${search[3].innerText}\n${search[4].innerText} ${search[5].innerText}\n${search[6].innerText} ${search[7].innerText}\n${search[8].innerText} ${search[9].innerText}\n**${search[10].innerText} ${search[11].innerText}**'}, false);
if (choice.length > 1) {
var result = data.find('div#result');
m.reply({content:'## ${search[12].innerText}\n${search[14].innerText} ${search[15].innerText}\n${search[16].innerText} ${search[17].innerText}\n${search[18].innerText} ${search[19].innerText}\n${search[20].innerText} ${search[21].innerText}\n**${search[22].innerText} ${search[23].innerText}**'}, false);
m.reply({content:'## ${result[0].innerText}'}, false);
}
} catch(e) {
m.reply({content:'$choice: Error - $e'}, false);
Sys.println('[${timestamp()}] $choice: Error - $e');
}
}
req.onError = function (error) {
m.reply({content:'Error in PVN: $error'}, false);
Sys.println('[${timestamp()}] $choice: Error - $error, request was $query');
}
req.request();
}
/**
* This function performs a YouTube search and replies with the first result.
* @param m The message data.
* @param query The string to search.
*/
* This function performs a YouTube search and replies with the first result.
* @param m The message data.
* @param query The string to search.
*/
public static function ytlookup(m:Message,query:String) {
var http = new haxe.Http('https://www.youtube.com/results?search_query=${query}');
var req = new haxe.Http('https://www.youtube.com/results?search_query=${query.urlEncode()}');
Sys.println('[${timestamp()}] ytlookup: URL - https://www.youtube.com/results?search_query=${query.urlEncode()}');
http.onData = function (request) {
req.onData = function (request) {
File.saveContent("export/ytlookup_result.txt", request);
var data = new HtmlDocument(request);
var search = data.find("script"); // YouTube obfuscates everything into JS garbage so we have to check every <script>
@ -474,36 +562,63 @@ class Onequestionmark {
var result = "Error: Unable to parse YouTube search result.";
for (i in search) {
if (i.toString().contains("https://youtu.be/")) { // Basic results link to the video like this
if (i.toString().contains("\"videoRenderer\":{\"videoId\":\"") || i.toString().contains("\"reelWatchEndpoint\":{\"videoId\":\"")) {
gotcha = i.toString();
result = gotcha.substring(gotcha.indexOf("https://youtu.be/"), gotcha.indexOf(".", gotcha.indexOf("https://youtu.be/")+17));
break;
}
else if (i.toString().contains("watch?v=")) { // Because YouTube isn't consistent, alternative scrape
gotcha = i.toString();
// Detect YouTube Shorts link and clean it up
if (gotcha.substring(gotcha.indexOf("watch?v=")+8, gotcha.indexOf("\"", gotcha.indexOf("watch?v="))).contains("\\")) {
result = "https://youtu.be/" + gotcha.substring(gotcha.indexOf("watch?v=")+8, gotcha.indexOf("\\", gotcha.indexOf("watch?v=")));
if ((i.toString().contains("\"reelWatchEndpoint\":{\"videoId\":\"")) && (gotcha.indexOf("\"reelWatchEndpoint\":{\"videoId\":\"") < gotcha.indexOf("\"videoRenderer\":{\"videoId\":\""))) {
// Top result is a Short
result = "https://youtu.be/" + gotcha.substring(gotcha.indexOf("\"reelWatchEndpoint\":{\"videoId\":\"")+32, gotcha.indexOf("\"", gotcha.indexOf("\"reelWatchEndpoint\":{\"videoId\":\"")+32));
Sys.println('[${timestamp()}] ytlookup: Result - $result (Short)');
break;
} else {
// Top result is a normal video
result = "https://youtu.be/" + gotcha.substring(gotcha.indexOf("\"videoRenderer\":{\"videoId\":\"")+28, gotcha.indexOf("\"", gotcha.indexOf("\"videoRenderer\":{\"videoId\":\"")+28));
Sys.println('[${timestamp()}] ytlookup: Result - $result (Normal)');
break;
}
// Normal video
else {
result = "https://youtu.be/" + gotcha.substring(gotcha.indexOf("watch?v=")+8, gotcha.indexOf("\"", gotcha.indexOf("watch?v=")));
}
break;
}
}
m.reply({content:result}, false);
m.reply({content:result}, false);
m.reply({content:"-# YouTube Lookup is still in beta. If the result seems inaccurate, please use the command `?ytdebug` so the developer can review the data that was received."}, false);
}
http.onError = function (error) {
Sys.println('[${timestamp()}] YTLOOKUP: Error - $error, request was $query');
m.reply({content:'Error in YouTube Lookup: $error'}, false);
req.onError = function (error) {
m.reply({content:'Error in YouTube Lookup: $error'}, false);
Sys.println('[${timestamp()}] ytlookup: Error - $error, request was $query');
}
http.request();
req.request();
}
/**
* Retrieves a specific message in the channel and returns it as a JSON object.
* @param channel_id Numerical ID of the channel.
* @param m_id Numerical ID of the message.
*/
public static function getMessage(channel_id:String, m_id:String) {
var req:hxdiscord.utils.Http = new hxdiscord.utils.Http("https://discord.com/api/v"+hxdiscord.gateway.Gateway.API_VERSION+"/channels/"+channel_id+"/messages/"+m_id);
req.addHeader("User-Agent", "hxdiscord (https://github.com/FurretDev/hxdiscord)");
req.addHeader("Authorization", DiscordClient.authHeader);
req.setMethod("GET");
var msg:Dynamic = null;
req.onData = function(data:String) {
msg = haxe.Json.parse(data);
}
req.onError = function(error) {
Sys.println('[${timestamp()}] getMessage: Error - $error');
Sys.println('[${timestamp()}] getMessage: ${req.responseData}');
}
req.send();
return msg;
}
@ -515,7 +630,7 @@ class Onequestionmark {
public static function saveSystem() {
Timer.delay(function() {
saveTimer = new Timer(60*1000);
saveTimer.run = function() {
saveTimer.run = function() {
if (saveQueue.length > 0) { // See if there's anything in the queue
switch (saveQueue.shift()) {
case "settings":