Advertisement
Guest User

dscord bot test

a guest
Sep 25th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.71 KB | None | 0 0
  1. import
  2.     std.json,
  3.     std.conv,
  4.     std.array,
  5.     std.stdio;
  6.  
  7. import
  8.     dscord.core,
  9.     dscord.util.process;
  10.  
  11. import
  12.     vibe.vibe;
  13.  
  14. static import
  15.     vibe.core.log;
  16.  
  17. class NettoPlugin : Plugin {
  18.     this() {
  19.         super();
  20.     }
  21.  
  22.     string joinJSONStringArr(JSONValue[] arr, string connector) {
  23.         string result = "";
  24.  
  25.         for(int v = 0; v < arr.length; v++) {
  26.             (v == arr.length - 1) ? result ~= arr[v].str : result ~= arr[v].str ~ connector;
  27.         }
  28.  
  29.         return result;
  30.     }
  31.  
  32.     string lookupWord(string search) {
  33.         JSONValue j;
  34.         string response = "";
  35.  
  36.         requestHTTP("http://jisho.org/api/v1/search/words?keyword=" ~ search,
  37.             (scope req) {
  38.  
  39.             },
  40.             (scope res) {
  41.                 response = res.bodyReader.readAllUTF8();
  42.                 j = parseJSON(response);
  43.             }
  44.         );
  45.  
  46.         JSONValue data = j["data"];
  47.  
  48.         string result = "";
  49.  
  50.         for(int v = 0; v < data.array.length; v++) {
  51.             result ~= "[" ~ to!string(v + 1) ~ ".] ";
  52.             string[] words;
  53.             result ~= "[";
  54.             for(int vj = 0; vj < data[v]["japanese"].array.length; vj++) {
  55.                 string word = data[v]["japanese"][vj]["word"].str;
  56.                 string reading = "";
  57.  
  58.                 if(const(JSONValue)* readingVal = "reading" in data[v]["japanese"][vj]) {
  59.                     reading = readingVal.str;
  60.                     words ~= "<" ~ word ~ " (" ~ reading ~ ")>";
  61.                 } else {
  62.                     words ~= "<" ~ word ~ ">";
  63.                 }
  64.             }
  65.  
  66.             result ~= words.join(", ") ~ "]\n";
  67.  
  68.             for(int vs = 0; vs < data[v]["senses"].array.length; vs++) {
  69.                 string types = joinJSONStringArr(data[v]["senses"][vs]["parts_of_speech"].array, ", ");
  70.                 string definitions = joinJSONStringArr(data[v]["senses"][vs]["english_definitions"].array, ", ");
  71.  
  72.                 if(types == "") result ~= "      <" ~ to!string(vs + 1) ~ ". " ~ definitions ~ ">\n";
  73.                 else result ~= "      <" ~ to!string(vs + 1) ~ ". (" ~ types ~ ") " ~ definitions ~ ">\n";
  74.             }
  75.         }
  76.  
  77.         return result;
  78.     }
  79.  
  80.     @Command("def")
  81.     void onDefCommand(CommandEvent event) {
  82.         string response = lookupWord(event.args[0]);
  83.  
  84.         event.msg.reply("```md\n" ~ response ~ "\n```");
  85.     }
  86. }
  87.  
  88. void main(string[] args) {
  89.   if (args.length <= 1) {
  90.     writefln("Usage: %s <token>", args[0]);
  91.     return;
  92.   }
  93.  
  94.   BotConfig config;
  95.   config.token = args[1];
  96.   config.cmdPrefix = "~!";
  97.   Bot bot = new Bot(config);
  98.   bot.loadPlugin(new NettoPlugin);
  99.   bot.run();
  100.   runEventLoop();
  101.   return;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement