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 5783K of libraries. Click here for Pure Java version (9095L/47K).

1  
!7
2  
3  
cmodule Giraffe > DynDiscordHopper {
4  
  switchable S myName = "Giraffe";
5  
  switchable S sliceID = "wgeuwbcepeoxfwma"; // "Height bot" slice - https://agi.blue/?slice=cxbfnllthxfsuwxw
6  
  
7  
  WaitForAnswerState state;
8  
  
9  
  class WaitForAnswerState {
10  
    long waitingSince = now(), waitUntil = nowPlusSeconds(60.0);
11  
    long channelID;
12  
    S user;
13  
    
14  
    void check {
15  
      if (now() >= waitUntil) try {
16  
        postInChannel(channelID, "Hey " + user + ", can't you answer?");
17  
      } finally {
18  
        setField(state := null);
19  
      }
20  
    }
21  
    
22  
    void processInput(long userID, S input) {
23  
      if (discord_parseUserMention(user) != userID) ret;
24  
      if (processStatement(userID, preferredChannelID, input))
25  
        setField(state := null);
26  
    }
27  
  }
28  
29  
  bool processStatement(long userID, long channelID, S input) {
30  
    S parsedHeight = ai_parsePersonHeightStatement(input);
31  
    if (parsedHeight == null) false;
32  
    postInChannel(channelID, "Aha! " + discordAt(userID) + " is " + parsedHeight + " tall");
33  
    agiBlue_postInSlice(sliceID, 
34  
      "Discord user " + discordAt(userID), "'s height is", parsedHeight);
35  
    true;
36  
  }
37  
38  
  void askUserTheQuestion(long userID, long channelID) {
39  
    S knownHeight = agiBlue_lookupInSlice(sliceID, 
40  
      "Discord user " + discordAt(userID), "'s height is");
41  
    if (nempty(knownHeight))
42  
      ret with postInChannel(channelID, "I know that your height is " + knownHeight + ", " + discordAt(userID));
43  
44  
    postInChannel(channelID,
45  
      discordAt(userID) + ", " + questionToAsk());
46  
    if (state == null)
47  
      setField(state := setAll(new WaitForAnswerState, +channelID, user := discordAt(userID)));
48  
  }
49  
  
50  
  start {
51  
    agiBlue_useProductiveVersion();
52  
    dm_vmBus_onMessage_q('discordGuildJoin, voidfunc(Map map) {
53  
      ret unless map.get('module) == module();
54  
      print("Got join");
55  
      askUserTheQuestion(getLong userID(map), preferredChannelID);
56  
    });
57  
    dm_vmBus_onMessage_q('discordGuildLeave, voidfunc(Map map) {
58  
      ret unless map.get('module) == module();
59  
      if (eq(discordAt(getLong userID(map)), getString user(state)))
60  
        state = null;
61  
    });    
62  
    doEvery(1.0, r { if (state != null) state.check(); });
63  
  }
64  
  
65  
  @Override S answer(S input, Map map) {
66  
    ret mapEachLine_tlft_nempties(input, s -> {
67  
      new Matches m;
68  
      
69  
      // preprocess
70  
      if (discordBotID != 0)
71  
        s = replace(s, discordAt(discordBotID), " " + myName + " ");
72  
      s = dropPunctuation3(s);
73  
      s = trim(simpleSpaces_noTok(s));
74  
      print("simplified >> " + quote(s));
75  
      
76  
      if "what's your name" ret myName;
77  
      
78  
      if (state != null)
79  
        state.processInput(getLong userID(map), input);
80  
81  
      S questionToAsk = questionToAsk();
82  
      
83  
      if (eqic_underCollapse(s, "hey " + myName))
84  
 {
85  
        print("asking user");
86  
        askUserTheQuestion(getLong userID(map), getLong channelID(map));
87  
      }
88  
89  
      if (cic_underCollapse(s, myName) && cicOneOf_underCollapse(s, "database", "slice", "agi.blue"))
90  
        ret "My database is here: " + agiBlue_linkToSlice(sliceID);
91  
        
92  
      if (swic_trim(s, myName + " example for: ", m))
93  
        ret findExample(m.rest());
94  
95  
      if (cic(s, myName) && processStatement(getLong userID(map), getLong channelID(map), s)) null;
96  
        
97  
      if (swic_trim(s, myName + " ", m)) {
98  
        S question = m.rest();
99  
        S answerPattern = randomAnswerPattern(m.rest());
100  
        if (nempty(answerPattern))
101  
          ret fillPattern(answerPattern);
102  
      }
103  
104  
      // react to user input matching an answer pattern
105  
      
106  
      /*for (S pattern : allAnswerPatterns(questionToAsk)) {
107  
        S regexp = ellipsisToGroupedDotPlusRegexp(angleBracketVarsToEllipsis(pattern));
108  
        print("Have regexp: " + regexp);
109  
        LS groups = regexpFirstGroupsIC(regexp, s);
110  
        if (nempty(groups))
111  
          ret "Aha! " + first(groups) + " " + discordAt(getLong userID(map));
112  
      }*/
113  
114  
      null;
115  
    });
116  
  }
117  
  
118  
  S questionToAsk() {
119  
    ret agiBlue_randomLookupInSlice(sliceID, "question to ask", "is");
120  
  }
121  
  
122  
  // assume s is sanitized
123  
  S findExample(S s) {
124  
    ret agiBlue_randomALookupInSlice(sliceID, "is a", s);
125  
  }
126  
  
127  
  S randomAnswerPattern(S question) {
128  
    ret agiBlue_randomLookupInSlice(sliceID, question, "answer pattern");
129  
  }
130  
131  
  LS allAnswerPatterns(S question) {
132  
    ret agiBlue_multiLookupInSlice(sliceID, question, "answer pattern");
133  
  }
134  
135  
  S fillPattern(S pattern) {
136  
    LS tok = javaTokWithAngleBrackets(pattern);
137  
    for (int idx : indicesOfAngleBracketVars(tok)) {
138  
      S value = findExample(deAngleBracket(tok.get(idx)));
139  
      if (nempty(value))
140  
        tok.set(idx, value);
141  
    }
142  
    ret join(tok);
143  
  }
144  
  
145  
}

Author comment

Began life as a copy of #1023494

download  show line numbers  debug dex   

Travelled to 2 computer(s): mqqgnosmbjvj, tvejysmllsmz

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: 114 / 828
Version history: 48 change(s)
Referenced in: [show references]