Added ytlookup function

This commit is contained in:
Izwzyzx 2024-11-09 14:22:29 -06:00
parent 08d023076b
commit 81523028ae
4 changed files with 57 additions and 3 deletions

View File

@ -1,6 +1,7 @@
# Changelog
## Release Version 1.2
- Added YouTube search command
- Fixed reconnection starting duplicate timers
- Added system to prevent MOTD from chosing the same daily result consecutively
- Added support for MOTDs matching only the date

View File

@ -1,6 +1,7 @@
import hxdiscord.DiscordClient;
import hxdiscord.types.*;
import hxdiscord.endpoints.Endpoints;
import htmlparser.HtmlDocument;
import haxe.Timer;
import haxe.Json;
import sys.io.File;
@ -220,6 +221,8 @@ class Onequestionmark {
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);
case "youtube":
if (msg.length > 0) {ytlookup(m,msg);}
// Gaming functions
case "coin":
if ((msg.length == 0) || (Std.parseInt(msg) == null) || (Std.parseInt(msg) == 1)) {
@ -435,7 +438,6 @@ class Onequestionmark {
special = false;
}
//if (day.length > 0) {msg = day[randInt(0, day.length-1)];}
if (day.length > 0) {
if (update && !special && (day.length > 2)) {
var today = Date.now().getDay();
@ -456,6 +458,55 @@ class Onequestionmark {
}
/**
* 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}');
http.onData = function (request) {
var data = new HtmlDocument(request);
var search = data.find("script"); // YouTube obfuscates everything into JS garbage so we have to check every <script>
var gotcha = "";
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
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=")));
}
// 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);
}
http.onError = function (error) {
Sys.println('[${timestamp()}] YTLOOKUP: Error - $error, request was $query');
m.reply({content:'Error in YouTube Lookup: $error'}, false);
}
http.request();
}
/**
* The `saveSystem()` function handles all filesystem writes. When the bot needs to save a file, it pushes data to the `saveQueue` array.
* The save system checks the queue regularly and operates on the first entry provided.

View File

@ -14,7 +14,8 @@ A `settings.json` file is required to run the bot. An example is provided below:
"motd": {
"date": "",
"time": 8,
"channels": []
"channels": [],
"previous": [0,0,0,0,0,0,0]
},
"debug": false,
"devmode": false,
@ -25,7 +26,7 @@ A `settings.json` file is required to run the bot. An example is provided below:
}
```
- onequestionmark will handle most of the `motd` values itself.
- `date` is modified by the bot during normal operations.
- `date` and `previous` are modified by the bot during normal operations.
- `time` determines the hour at which the MOTD is posted (24-hour notation, local time).
- Using the `?motd` command adds the current channel to the `channels` array.
- `debug` determines whether HxDiscord's console debug messages are enabled.

View File

@ -1,6 +1,7 @@
-m Onequestionmark
-L hxdiscord
-L IzzComLib
-L HtmlParser
# hxdiscord uses deprecated functions
-D no-deprecation-warnings
--neko export/onequestionmark.n