mirror of https://github.com/bonobonet/botty
Compare commits
6 Commits
ab83859175
...
d2423d986a
Author | SHA1 | Date |
---|---|---|
Tristan B. Velloza Kildaire | d2423d986a | |
Tristan B. Velloza Kildaire | f8a2ab8265 | |
Tristan B. Velloza Kildaire | fd6efacb1c | |
Tristan B. Velloza Kildaire | d8096caba2 | |
Tristan B. Velloza Kildaire | e023dc8018 | |
Tristan B. Velloza Kildaire | b7dbb80610 |
|
@ -6,6 +6,8 @@ module botty.modules.urbandict;
|
||||||
import botty.mod;
|
import botty.mod;
|
||||||
import botty.bot : Bot;
|
import botty.bot : Bot;
|
||||||
import birchwood.protocol.messages : Message;
|
import birchwood.protocol.messages : Message;
|
||||||
|
import std.net.curl : CurlException;
|
||||||
|
import std.json : JSONValue, JSONException, parseJSON;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Urban dictionary command
|
* Urban dictionary command
|
||||||
|
@ -48,17 +50,77 @@ public final class UrbanDict : Mod
|
||||||
* Split the ` .ub dictionarydef`
|
* Split the ` .ub dictionarydef`
|
||||||
* into two parts `[.ub, dictionarydef]`
|
* into two parts `[.ub, dictionarydef]`
|
||||||
*/
|
*/
|
||||||
string[] splits = split(strip(messageBody), " ");
|
messageBody = strip(messageBody);
|
||||||
|
string[] splits = split(messageBody, " ");
|
||||||
writeln("splits", splits);
|
writeln("splits", splits);
|
||||||
|
|
||||||
if(splits.length >= 2)
|
if(splits.length >= 2)
|
||||||
{
|
{
|
||||||
string searchTerm = splits[1];
|
string searchTerm = strip(messageBody[messageBody.indexOf(" ")..$]);
|
||||||
getBot().channelMessage("hehe", channel);
|
|
||||||
|
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
|
else
|
||||||
{
|
{
|
||||||
// Do nothing
|
// 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;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue