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

99
LINES

< > BotCompany Repo | #1025128 - DynTalkBot2 - DynServerAwareDiscordBot + input simplification, onUserJoin, name, authorizedUsers

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

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

asclass DynTalkBot2<A extends DynTalkBot2.ByServer> extends DynServerAwareDiscordBot<A> {
  switchable S myName = "Anonymous bot";
  transient bool useAGIBlueForDropPunctuation = true;
  transient bool preprocessAtSelfToMyName = true;
  transient bool dropPunctuation = true;
  L<Long> authorizedUsers = ll(547706854680297473); // stefan
  
  start {
    makeByServer = () -> (A) new ByServer;
    dm_vmBus_onMessage_q('discordGuildJoin, voidfunc(Map map) {
      ret unless map.get('module) == module();
      print("Got join");
      getByServer(getLong guildID(map), true)
        .onUserJoin(getLong userID(map), map);
    });
  }
  
  class ByServer extends DynServerAwareDiscordBot.ByServer {
    DynTalkBot2 m2() { ret DynTalkBot2.this; } // for debugging
    
    // overridable
    void onUserJoin(long userID, O... _) {}
  
    @Override S answer(S input, Map map) {
      try answer super.answer(input, map); // server-aware stuff
      ret processLine(input, map);
    }
  
    S processLine(S s, O... _) {
      s = preprocess(s, _);
      ret processSimplifiedLine(s, _);
    }
    
    S preprocess(S s, O... _) {
      print("Preprocessing: " + s);
      pcall { print("mother: " + DynTalkBot2.this); }
      if (preprocessAtSelfToMyName && discordBotID != 0)
        s = replace(s, atSelf(), " " + myName + " ");
      if (dropPunctuation)
        s = dropPunctuation3_withAGIBlue(useAGIBlueForDropPunctuation, s);
      s = trim(simpleSpaces_noTok(s));
      print("simplified >> " + quote(s));
      ret s;
    }
    
    S myPrefix() {
      ret preprocessAtSelfToMyName ? myName + " " :
        (dropPunctuation ? replace(atSelf(), "@", "") /* @ is killed by preprocessing */ : atSelf()) + " ";
    }
  
    // extend me
    S processSimplifiedLine(S input, O... _) {
      new Matches m;
      
      input = dropPrefixOrNull(myPrefix(), input);
      if (input == null) null;
      
      if (eqicOneOf(input, "support channel", "support server", "support"))
        ret "Get support for me here: " + nextGenBotsDiscordInvite();
        
      if (eqicOneOf(input, "source", "sources", "source code"))
        ret snippetLink(programID());
  
      if (swic_trim(input, "add master ", m)) {
        try answer checkAuth(_);
  
        add(authorizedUsers, parseFirstLong(m.rest()));
        change();
        
        ret "Okidoki. Have " + n2(l(authorizedUsers), "master");
      }
      
      if (eqic(input, "masters"))
        ret empty(authorizedUsers) ? "I have no masters." : "My masters are: " + joinWithComma(map discordAtPlusID(authorizedUsers));
      
      if (swic_trim(input, "delete master ", m)) {
        try answer checkAuth(_);
  
        remove(authorizedUsers, parseFirstLong(m.rest()));
        change();
        
        ret "Okidoki. Have " + n2(l(authorizedUsers), "master");
      }
      null;
    }
    
    bool authed(O... _) {
      ret contains(authorizedUsers, optPar userID(_));
    }
  
    S checkAuth(O... _) {  
      long userID = longPar userID(_);
      bool result = authed(_);
      print("Auth-checking user ID: " + userID + " => " + result);
      if (!result) ret "You are not authorized";
      null;
    }
  }
}

Author comment

Began life as a copy of #1024519

download  show line numbers  debug dex   

Travelled to 2 computer(s): mqqgnosmbjvj, tvejysmllsmz

No comments. add comment

Snippet ID: #1025128
Snippet name: DynTalkBot2 - DynServerAwareDiscordBot + input simplification, onUserJoin, name, authorizedUsers
Eternal ID of this version: #1025128/22
Text MD5: 156021965cbfb15d9b7bc4dce02e06c3
Transpilation MD5: 5479893f90e752901ba27c7fe76eb39a
Author: stefan
Category: javax / agi.blue
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-09 23:14:18
Source code size: 3282 bytes / 99 lines
Pitched / IR pitched: No / No
Views / Downloads: 56 / 152
Version history: 21 change(s)
Referenced in: [show references]