Compare commits

..

No commits in common. "08ce59de5a098f4a2709cd97b585f115bab6d0d0" and "21329400ec7e6bd384b054cae65610a985607a4c" have entirely different histories.

1 changed files with 53 additions and 88 deletions

View File

@ -8,7 +8,6 @@ import botty.bot : Bot;
import birchwood.protocol.messages : Message;
import std.net.curl : CurlException;
import std.json : JSONValue, JSONException, parseJSON;
import birchwood;
/**
* Urban dictionary command
@ -46,7 +45,6 @@ public final class UrbanDict : Mod
import std.stdio;
import std.string : split;
import std.conv : to, ConvException;
/**
* Split the ` .ub dictionarydef`
@ -56,103 +54,70 @@ public final class UrbanDict : Mod
string[] splits = split(messageBody, " ");
writeln("splits", splits);
try
if(splits.length >= 2)
{
if(splits.length >= 2)
writeln("Are we ongod yet? ",messageBody.indexOf(" "));
string searchTerm = strip(messageBody[messageBody.indexOf(" ")..$]);
writeln("search term: '"~searchTerm~"'");
try
{
// Perform lookup and parsing
JSONValue[] definitions = doThing(searchTerm);
/**
* Checks for `.ub:2`
*/
ulong count = 0;
long countIdx = splits[0].indexOf(":");
if(countIdx > -1)
if(definitions.length > 0)
{
string countStr = splits[0].split(":")[1];
try
{
count = to!(ulong)(countStr);
}
catch(ConvException e)
{
getBot().channelMessage("Invalid index '"~countStr~"'", channel);
}
// 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;
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);
}
writeln("Are we ongod yet? ",messageBody.indexOf(" "));
string searchTerm = strip(messageBody[messageBody.indexOf(" ")..$]);
writeln("search term: '"~searchTerm~"'");
try
// If no definitions are found
else
{
// 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);
}
getBot().channelMessage("No definitions for '"~searchTerm~"'", channel);
}
}
else
// On network error
catch(CurlException e)
{
// 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 ");
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);
}
}
catch(BirchwoodException e)
else
{
writeln("Birchwood error: ", e);
// 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 ");
}
}