Advertisement
Guest User

actual discord bot test

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