Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

145
LINES

< > BotCompany Repo | #1024415 // Giraffe (bot based on an agi.blue slice)

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 5909K of libraries. Click here for Pure Java version (9095L/47K).

!7

cmodule Giraffe > DynDiscordHopper {
  switchable S myName = "Giraffe";
  switchable S sliceID = "wgeuwbcepeoxfwma"; // "Height bot" slice - https://agi.blue/?slice=cxbfnllthxfsuwxw
  
  WaitForAnswerState state;
  
  class WaitForAnswerState {
    long waitingSince = now(), waitUntil = nowPlusSeconds(60.0);
    long channelID;
    S user;
    
    void check {
      if (now() >= waitUntil) try {
        postInChannel(channelID, "Hey " + user + ", can't you answer?");
      } finally {
        setField(state := null);
      }
    }
    
    void processInput(long userID, S input) {
      if (discord_parseUserMention(user) != userID) ret;
      if (processStatement(userID, preferredChannelID, input))
        setField(state := null);
    }
  }

  bool processStatement(long userID, long channelID, S input) {
    S parsedHeight = ai_parsePersonHeightStatement(input);
    if (parsedHeight == null) false;
    postInChannel(channelID, "Aha! " + discordAt(userID) + " is " + parsedHeight + " tall");
    agiBlue_postInSlice(sliceID, 
      "Discord user " + discordAt(userID), "'s height is", parsedHeight);
    true;
  }

  void askUserTheQuestion(long userID, long channelID) {
    S knownHeight = agiBlue_lookupInSlice(sliceID, 
      "Discord user " + discordAt(userID), "'s height is");
    if (nempty(knownHeight))
      ret with postInChannel(channelID, "I know that your height is " + knownHeight + ", " + discordAt(userID));

    postInChannel(channelID,
      discordAt(userID) + ", " + questionToAsk());
    if (state == null)
      setField(state := setAll(new WaitForAnswerState, +channelID, user := discordAt(userID)));
  }
  
  start {
    agiBlue_useProductiveVersion();
    dm_vmBus_onMessage_q('discordGuildJoin, voidfunc(Map map) {
      ret unless map.get('module) == module();
      print("Got join");
      askUserTheQuestion(getLong userID(map), preferredChannelID);
    });
    dm_vmBus_onMessage_q('discordGuildLeave, voidfunc(Map map) {
      ret unless map.get('module) == module();
      if (eq(discordAt(getLong userID(map)), getString user(state)))
        state = null;
    });    
    doEvery(1.0, r { if (state != null) state.check(); });
  }
  
  @Override S answer(S input, Map map) {
    ret mapEachLine_tlft_nempties(input, s -> {
      new Matches m;
      
      // preprocess
      if (discordBotID != 0)
        s = replace(s, discordAt(discordBotID), " " + myName + " ");
      s = dropPunctuation3(s);
      s = trim(simpleSpaces_noTok(s));
      print("simplified >> " + quote(s));
      
      if "what's your name" ret myName;
      
      if (state != null)
        state.processInput(getLong userID(map), input);

      S questionToAsk = questionToAsk();
      
      if (eqic_underCollapse(s, "hey " + myName))
 {
        print("asking user");
        askUserTheQuestion(getLong userID(map), getLong channelID(map));
      }

      if (cic_underCollapse(s, myName) && cicOneOf_underCollapse(s, "database", "slice", "agi.blue"))
        ret "My database is here: " + agiBlue_linkToSlice(sliceID);
        
      if (swic_trim(s, myName + " example for: ", m))
        ret findExample(m.rest());

      if (cic(s, myName) && processStatement(getLong userID(map), getLong channelID(map), s)) null;
        
      if (swic_trim(s, myName + " ", m)) {
        S question = m.rest();
        S answerPattern = randomAnswerPattern(m.rest());
        if (nempty(answerPattern))
          ret fillPattern(answerPattern);
      }

      // react to user input matching an answer pattern
      
      /*for (S pattern : allAnswerPatterns(questionToAsk)) {
        S regexp = ellipsisToGroupedDotPlusRegexp(angleBracketVarsToEllipsis(pattern));
        print("Have regexp: " + regexp);
        LS groups = regexpFirstGroupsIC(regexp, s);
        if (nempty(groups))
          ret "Aha! " + first(groups) + " " + discordAt(getLong userID(map));
      }*/

      null;
    });
  }
  
  S questionToAsk() {
    ret agiBlue_randomLookupInSlice(sliceID, "question to ask", "is");
  }
  
  // assume s is sanitized
  S findExample(S s) {
    ret agiBlue_randomALookupInSlice(sliceID, "is a", s);
  }
  
  S randomAnswerPattern(S question) {
    ret agiBlue_randomLookupInSlice(sliceID, question, "answer pattern");
  }

  LS allAnswerPatterns(S question) {
    ret agiBlue_multiLookupInSlice(sliceID, question, "answer pattern");
  }

  S fillPattern(S pattern) {
    LS tok = javaTokWithAngleBrackets(pattern);
    for (int idx : indicesOfAngleBracketVars(tok)) {
      S value = findExample(deAngleBracket(tok.get(idx)));
      if (nempty(value))
        tok.set(idx, value);
    }
    ret join(tok);
  }
  
}

Author comment

Began life as a copy of #1023494

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1024415
Snippet name: Giraffe (bot based on an agi.blue slice)
Eternal ID of this version: #1024415/49
Text MD5: 5df374105b1054f6bec60e5e7f7a4432
Transpilation MD5: 96abf173222f5a0e0da066f8ac142736
Author: stefan
Category: javax / discord / a.i.
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-23 12:13:34
Source code size: 4796 bytes / 145 lines
Pitched / IR pitched: No / No
Views / Downloads: 598 / 1342
Version history: 48 change(s)
Referenced in: #1024502 - Giraffe backup