Compare commits

...

6 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire d2423d986a UrbandDict
- Removed now-completed TODOs
- Now on no definitions we send an error message
2023-07-03 14:27:34 +02:00
Tristan B. Velloza Kildaire f8a2ab8265 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
2023-07-03 14:23:44 +02:00
Tristan B. Velloza Kildaire fd6efacb1c UbranDIct
- Made static again (`doThing(string)`)
- Now returns JSON
2023-07-03 14:16:33 +02:00
Tristan B. Velloza Kildaire d8096caba2 UrbanDict
- Made `doTing(string)` instance method
2023-07-03 14:14:18 +02:00
Tristan B. Velloza Kildaire e023dc8018 UrbanDict
- Fixed message send
- Added JSON parsing of result
- Handle Curl and JSON parsing errors now
2023-07-03 14:13:54 +02:00
Tristan B. Velloza Kildaire b7dbb80610 UrbanDict
- Added rudimentary UB fetch code (no parsing yet)
- Improved the `searchTerm` extraction algo
2023-07-03 14:07:55 +02:00
1 changed files with 65 additions and 3 deletions

View File

@ -6,6 +6,8 @@ module botty.modules.urbandict;
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
@ -48,17 +50,77 @@ 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(" ")..$]);
try
{
// Perform lookup and parsing
JSONValue[] definitions = doThing(searchTerm);
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);
}
// If no definitions are found
else
{
getBot().channelMessage("No definitions for '"~searchTerm~"'", 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);
}
}
else
{
// Do nothing
}
}
private static JSONValue[] 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;
import std.stdio;
// writeln("UB result: ", data);
string data = cast(string)get(ubBase~term);
JSONValue[] json = parseJSON(data)["list"].array();
return json;
}
}