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

138
LINES

< > BotCompany Repo | #1026293 // Gazelle 2 with JDA 4.0 [OK]

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

Uses 11435K of libraries. Click here for Pure Java version (20186L/113K).

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

Author comment

Began life as a copy of #1024145

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1026293
Snippet name: Gazelle 2 with JDA 4.0 [OK]
Eternal ID of this version: #1026293/7
Text MD5: f1fadf9cf01c5da5d125d2dfe5fa0019
Transpilation MD5: 673236080121669cc6f9058e2a785446
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-05-04 00:17:10
Source code size: 4426 bytes / 138 lines
Pitched / IR pitched: No / No
Views / Downloads: 190 / 56116
Version history: 6 change(s)
Referenced in: [show references]