mirror of https://github.com/bonobonet/botty
Compare commits
2 Commits
21329400ec
...
08ce59de5a
Author | SHA1 | Date |
---|---|---|
Tristan B. Velloza Kildaire | 08ce59de5a | |
Tristan B. Velloza Kildaire | 6b03585b53 |
|
@ -8,6 +8,7 @@ import botty.bot : Bot;
|
||||||
import birchwood.protocol.messages : Message;
|
import birchwood.protocol.messages : Message;
|
||||||
import std.net.curl : CurlException;
|
import std.net.curl : CurlException;
|
||||||
import std.json : JSONValue, JSONException, parseJSON;
|
import std.json : JSONValue, JSONException, parseJSON;
|
||||||
|
import birchwood;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Urban dictionary command
|
* Urban dictionary command
|
||||||
|
@ -45,6 +46,7 @@ public final class UrbanDict : Mod
|
||||||
|
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
import std.string : split;
|
import std.string : split;
|
||||||
|
import std.conv : to, ConvException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Split the ` .ub dictionarydef`
|
* Split the ` .ub dictionarydef`
|
||||||
|
@ -54,70 +56,103 @@ public final class UrbanDict : Mod
|
||||||
string[] splits = split(messageBody, " ");
|
string[] splits = split(messageBody, " ");
|
||||||
writeln("splits", splits);
|
writeln("splits", splits);
|
||||||
|
|
||||||
if(splits.length >= 2)
|
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
writeln("Are we ongod yet? ",messageBody.indexOf(" "));
|
|
||||||
string searchTerm = strip(messageBody[messageBody.indexOf(" ")..$]);
|
|
||||||
writeln("search term: '"~searchTerm~"'");
|
if(splits.length >= 2)
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
// Perform lookup and parsing
|
|
||||||
JSONValue[] definitions = doThing(searchTerm);
|
|
||||||
|
|
||||||
if(definitions.length > 0)
|
/**
|
||||||
|
* Checks for `.ub:2`
|
||||||
|
*/
|
||||||
|
ulong count = 0;
|
||||||
|
long countIdx = splits[0].indexOf(":");
|
||||||
|
if(countIdx > -1)
|
||||||
{
|
{
|
||||||
// TODO: Send result below
|
string countStr = splits[0].split(":")[1];
|
||||||
// getBot().channelMessage(translatedText, channel);
|
try
|
||||||
|
{
|
||||||
JSONValue firstDef = definitions[0];
|
count = to!(ulong)(countStr);
|
||||||
|
}
|
||||||
string definition = firstDef["definition"].str();
|
catch(ConvException e)
|
||||||
string example = firstDef["example"].str();
|
{
|
||||||
string author = firstDef["author"].str();
|
getBot().channelMessage("Invalid index '"~countStr~"'", channel);
|
||||||
string permalink = firstDef["permalink"].str();
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
getBot().channelMessage(bold("Permalink: ")~permalink, channel);
|
|
||||||
}
|
}
|
||||||
// If no definitions are found
|
|
||||||
else
|
|
||||||
|
writeln("Are we ongod yet? ",messageBody.indexOf(" "));
|
||||||
|
string searchTerm = strip(messageBody[messageBody.indexOf(" ")..$]);
|
||||||
|
writeln("search term: '"~searchTerm~"'");
|
||||||
|
|
||||||
|
try
|
||||||
{
|
{
|
||||||
getBot().channelMessage("No definitions for '"~searchTerm~"'", channel);
|
// Perform lookup and parsing
|
||||||
}
|
JSONValue[] definitions = doThing(searchTerm);
|
||||||
|
writeln("Definitions count: ", definitions.length);
|
||||||
|
writeln("Selecting definition: ", count);
|
||||||
|
|
||||||
|
if(definitions.length > 0)
|
||||||
|
{
|
||||||
|
// TODO: Send result below
|
||||||
|
// getBot().channelMessage(translatedText, channel);
|
||||||
|
|
||||||
|
JSONValue firstDef = definitions[count];
|
||||||
|
|
||||||
|
string definition = firstDef["definition"].str();
|
||||||
|
string example = firstDef["example"].str();
|
||||||
|
string author = firstDef["author"].str();
|
||||||
|
string permalink = firstDef["permalink"].str();
|
||||||
|
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// On network error
|
else
|
||||||
catch(CurlException e)
|
|
||||||
{
|
{
|
||||||
getBot().channelMessage("There was a network error when looking up on urban dictionary", channel);
|
// Do nothing
|
||||||
}
|
writeln("IMM A W ");
|
||||||
// On parsing error
|
writeln("IMM A W ");
|
||||||
catch(JSONException e)
|
writeln("IMM A W ");
|
||||||
{
|
writeln("IMM A W ");
|
||||||
getBot().channelMessage("There was a parsing error when looking up on urban dictionary", channel);
|
writeln("IMM A W ");
|
||||||
|
writeln("IMM A W ");
|
||||||
|
writeln("IMM A W ");
|
||||||
|
writeln("IMM A W ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
catch(BirchwoodException e)
|
||||||
{
|
{
|
||||||
// Do nothing
|
writeln("Birchwood error: ", e);
|
||||||
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 ");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue