From 94c5403ebd0c1b4bab57aeeceab2e9de78cba719 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 3 Jul 2023 14:00:20 +0200 Subject: [PATCH 01/13] UrbanDict - WIP: New module --- source/botty/modules/urbandict.d | 64 ++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 source/botty/modules/urbandict.d diff --git a/source/botty/modules/urbandict.d b/source/botty/modules/urbandict.d new file mode 100644 index 0000000..edea0e0 --- /dev/null +++ b/source/botty/modules/urbandict.d @@ -0,0 +1,64 @@ +/** + * Urban Dictionary module + */ +module botty.modules.urbandict; + +import botty.mod; +import botty.bot : Bot; +import birchwood.protocol.messages : Message; + +/** + * Urban dictionary command + */ +public final class UrbanDict : Mod +{ + private static string commandStr = ".ub"; + + /** + * Base URL to use for constructing requests + */ + private static string ubBase = "http://api.urbandictionary.com/v0/define?term="; + + /** + * Constructs a new `UrbanDict` module with the provided + * bot to associate with + * + * Params: + * bot = the `Bot` to associate with + */ + this(Bot bot) + { + super(bot); + } + + public override bool accepts(Message fullMessage, string channel, string messageBody) + { + import std.string : startsWith; + return messageBody.startsWith(commandStr); + } + + public override void react(Message fullMessage, string channel, string messageBody) + { + import std.string : indexOf, strip; + + import std.stdio; + import std.string : split; + + /** + * Split the ` .ub dictionarydef` + * into two parts `[.ub, dictionarydef]` + */ + string[] splits = split(strip(messageBody), " "); + writeln("splits", splits); + + if(splits.length >= 2) + { + string searchTerm = splits[1]; + getBot().channelMessage("hehe", channel); + } + else + { + // Do nothing + } + } +} \ No newline at end of file From ab83859175f1d7b8b6bd785322a69cef63c54066 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 3 Jul 2023 14:00:33 +0200 Subject: [PATCH 02/13] Bot - Enabled `.ub` command --- source/botty/bot.d | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/botty/bot.d b/source/botty/bot.d index 79c6700..359d187 100644 --- a/source/botty/bot.d +++ b/source/botty/bot.d @@ -29,12 +29,14 @@ public class Bot : Client import botty.modules.ping : Ping; import botty.modules.rot13 : Rot13; import botty.modules.translate : Translate; + import botty.modules.urbandict : UrbanDict; modules ~= [ new DeavmiComedy(this), new EskomCalendarAPI(this), new Ping(this), new Rot13(this), - new Translate(this) + new Translate(this), + new UrbanDict(this) ]; } From b7dbb8061057b9e991357082fec66b34f228405c Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 3 Jul 2023 14:07:55 +0200 Subject: [PATCH 03/13] UrbanDict - Added rudimentary UB fetch code (no parsing yet) - Improved the `searchTerm` extraction algo --- source/botty/modules/urbandict.d | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/source/botty/modules/urbandict.d b/source/botty/modules/urbandict.d index edea0e0..95889fc 100644 --- a/source/botty/modules/urbandict.d +++ b/source/botty/modules/urbandict.d @@ -6,6 +6,7 @@ module botty.modules.urbandict; import botty.mod; import botty.bot : Bot; import birchwood.protocol.messages : Message; +import std.net.curl : CurlException; /** * Urban dictionary command @@ -48,17 +49,34 @@ public final class UrbanDict : Mod * Split the ` .ub dictionarydef` * into two parts `[.ub, dictionarydef]` */ - string[] splits = split(strip(messageBody), " "); + messageBody = strip(messageBody); + string[] splits = split(messageBody, " "); writeln("splits", splits); if(splits.length >= 2) { - string searchTerm = splits[1]; - getBot().channelMessage("hehe", channel); + + string searchTerm = strip(messageBody[messageBody.indexOf(" ")..$]); + + getBot().channelMessage("hehe", searchTerm); } else { // Do nothing } } + + private static void doThing(string term) + { + import std.string : fromStringz; + import etc.c.curl : curl_escape; + term = cast(string)fromStringz(curl_escape(cast(const(char)*)term.ptr, cast(int)term.length)); + + // Do the request + import std.net.curl; + string data = cast(string)get(ubBase~term); + + import std.stdio; + writeln("UB result: ", data); + } } \ No newline at end of file From e023dc8018d7c61784c0aabd18e05653d5b8ab60 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 3 Jul 2023 14:13:54 +0200 Subject: [PATCH 04/13] UrbanDict - Fixed message send - Added JSON parsing of result - Handle Curl and JSON parsing errors now --- source/botty/modules/urbandict.d | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/source/botty/modules/urbandict.d b/source/botty/modules/urbandict.d index 95889fc..9187984 100644 --- a/source/botty/modules/urbandict.d +++ b/source/botty/modules/urbandict.d @@ -55,10 +55,13 @@ public final class UrbanDict : Mod if(splits.length >= 2) { - string searchTerm = strip(messageBody[messageBody.indexOf(" ")..$]); - getBot().channelMessage("hehe", searchTerm); + doThing(searchTerm); + + + + getBot().channelMessage("hehe", channel); } else { @@ -74,9 +77,30 @@ public final class UrbanDict : Mod // Do the request import std.net.curl; - string data = cast(string)get(ubBase~term); + import std.stdio; - writeln("UB result: ", data); + // writeln("UB result: ", data); + + + + try + { + string data = cast(string)get(ubBase~term); + JSONValue json = parseJSON(data); + + // TODO: Send result below + // getBot().channelMessage(translatedText, channel); + } + // On network error + catch(CurlException e) + { + getBot().channelMessage("There was a network error when looking up on urban dictionary", channel); + } + // On parsing error + catch(JSONException e) + { + getBot().channelMessage("There was a parsing error when looking up on urban dictionary", channel); + } } } \ No newline at end of file From d8096caba2f0ff257303f044ada8822e40d48ef1 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 3 Jul 2023 14:14:18 +0200 Subject: [PATCH 05/13] UrbanDict - Made `doTing(string)` instance method --- source/botty/modules/urbandict.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/botty/modules/urbandict.d b/source/botty/modules/urbandict.d index 9187984..39447c3 100644 --- a/source/botty/modules/urbandict.d +++ b/source/botty/modules/urbandict.d @@ -69,7 +69,7 @@ public final class UrbanDict : Mod } } - private static void doThing(string term) + private void doThing(string term) { import std.string : fromStringz; import etc.c.curl : curl_escape; From fd6efacb1c606113fd0c3fb746632e69015636fd Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 3 Jul 2023 14:16:33 +0200 Subject: [PATCH 06/13] UbranDIct - Made static again (`doThing(string)`) - Now returns JSON --- source/botty/modules/urbandict.d | 46 +++++++++++++++++--------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/source/botty/modules/urbandict.d b/source/botty/modules/urbandict.d index 39447c3..773a5db 100644 --- a/source/botty/modules/urbandict.d +++ b/source/botty/modules/urbandict.d @@ -7,6 +7,7 @@ import botty.mod; import botty.bot : Bot; import birchwood.protocol.messages : Message; import std.net.curl : CurlException; +import std.json : JSONValue, JSONException, parseJSON; /** * Urban dictionary command @@ -57,7 +58,24 @@ public final class UrbanDict : Mod { string searchTerm = strip(messageBody[messageBody.indexOf(" ")..$]); - doThing(searchTerm); + try + { + // Perform lookup and parsing + JSONValue json = doThing(searchTerm); + + // TODO: Send result below + // getBot().channelMessage(translatedText, channel); + } + // On network error + catch(CurlException e) + { + getBot().channelMessage("There was a network error when looking up on urban dictionary", channel); + } + // On parsing error + catch(JSONException e) + { + getBot().channelMessage("There was a parsing error when looking up on urban dictionary", channel); + } @@ -69,7 +87,7 @@ public final class UrbanDict : Mod } } - private void doThing(string term) + private static JSONValue doThing(string term) { import std.string : fromStringz; import etc.c.curl : curl_escape; @@ -82,25 +100,11 @@ public final class UrbanDict : Mod import std.stdio; // writeln("UB result: ", data); - + string data = cast(string)get(ubBase~term); + JSONValue json = parseJSON(data); - try - { - string data = cast(string)get(ubBase~term); - JSONValue json = parseJSON(data); - - // TODO: Send result below - // getBot().channelMessage(translatedText, channel); - } - // On network error - catch(CurlException e) - { - getBot().channelMessage("There was a network error when looking up on urban dictionary", channel); - } - // On parsing error - catch(JSONException e) - { - getBot().channelMessage("There was a parsing error when looking up on urban dictionary", channel); - } + return json; + + } } \ No newline at end of file From f8a2ab8265f7eddd30f6ae29807dac06a6d24ee1 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 3 Jul 2023 14:23:44 +0200 Subject: [PATCH 07/13] UrbanDict - Ensure we have atleats one definition - In case of atleast one defintion, extract data, format and send it in similar fashion to that of PyBotty - `doThing(string)` now returns the list of definitions --- source/botty/modules/urbandict.d | 35 +++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/source/botty/modules/urbandict.d b/source/botty/modules/urbandict.d index 773a5db..af181c5 100644 --- a/source/botty/modules/urbandict.d +++ b/source/botty/modules/urbandict.d @@ -61,10 +61,33 @@ public final class UrbanDict : Mod try { // Perform lookup and parsing - JSONValue json = doThing(searchTerm); + JSONValue[] definitions = doThing(searchTerm); - // TODO: Send result below - // getBot().channelMessage(translatedText, channel); + if(definitions.length >= 0) + { + // TODO: Send result below + // getBot().channelMessage(translatedText, channel); + + JSONValue firstDef = definitions[0]; + + string definition = firstDef["definition"].str(); + string example = firstDef["example"].str(); + string author = firstDef["author"].str(); + string permalink = firstDef["permalink"].str(); + + import birchwood.protocol.formatting; + + getBot().channelMessage(bold("Definition: ")~definition, channel); + getBot().channelMessage(bold("Example: ")~example, channel); + getBot().channelMessage(bold("Author: ")~author, channel); + getBot().channelMessage(bold("Permalink: ")~permalink, channel); + } + else + { + // TODO: Handl eno defintions + // TODO: Send result below + // getBot().channelMessage(translatedText, channel); + } } // On network error catch(CurlException e) @@ -87,7 +110,7 @@ public final class UrbanDict : Mod } } - private static JSONValue doThing(string term) + private static JSONValue[] doThing(string term) { import std.string : fromStringz; import etc.c.curl : curl_escape; @@ -101,10 +124,8 @@ public final class UrbanDict : Mod // writeln("UB result: ", data); string data = cast(string)get(ubBase~term); - JSONValue json = parseJSON(data); + JSONValue[] json = parseJSON(data)["list"].array(); return json; - - } } \ No newline at end of file From d2423d986a07fe7965b5badfb0af614d2f7ce34a Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 3 Jul 2023 14:27:34 +0200 Subject: [PATCH 08/13] UrbandDict - Removed now-completed TODOs - Now on no definitions we send an error message --- source/botty/modules/urbandict.d | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/source/botty/modules/urbandict.d b/source/botty/modules/urbandict.d index af181c5..3941ff6 100644 --- a/source/botty/modules/urbandict.d +++ b/source/botty/modules/urbandict.d @@ -82,11 +82,10 @@ public final class UrbanDict : Mod getBot().channelMessage(bold("Author: ")~author, channel); getBot().channelMessage(bold("Permalink: ")~permalink, channel); } + // If no definitions are found else { - // TODO: Handl eno defintions - // TODO: Send result below - // getBot().channelMessage(translatedText, channel); + getBot().channelMessage("No definitions for '"~searchTerm~"'", channel); } } // On network error @@ -99,10 +98,6 @@ public final class UrbanDict : Mod { getBot().channelMessage("There was a parsing error when looking up on urban dictionary", channel); } - - - - getBot().channelMessage("hehe", channel); } else { From 0638b6c713dd0c1daaf466a8239511b6939cf58c Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 3 Jul 2023 14:34:43 +0200 Subject: [PATCH 09/13] App - Read in the server's address and port from the configuration file --- source/botty/app.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/botty/app.d b/source/botty/app.d index 74c5f3f..717c87e 100644 --- a/source/botty/app.d +++ b/source/botty/app.d @@ -33,7 +33,7 @@ void main(string[] args) } // TODO: Extract config info here and set it in the `connInfo` below - ConnectionInfo connInfo = ConnectionInfo.newConnection("worcester.community.networks.deavmi.assigned.network", 6667, "bottyng", "doggie", "Tristan B. Kildaire"); + ConnectionInfo connInfo = ConnectionInfo.newConnection(config.serverAddr, config.serverPort, "bottyng", "doggie", "Tristan B. Kildaire"); // // Set the fakelag to 1 second // connInfo.setFakeLag(1); From bdd96f63d342d51b2ceb1938bb3c4fb66977042d Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 3 Jul 2023 14:37:41 +0200 Subject: [PATCH 10/13] App - Cast to `ushort` --- source/botty/app.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/botty/app.d b/source/botty/app.d index 717c87e..2ab75d7 100644 --- a/source/botty/app.d +++ b/source/botty/app.d @@ -33,7 +33,7 @@ void main(string[] args) } // TODO: Extract config info here and set it in the `connInfo` below - ConnectionInfo connInfo = ConnectionInfo.newConnection(config.serverAddr, config.serverPort, "bottyng", "doggie", "Tristan B. Kildaire"); + ConnectionInfo connInfo = ConnectionInfo.newConnection(config.serverAddr, cast(ushort)config.serverPort, "bottyng", "doggie", "Tristan B. Kildaire"); // // Set the fakelag to 1 second // connInfo.setFakeLag(1); From d59a538851f7fc72358e716c81b7ba53314270ca Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 3 Jul 2023 14:39:28 +0200 Subject: [PATCH 11/13] - Updated supported modules --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 0b10d83..b67d8d0 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,9 @@ Many modules are supported (you can see [here]()) but to name a few we have: * You can do Rot13 using `.rot13 ` 3. Ping * Let's you ping a endpoint with `.ping ` +4. Urban dictionary + * Let's you ookup definitions on Urban dictionary + * Using `.ub Marshmellow` ## Credits From ebe50679b7388006ec11903532037c24e0f137b5 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 3 Jul 2023 14:41:24 +0200 Subject: [PATCH 12/13] App - Set fake lag to `0` --- source/botty/app.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/botty/app.d b/source/botty/app.d index 2ab75d7..99271fd 100644 --- a/source/botty/app.d +++ b/source/botty/app.d @@ -36,7 +36,7 @@ void main(string[] args) ConnectionInfo connInfo = ConnectionInfo.newConnection(config.serverAddr, cast(ushort)config.serverPort, "bottyng", "doggie", "Tristan B. Kildaire"); // // Set the fakelag to 1 second - // connInfo.setFakeLag(1); + connInfo.setFakeLag(0); Bot botty = new Bot(connInfo, config); From ae31c7b2c0d38c2cd220013bb18cf9788fd47437 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Mon, 3 Jul 2023 14:51:24 +0200 Subject: [PATCH 13/13] UrbandiDt - Added some debugging --- source/botty/modules/urbandict.d | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/source/botty/modules/urbandict.d b/source/botty/modules/urbandict.d index 3941ff6..169c038 100644 --- a/source/botty/modules/urbandict.d +++ b/source/botty/modules/urbandict.d @@ -56,7 +56,9 @@ public final class UrbanDict : Mod if(splits.length >= 2) { + writeln("Are we ongod yet? ",messageBody.indexOf(" ")); string searchTerm = strip(messageBody[messageBody.indexOf(" ")..$]); + writeln("search term: '"~searchTerm~"'"); try { @@ -77,6 +79,12 @@ public final class UrbanDict : Mod import birchwood.protocol.formatting; + writeln("Def '"~definition~"'"); + writeln("Ex '"~example~"'"); + writeln("Au '"~definition~"'"); + writeln("Perm '"~permalink~"'"); + + getBot().channelMessage(bold("Definition: ")~definition, channel); getBot().channelMessage(bold("Example: ")~example, channel); getBot().channelMessage(bold("Author: ")~author, channel); @@ -102,6 +110,14 @@ public final class UrbanDict : Mod else { // Do nothing + writeln("IMM A W "); + writeln("IMM A W "); + writeln("IMM A W "); + writeln("IMM A W "); + writeln("IMM A W "); + writeln("IMM A W "); + writeln("IMM A W "); + writeln("IMM A W "); } } @@ -116,9 +132,10 @@ public final class UrbanDict : Mod import std.stdio; - // writeln("UB result: ", data); + string data = cast(string)get(ubBase~term); + writeln("UB result: ", data); JSONValue[] json = parseJSON(data)["list"].array(); return json;