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

77
LINES

< > BotCompany Repo | #1025905 // Asker Bot [dev.]

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

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

!7

concept Msg {
  long msgID, channelID, userID;
  long date, edited; // epochSeconds
  S text;
  
  S text() { ret text; }
}

concept HasMsg {
  new Ref<Msg> msg;
  
  S text() { ret msg->text; }
}  

concept Question > HasMsg {
  Answer acceptedAnswer;
}

concept Answer > HasMsg {
  Question question;
}

standardBot1 Asker {
  init {
    dbIndexing(Msg, 'msgID, Msg, 'channelID);
    thread {
      temp enter();
      for (Question q)
        print(q.msg->text + " => " + mapMethod text(linesAfterQuestion(q)));
    }
  }
  
  allServers {
    !include #1025906 // DiscordScanner
    
    L<Msg> linesAfterQuestion(Question q) {
      long channelID = q.msg->channelID;
      if (channelID == 0) null;
      L<Msg> l = conceptsWhere(Msg, +channelID);
      l = takeFirst(10, sortByField date(filter(l, m -> m.date > q.msg->date)));
      ret l;
    }
    
    L<Question> openQuestionsInChannel(long channelID) {
      ret sortedByField date(filter(conceptsWhere(Question, acceptedAnswer := null),
        q -> q.msg->channelID == channelID));
    }
  }
  
  processSimplified {
    optPar Message msg;
    optPar long channelID;
    optPar long userID;
    Msg storedMsg = pairA(storeMsg(msg));
    
    if null (s = dropMyPrefixOrNull(s)) null;
    
    if "ask me something" {
      S q = "Is " + random(100) + " bigger than " + random(100) + "?";
      postInChannel(channelID, discordAt(userID) + " " + q, mm -> {
        Msg stored = pairA(storeMsg(mm));
        printStruct("New question: " + cnew Question(msg := stored));
      });
      null;
    }
    
    L<Question> qs = openQuestionsInChannel(channelID);
    Question q = last(qs);
    if (q != null) {
      Answer a = cnew Answer(msg := storedMsg);
      cset(q, acceptedAnswer := a);
      ret "Assuming " + quote(s) + " is the answer to " + quote(q.text());
    }
  }
}

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: #1025905
Snippet name: Asker Bot [dev.]
Eternal ID of this version: #1025905/18
Text MD5: 32a95923943bb14760c2fcc4ef2adaf8
Transpilation MD5: 180277dda3fbc1f641fd5f43b83279fd
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-29 13:36:43
Source code size: 1921 bytes / 77 lines
Pitched / IR pitched: No / No
Views / Downloads: 199 / 5259
Version history: 17 change(s)
Referenced in: [show references]