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

88
LINES

< > BotCompany Repo | #1002404 // Store messages bot (optimized)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (2397L/15K/51K).

!752
static new MultiMap<S, Line> map;
static class Line { 
  S text;
  S timestamp;
  
  Line(){
  
  }
  *(S *text, S *timestamp) {}
}

static boolean saveLater;

p{
  load("#1002356", "map");
  
  thread "msgs saver" {
    while (licensed()) {
      saveIt();
      sleepSeconds(60);
    }
  }
  
  makeBot("Store messages bot");
}

synchronized answer{
  S username = "";
  if(getUserName() == null){
    username = "anonymous";
  }else{
    username = getUserName();
  }
  if(matchStart("list all messages from *", s, m)){
    S user = m.unq(0);
    boolean isMe = isMe(user);
    if (isMe) user = getUserName();
    ret structure(map.get(user));
  }
  if(match("list all users who said something", s)){
    ret structure(map.keySet());
  }
  if(matchStart("did * say something about *", s, m)){
    S user = m.unq(0);
    boolean isMe = isMe(user);
    if (isMe) user = getUserName();
    if(map.containsKey(user)){
      List<Line> list = map.get(user);
      int times = 0;
      L<S> lolist = new L;
      for (int i = l(list)-1; i >= 0; i--) {
        if (indexOfIgnoreCase(list.get(i).text, m.unq(1)) >= 0){
          times = times + 1;
          lolist.add(list.get(i).text);
        }
      }
      if (times == 0){
        ret (isMe ? "You" : user) + " did not say anything about that.";
      }else{
        ret lolist + "\n" + times +" results found";
      }
    }else{
      ret (isMe ? "You" : user) + " did not say anything yet.";
    }
  } 
  Line line = new Line(s,getSlackTS());
  map.put(username, line);
  saveLater = true;
}

synchronized static void saveIt() {
  if (saveLater) {
    time {
      print(programID() + ": Saving msgs.");
      save("#1002356", "map");
    }
    saveLater = false;
  }
}

static void cleanUp() {
  saveIt();
}

static boolean isMe(S user) {
  ret eqic(user, "I") || eqic(user, "me");
}

Author comment

Began life as a copy of #1002356

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002404
Snippet name: Store messages bot (optimized)
Eternal ID of this version: #1002404/1
Text MD5: 4a52009c2f9e3268f5eac7fe67f3c8d1
Transpilation MD5: 18743e12d97b200302072e3e11b9af2d
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-01-22 19:50:25
Source code size: 1930 bytes / 88 lines
Pitched / IR pitched: No / No
Views / Downloads: 754 / 1307
Referenced in: [show references]