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

141
LINES

< > BotCompany Repo | #1024131 // GBot v3 (Discord Bot that googles, server-aware version, LIVE)

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

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

!7

// cmodule fails because of loadPageWithUserAgent
module GBot > DynDiscordHopper {
  new Map<Long, ByServer> dataByServer;
  int guildCount;
  long searches;
  
  class ByServer {
    bool isGuild;
    S myName = "GBot";
    int defaultResults = 1;
    Set<S> syntaxes = synchroSet();
    
    S answer(S s, Map map) {
      new Matches m;
      
      try answer answerToHello(s, myName);
      
      if (discordBotID != 0)
        s = replacePrefix("<@" + discordBotID + ">", myName + " ", s);
      
      s = simpleSpaces_noTok(s);
      s = regexpReplace_direct(s, "\\s+:", ":");
      
      if (eqic(s, myName + " guild count")) ret renderGuildCount();
      if (eqic(s, myName + " searches")) ret str(searches);
      if (eqic(s, myName + " stats")) ret n2(guildCount, "server") + ", " + nSearches(searches);
      
      if (eqicOneOf(replaceIC(words2_spaces(s), " show ", " "), myName + " Help", myName + " info", myName + " commands"))
        ret loadSnippet(#1024125);
      
      if (swic_trim(s, myName + ":", m))
        ret doIt(m.rest(), map);
        
      if (swic_trim(s, myName + " with description:", m))
        ret doIt(m.rest(), map, withDescription := true);
        
      if (swic_trim(s, myName + " default results:", m)) {
        setField(defaultResults := parseInt(m.rest());
        ret "OK, returning " + nResults(defaultResults) + " from now on";
      }
        
      if (eqic(s, myName + " default results"))
        ret "I'm returning " + nResults(defaultResults) + " by default";
      
      if (eqic(s, myName + " syntaxes"))
        ret backtickQuote(lines_rtrim(elementPlusList(myName + ": ...", syntaxes)));
        
      if (swic_trim_any(s, m, myName + " new syntax:", myName + " add syntax:")) {
        S syntax = massageSyntax(m.rest());
        if (!endsWithEllipsis(syntax))
          ret "Shouldn't this end with \"...\"? >> " + syntax;
        if (!syntaxes.add(syntax)) ret "I already know that syntax!";
        change();
        ret "Syntax added! You can try: " + backtickQuote(replaceSuffix("...", randomThingToGoogle(), syntax));
      }
      
      if (swic_trim(s, myName + " forget syntax:", m)) {
        S syntax = massageSyntax(m.rest());
        if (!syntaxes.remove(syntax)) ret "Syntax not found";
        change();
        ret "Syntax removed!";
      }
      
      if (swicOneOf_trim(s, m, myName + " source", myName + " code"))
        ret snippetURL(programID());
      
      if (ellipsisToDotPlusRegexpIC_matchAny(syntaxes, s, m))
        ret doIt(m.rest(), map);
        
      null;
    }
    
    bool setField(S name, O value) {
      if (set_trueIfChanged(this, name, value)) false;
      ret true with _change();
    }
    
    S doIt(S query, Map map, O... _) {
      LS l = regexpICFullMatch_groups("(\\d+) results? for[ :](.+)", query);
      int results = defaultResults;
      if (l != null) {
        results = parseInt(first(l));
        query = unquote(trim(last(l)));
      }
      bool safeSearch = !discord_isNSFWChannel_gen(map.get('channel));
      module().setField(searches := searches+1);
      updateModuleName();
      ret discord_google(query, paramsPlus(_, +results, +safeSearch));
    }
  }
  
  @Override S answer(S input, Map map) {
    ret mapEachLine_tlft_nempties(input, s -> {
      // config by guild or user
      long guildID = toLong(map.get('guildID));
      long id = guildID;
      print("Guild ID: " + guildID);
      if (id == 0) {
        id = toLong(map.get('userID));
        print("User ID: " + id);
      }
      
      ByServer data, bool isNew = unpair syncGetOrCreate2(dataByServer, id, func -> ByServer { new ByServer });
      data.isGuild = id == guildID;
      if (isNew) {
        if (data.isGuild) ++guildCount;
        change();
      }
      ret data.answer(s, map);
    });
  }
  
  S randomThingToGoogle() {
    ret "why are penguins black and white?";
  }
  
  S massageSyntax(S syntax) {
    syntax = unquote(syntax);
    ret trim(dropSuffix("...", syntax)) + " ...";
  }
  
  start {
    if (guildCount == 0)
      setField(guildCount := countValuesWhere(dataByServer, isGuild := true));
    updateModuleName();
  }
  
  void updateModuleName() pcall {
    dm_setModuleName("GBot - " + n2(guildCount, "guild") + ", " + n2(searches, "search", "searches"));
  }
  
  S renderGuildCount() {
    ret "Total guilds joined: " + guildCount + ". Live guilds: " + liveGuildCount();
  }
  
  int liveGuildCount() {
    ret l(discord.getGuilds());
  }
}

Author comment

Began life as a copy of #1024114

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: #1024131
Snippet name: GBot v3 (Discord Bot that googles, server-aware version, LIVE)
Eternal ID of this version: #1024131/28
Text MD5: 8e6f72d228a56266f28f2d9013ba4b8e
Transpilation MD5: fd5204bd7756f6f60d3251232440c2d3
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-10-23 22:32:59
Source code size: 4631 bytes / 141 lines
Pitched / IR pitched: No / No
Views / Downloads: 301 / 1512
Version history: 27 change(s)
Referenced in: #1025127 - DynServerAwareDiscordBot