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

63
LINES

< > BotCompany Repo | #1025497 // DynListBot [discord bot that manages a list with custom cmds]

JavaX fragment (include) [tags: use-pretranspiled]

Uses 4998K of libraries. Click here for Pure Java version (14933L/110K).

1  
!7
2  
3  
abstract sclass DynListBot extends DynTalkBot2<.ByServer> {
4  
  /* example program:
5  
  
6  
      The AI should ... = +
7  
      The AI doesn't have to ... = -
8  
      What should the AI do = listWithPattern("The AI should ...")
9  
      The AI shouldn't do anything = clear after confirm
10  
      
11  
  */
12  
  S program;
13  
  S nothingText = "nothing"; // for listing an empty database
14  
  
15  
  void init {
16  
    super.init();
17  
    makeByServer = () -> new ByServer;
18  
    useAGIBlueForDropPunctuation = false;
19  
    preprocessAtSelfToMyName = false;
20  
  }
21  
  
22  
  static noeq record Entry(S text) extends HasGlobalID {}
23  
  transient IF0<Entry> makeEntry = () -> new Entry();
24  
        
25  
  class ByServer extends DynTalkBot2.ByServer {
26  
    Map<S, Entry> db = ciMap();
27  
    
28  
    S add(S s) {
29  
      if (!db.containsKey(s)) { db.put(s, setAll(makeEntry!, text := s)); change(); ret "OK, stored"; }
30  
      ret "I know";
31  
    }
32  
    S remove(S s) { ret db.remove(s) != null ? with(r change, "OK, then not") : "I know"; }
33  
    S listWithPattern(S pat) { ret or2(lines(mapValuesToList(db, x -> discord_prependGlobalID(x) + format_ellipsis(pat, x.text))), nothingText); }
34  
    S clearAfterConfirm() { ret "Function not implemented yet :)"; } // TODO
35  
  
36  
    sync S processSimplifiedLine(S s, O... _) {
37  
      try answer super.processSimplifiedLine(s, _);
38  
      new Matches m;
39  
      
40  
      for (S lhs, rhs : unpair extractLineEquations(program)) {
41  
        if (matchX2(lhs, s, m)) {
42  
          if (eq(rhs, "+")) ret add($1);
43  
          if (eq(rhs, "-")) ret remove($1);
44  
          if (eq(rhs, "clear after confirm")) ret clearAfterConfirm();
45  
          Pair<S, LS> call = parseFunctionCall(rhs);
46  
          if (call != null && eq(call.a, "listWithPattern"))
47  
            ret listWithPattern(unquote(first(call.b)));
48  
        }
49  
      }
50  
      
51  
      s = dropMyPrefixOrNull(s);
52  
    
53  
      if "download" {
54  
        optPar Guild guild;
55  
        optPar MessageChannel channel;
56  
        ret serveGuildBackup(channel, guild,
57  
          structure_nullingInstancesOfClass(_getClass(module()), ByServer.this));
58  
      }
59  
60  
      null;
61  
    }
62  
  }
63  
}

Author comment

Began life as a copy of #1025493

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: #1025497
Snippet name: DynListBot [discord bot that manages a list with custom cmds]
Eternal ID of this version: #1025497/18
Text MD5: 5a42cd959d9b5d4dfca73e26b35c7378
Transpilation MD5: 8fe2326e5148417b442b85bef72d11ec
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-05 14:54:49
Source code size: 2124 bytes / 63 lines
Pitched / IR pitched: No / No
Views / Downloads: 345 / 773
Version history: 17 change(s)
Referenced in: [show references]