Merge branch 'master' of github.com:bonobonet/botty

This commit is contained in:
Tristan B. Velloza Kildaire 2023-07-03 16:47:26 +02:00
commit fe7454dc01
4 changed files with 151 additions and 3 deletions

View File

@ -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 <Text>`
3. Ping
* Let's you ping a endpoint with `.ping <addr/domain>`
4. Urban dictionary
* Let's you ookup definitions on Urban dictionary
* Using `.ub Marshmellow`
## Credits

View File

@ -33,10 +33,10 @@ 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, 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);

View File

@ -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)
];
}

View File

@ -0,0 +1,143 @@
/**
* Urban Dictionary module
*/
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
*/
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]`
*/
messageBody = strip(messageBody);
string[] splits = split(messageBody, " ");
writeln("splits", splits);
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);
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;
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);
}
}
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 ");
}
}
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;
string data = cast(string)get(ubBase~term);
writeln("UB result: ", data);
JSONValue[] json = parseJSON(data)["list"].array();
return json;
}
}