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

107
LINES

< > BotCompany Repo | #1023928 // eeZee [LIVE, socket/http]

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

Uses 5909K of libraries. Click here for Pure Java version (9759L/53K).

1  
!7
2  
3  
cmodule eeZee > DynDiscordHopper {
4  
  switchable S httpURL = "https://55392290.ngrok.io";
5  
  switchable S serverName;
6  
  
7  
  switchable S prefix = "<@599426196647706645> ";
8  
  transient double sendTypingAfter = 6.0;
9  
10  
  L<Long> authorizedUsers = ll(463778175432785932, 547706854680297473);
11  
  
12  
  transient NotTooOften ntoError = notTooOften_1min();
13  
  
14  
  @Override S answer(S input, Map map) {
15  
    try answer super.answer(input, map);
16  
    
17  
    setField(prefix := "<@" + gazelle_selfUserID() + "> ");
18  
    
19  
    //setField(httpURL := or2(loadProgramTextFile("http-url"), httpURL));
20  
    
21  
    new Matches m;
22  
    
23  
    long userID = getLong userID(map);
24  
    print("User ID: " + userID);
25  
    
26  
    if (eqic(input, "hey " + trim(prefix)))
27  
      ret "hey";
28  
    
29  
    if (swic_trim(input, prefix + "add master ", m)) {
30  
      if (!contains(authorizedUsers, userID))
31  
        ret "You are not my master";
32  
        
33  
      add(authorizedUsers, parseFirstLong(m.rest()));
34  
      change();
35  
      
36  
      ret "Okidoki. Have " + n2(l(authorizedUsers), "master");
37  
    }
38  
    
39  
    S simplified = simpleSpaces_noTok(input);
40  
    //print(+simplified);
41  
    
42  
    if (swic_trim(simplified, prefix + "set url ", m)) {
43  
      print("Setting URL");
44  
      if (!contains(authorizedUsers, userID))
45  
        ret "You are not my master";
46  
        
47  
      S s = m.rest();
48  
      if (neq(s, "disabled") && !contains(s, ".")) ret "Invalid URL";
49  
50  
      setField(httpURL := s);
51  
      saveProgramTextFile("http-url", httpURL);
52  
      ret "URL set to " + httpURL + ", master";
53  
    }
54  
    
55  
    if (swic_trim(simplified, prefix + "get url", m)) {
56  
      if (!contains(authorizedUsers, userID))
57  
        ret "You are not my master";
58  
        
59  
      ret "URL is: " + backtickQuote(httpURL);
60  
    }
61  
    
62  
    long time = sysNow();
63  
    S s;
64  
    
65  
    if (eqic(httpURL, "disabled")) null;
66  
    
67  
    try {
68  
      temp doAfterSecondsIfStillRunning(sendTypingAfter, r { iAmTyping(map) });
69  
      s = loadPageWithTimeout(60.0, httpURL + hquery(
70  
        message := input,
71  
        +userID,
72  
        channelID := map.get('channelID),
73  
        msgID := map.get('msgID)));
74  
75  
      print("Got " + (s == null ? "timeout" : nChars(s)) + " in " + elapsedSeconds(time) + " s");
76  
      ret s;
77  
    } catch e {
78  
      if (!ntoError.yo()) null;
79  
      S str = exceptionToStringShort(e);
80  
      if (cicOneOf(str, "Server returned HTTP response code: 502",
81  
        "Server code 500",
82  
        "FileNotFoundException", "SocketTimeoutException")) // rough
83  
        ret null with print("Bot is offline (500/502/404/timeout, " + elapsedSeconds(time) + " s): " + str);
84  
      print(e);
85  
      ret "Error: " + shorten(100, str);
86  
    }
87  
  }
88  
  
89  
  start {
90  
    dm_registerAs_directLink('eeZee);
91  
    dm_setModuleName(serverName + " - eeZee");
92  
    
93  
    dm_vmBus_onMessage_q discordGuildJoin(voidfunc(Map map) {
94  
      ret unless map.get('module) == module();
95  
      S name = "?";
96  
      pcall {
97  
        name = rcall_string getName(rcall getUser(rcall getMember(map.get('event))));
98  
      }
99  
      print("Got join, forwarding. Name: " + name);
100  
      print(loadPageWithTimeout(60.0, httpURL + hquery(
101  
        event := 'join,
102  
        userName := name,
103  
        userID := getLong userID(map)
104  
      )));
105  
    });
106  
  }
107  
}

Author comment

Began life as a copy of #1023494

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: #1023928
Snippet name: eeZee [LIVE, socket/http]
Eternal ID of this version: #1023928/69
Text MD5: 0f26ea3bd3f0fbbb0724cc397f0c2cc3
Transpilation MD5: 6c07dc1668040bf0d8555c19bb71a36b
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-11 22:24:45
Source code size: 3292 bytes / 107 lines
Pitched / IR pitched: No / No
Views / Downloads: 367 / 2021
Version history: 68 change(s)
Referenced in: [show references]