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

136
LINES

< > BotCompany Repo | #1024145 // Gazelle 2 [LIVE]

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

Uses 6525K of libraries. Click here for Pure Java version (19561L/112K).

1  
!7
2  
3  
set flag DynModule. // for evals
4  
5  
cmodule Gazelle2 > DynDiscordHopper {
6  
  S myName = "Gazelle";
7  
  
8  
  void init after super {
9  
    dm_vmBus_onMessage_q editedDiscordMessage(voidfunc(Map map) {
10  
      ret if !enabled || !dm_isMe(map.get('module));
11  
      ret if isTrue(map.get('bot));
12  
      S s = getString content(map);
13  
      Message msg = cast map.get('msg);
14  
      long msgID = getLong msgID(map);
15  
      MessageChannel channel = cast map.get('channel);
16  
      long userID = getLong userID(map);
17  
      
18  
      print("Got edited message: " + s);
19  
      
20  
      // experimental edited msg handling for evals
21  
      if (startsWith(s, "!")) {
22  
        // postInChannel(channel, "I saw your edit, " + discordAt(userID));
23  
        
24  
        RecordedAnswer oldAnswer = firstWhere(recordedAnswers, questionMsgID := msgID);
25  
        print("Have " + n2(recordedAnswers, "recorded answer")
26  
          + ", found " + oldAnswer);
27  
        if (oldAnswer != null) {
28  
          recordedAnswers.remove(oldAnswer);
29  
          change();
30  
          pcall {
31  
            print("Deleting my old answer: " + oldAnswer.answerMsgID);
32  
            discord.getTextChannelById(oldAnswer.channelID)
33  
              .deleteMessageById(oldAnswer.answerMsgID).queue();
34  
          }
35  
        }
36  
        S answer = answer(s, map);
37  
        sendReply(map, answer);
38  
      }
39  
    });
40  
  }
41  
  
42  
  transient new InheritableThreadLocal<Map> msgMap; // while answering
43  
  
44  
  @Override S answer(S s, Map map) {
45  
    new Matches m;
46  
    
47  
    temp tempSetTL(msgMap, map);
48  
    
49  
    long ch = getLong channelID(map);
50  
    long userID = getLong userID(map);
51  
52  
    S sOld = s;    
53  
    s = dropPrefix_trim(atSelf() + " ", s);
54  
    if (sOld != s) print("dropped atSelf >> " + s);
55  
      
56  
    s = simpleSpaces_javaTok(s);
57  
    
58  
    if (match("help", s) || match(myName + " help", s)) ret linesLL(
59  
      atSelf() + " source - show sources",
60  
      myName + ", show me X - show an image",
61  
      myName + ", look up X - dictionary lookup",
62  
      "what's your name - say my name",
63  
      "!eval 1+2 - evaluate JavaX code"
64  
    );
65  
    
66  
    if (eqicOneOf(s, "source", "sources", "source code"))
67  
      ret snippetLink(programID());
68  
      
69  
    if "what's your name" ret myName;
70  
    
71  
    if (swic_trim(s, myName + ", show me ", m)) {
72  
      iAmTyping(map);
73  
      S query = m.rest();
74  
      BufferedImage img = quickVisualize(query);
75  
      if (img == null) ret "No image found, sorry!";
76  
      postImage(map, uploadToImageServer(img, query));
77  
      null;
78  
    }
79  
    
80  
    if (swic_trim(s, myName + ", look up ", m)) {
81  
      if (isSingleWord($1))
82  
        try answer wordNet_pretty($1);
83  
      
84  
      ret ":cry: I don't know";
85  
    }
86  
    
87  
    if (ellipsisToDotStarRegexpFindIC("days...year", collapse(s)))
88  
      ret nDays(daysUntilEndOfYear()) + " till end of year";
89  
90  
    S content = s;      
91  
    try {
92  
      if (swicOneOf(content, "!eval ", "!fresh ", "!real-eval", "!safe-eval ") || eqic(content, "!fresh")) {
93  
        O safetyCheck = null;
94  
        bool authed = dm_discord_userCanEval(userID);
95  
        print("Authed " + userID + " => " + authed);
96  
        if (swic_trim(content, "!safe-eval ", m)) {
97  
          content = "!eval " + m.rest();
98  
          authed = false;
99  
        }
100  
        
101  
        /*if (regexpMatches("![a-z\\-]+\\s+again", content)) {
102  
          GazelleLine last = dm_discord_nextToLastEvalByUser(userID);
103  
          if (last == null) ret "No last eval found";
104  
          content = replaceSuffix("again", afterSpace(last.text), content);
105  
        }*/
106  
        
107  
        if (!authed)
108  
          safetyCheck = botEval_strictSafetyCheck();
109  
        
110  
        iAmTyping(map);
111  
        dm_bot_execEvalCmd(voidfunc(S s) {
112  
          if (s != null)
113  
            sendReply(map, shorten(ifEmpty(s, [[""]]), 1000));
114  
        }, content, +safetyCheck);
115  
        null;
116  
      }
117  
    } catch print error {
118  
      postInChannel(ch, exceptionToStringShort(error));
119  
    }
120  
121  
    ret super.answer(s, map);
122  
  }
123  
  
124  
  // for evals with gazelle_msg()
125  
  Message respondingTo() { ret (Message) get msg(msgMap!); }
126  
  
127  
  MessageChannel currentChannel() { ret (MessageChannel) get channel(msgMap!); }
128  
  
129  
  void postImage(S url) {
130  
    postImage(msgMap!, url);
131  
}
132  
133  
  void postImage(S url, S description) {
134  
    postImage(msgMap!, url, description);
135  
  }
136  
}

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: #1024145
Snippet name: Gazelle 2 [LIVE]
Eternal ID of this version: #1024145/50
Text MD5: 42a87e531e7bc362a13241ae536044e6
Transpilation MD5: dfe3027fc6f25384a186c9bd24dbd095
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-12-13 14:29:19
Source code size: 4348 bytes / 136 lines
Pitched / IR pitched: No / No
Views / Downloads: 440 / 33736
Version history: 49 change(s)
Referenced in: [show references]