Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import
- std.json,
- std.conv,
- std.array,
- std.stdio,
- std.algorithm,
- std.string,
- std.format,
- std.process,
- core.time;
- import
- dscord.core,
- dscord.util.process,
- dscord.bot;
- import
- vibe.vibe;
- static import
- std.experimental.logger;
- class NettoPlugin : Plugin {
- this() {
- super();
- }
- @Command("def")
- void onDefCommand(CommandEvent event) {
- string response = lookupWord(event.args[0]);
- event.msg.reply("```md\n" ~ response ~ "\n```");
- }
- @Command("test")
- void onTestCommand(CommandEvent event) {
- writeln("COMMAND");
- event.msg.reply("HI!");
- }
- string joinJSONStringArr(JSONValue[] arr, string connector) {
- string result = "";
- for(int v = 0; v < arr.length; v++) {
- (v == arr.length - 1) ? result ~= arr[v].str : result ~= arr[v].str ~ connector;
- }
- return result;
- }
- string lookupWord(string search) {
- JSONValue j;
- string response = "";
- requestHTTP("http://jisho.org/api/v1/search/words?keyword=" ~ search,
- (scope req) {
- },
- (scope res) {
- response = res.bodyReader.readAllUTF8();
- j = parseJSON(response);
- }
- );
- JSONValue data = j["data"];
- string result = "";
- for(int v = 0; v < data.array.length; v++) {
- result ~= "[" ~ to!string(v + 1) ~ ".] ";
- string[] words;
- result ~= "[";
- for(int vj = 0; vj < data[v]["japanese"].array.length; vj++) {
- string word = data[v]["japanese"][vj]["word"].str;
- string reading = "";
- if(const(JSONValue)* readingVal = "reading" in data[v]["japanese"][vj]) {
- reading = readingVal.str;
- words ~= "<" ~ word ~ " (" ~ reading ~ ")>";
- } else {
- words ~= "<" ~ word ~ ">";
- }
- }
- result ~= words.join(", ") ~ "]\n";
- for(int vs = 0; vs < data[v]["senses"].array.length; vs++) {
- string types = joinJSONStringArr(data[v]["senses"][vs]["parts_of_speech"].array, ", ");
- string definitions = joinJSONStringArr(data[v]["senses"][vs]["english_definitions"].array, ", ");
- if(types == "") result ~= " <" ~ to!string(vs + 1) ~ ". " ~ definitions ~ ">\n";
- else result ~= " [" ~ to!string(vs + 1) ~ ".] [<" ~ types ~ ">] <" ~ definitions ~ ">\n";
- }
- }
- return result;
- }
- }
- void main(string[] args) {
- if (args.length <= 1) {
- writefln("Usage: %s <token>", args[0]);
- return;
- }
- BotConfig config;
- config.token = args[1];
- config.cmdPrefix = "~!";
- Bot bot = new Bot(config, std.experimental.logger.LogLevel.trace);
- bot.loadPlugin(new NettoPlugin);
- bot.run();
- runEventLoop();
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement