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).

1  
!7
2  
3  
// cmodule fails because of loadPageWithUserAgent
4  
module GBot > DynDiscordHopper {
5  
  new Map<Long, ByServer> dataByServer;
6  
  int guildCount;
7  
  long searches;
8  
  
9  
  class ByServer {
10  
    bool isGuild;
11  
    S myName = "GBot";
12  
    int defaultResults = 1;
13  
    Set<S> syntaxes = synchroSet();
14  
    
15  
    S answer(S s, Map map) {
16  
      new Matches m;
17  
      
18  
      try answer answerToHello(s, myName);
19  
      
20  
      if (discordBotID != 0)
21  
        s = replacePrefix("<@" + discordBotID + ">", myName + " ", s);
22  
      
23  
      s = simpleSpaces_noTok(s);
24  
      s = regexpReplace_direct(s, "\\s+:", ":");
25  
      
26  
      if (eqic(s, myName + " guild count")) ret renderGuildCount();
27  
      if (eqic(s, myName + " searches")) ret str(searches);
28  
      if (eqic(s, myName + " stats")) ret n2(guildCount, "server") + ", " + nSearches(searches);
29  
      
30  
      if (eqicOneOf(replaceIC(words2_spaces(s), " show ", " "), myName + " Help", myName + " info", myName + " commands"))
31  
        ret loadSnippet(#1024125);
32  
      
33  
      if (swic_trim(s, myName + ":", m))
34  
        ret doIt(m.rest(), map);
35  
        
36  
      if (swic_trim(s, myName + " with description:", m))
37  
        ret doIt(m.rest(), map, withDescription := true);
38  
        
39  
      if (swic_trim(s, myName + " default results:", m)) {
40  
        setField(defaultResults := parseInt(m.rest());
41  
        ret "OK, returning " + nResults(defaultResults) + " from now on";
42  
      }
43  
        
44  
      if (eqic(s, myName + " default results"))
45  
        ret "I'm returning " + nResults(defaultResults) + " by default";
46  
      
47  
      if (eqic(s, myName + " syntaxes"))
48  
        ret backtickQuote(lines_rtrim(elementPlusList(myName + ": ...", syntaxes)));
49  
        
50  
      if (swic_trim_any(s, m, myName + " new syntax:", myName + " add syntax:")) {
51  
        S syntax = massageSyntax(m.rest());
52  
        if (!endsWithEllipsis(syntax))
53  
          ret "Shouldn't this end with \"...\"? >> " + syntax;
54  
        if (!syntaxes.add(syntax)) ret "I already know that syntax!";
55  
        change();
56  
        ret "Syntax added! You can try: " + backtickQuote(replaceSuffix("...", randomThingToGoogle(), syntax));
57  
      }
58  
      
59  
      if (swic_trim(s, myName + " forget syntax:", m)) {
60  
        S syntax = massageSyntax(m.rest());
61  
        if (!syntaxes.remove(syntax)) ret "Syntax not found";
62  
        change();
63  
        ret "Syntax removed!";
64  
      }
65  
      
66  
      if (swicOneOf_trim(s, m, myName + " source", myName + " code"))
67  
        ret snippetURL(programID());
68  
      
69  
      if (ellipsisToDotPlusRegexpIC_matchAny(syntaxes, s, m))
70  
        ret doIt(m.rest(), map);
71  
        
72  
      null;
73  
    }
74  
    
75  
    bool setField(S name, O value) {
76  
      if (set_trueIfChanged(this, name, value)) false;
77  
      ret true with _change();
78  
    }
79  
    
80  
    S doIt(S query, Map map, O... _) {
81  
      LS l = regexpICFullMatch_groups("(\\d+) results? for[ :](.+)", query);
82  
      int results = defaultResults;
83  
      if (l != null) {
84  
        results = parseInt(first(l));
85  
        query = unquote(trim(last(l)));
86  
      }
87  
      bool safeSearch = !discord_isNSFWChannel_gen(map.get('channel));
88  
      module().setField(searches := searches+1);
89  
      updateModuleName();
90  
      ret discord_google(query, paramsPlus(_, +results, +safeSearch));
91  
    }
92  
  }
93  
  
94  
  @Override S answer(S input, Map map) {
95  
    ret mapEachLine_tlft_nempties(input, s -> {
96  
      // config by guild or user
97  
      long guildID = toLong(map.get('guildID));
98  
      long id = guildID;
99  
      print("Guild ID: " + guildID);
100  
      if (id == 0) {
101  
        id = toLong(map.get('userID));
102  
        print("User ID: " + id);
103  
      }
104  
      
105  
      ByServer data, bool isNew = unpair syncGetOrCreate2(dataByServer, id, func -> ByServer { new ByServer });
106  
      data.isGuild = id == guildID;
107  
      if (isNew) {
108  
        if (data.isGuild) ++guildCount;
109  
        change();
110  
      }
111  
      ret data.answer(s, map);
112  
    });
113  
  }
114  
  
115  
  S randomThingToGoogle() {
116  
    ret "why are penguins black and white?";
117  
  }
118  
  
119  
  S massageSyntax(S syntax) {
120  
    syntax = unquote(syntax);
121  
    ret trim(dropSuffix("...", syntax)) + " ...";
122  
  }
123  
  
124  
  start {
125  
    if (guildCount == 0)
126  
      setField(guildCount := countValuesWhere(dataByServer, isGuild := true));
127  
    updateModuleName();
128  
  }
129  
  
130  
  void updateModuleName() pcall {
131  
    dm_setModuleName("GBot - " + n2(guildCount, "guild") + ", " + n2(searches, "search", "searches"));
132  
  }
133  
  
134  
  S renderGuildCount() {
135  
    ret "Total guilds joined: " + guildCount + ". Live guilds: " + liveGuildCount();
136  
  }
137  
  
138  
  int liveGuildCount() {
139  
    ret l(discord.getGuilds());
140  
  }
141  
}

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: 300 / 1512
Version history: 27 change(s)
Referenced in: [show references]