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

263
LINES

< > BotCompany Repo | #1021671 // Discord Bot [Gazelle-based, v8, old]

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

Uses 9211K of libraries. Click here for Pure Java version (20090L/120K).

1  
!7
2  
3  
set flag DynModule.
4  
5  
sclass ReasoningForLine {
6  
  long timestamp = now();
7  
  long outMsgID;
8  
  S outText;
9  
  long inMsgID, inUserID, inChannelID;
10  
  S inText;
11  
  GazelleTree tree;
12  
  int treeIndex; // line chosen from tree
13  
}
14  
15  
cmodule DiscordBot > DynPrintLogAndEnabled {
16  
  transient JDA bot;
17  
  transient new Set<Long> msgsReactedTo;
18  
  transient double deleteDelay = 60.0;
19  
  transient bool doDelete;
20  
  transient Color imageEmbedMysteriousLineColor = colorFromHex("36393f");
21  
  transient bool skipBad = true;
22  
  
23  
  transient new InheritableThreadLocal<MessageChannel> currentChannel;
24  
  transient new InheritableThreadLocal<Message> respondingTo;
25  
26  
  start {
27  
    logModuleOutput();
28  
    dm_useLocalMechListCopies();
29  
    // TODO: log JDA output
30  
    bot = discordBot(new ListenerAdapter {
31  
      public void onMessageReceived(MessageReceivedEvent e) pcall {
32  
        ret if !enabled || !licensed();
33  
        
34  
        bool bot = e.getAuthor().isBot();
35  
        
36  
        final Message msg = e.getMessage();
37  
        
38  
        print("Channel type: " + e.getChannelType());
39  
        bool isPrivate = e.getChannelType() == ChannelType.PRIVATE;
40  
        print("Msg from " + e.getAuthor().getName() + ": " + e.getMessage().getContentDisplay());
41  
        S content = trim(msg.getContentRaw());
42  
        if (empty(content)) ret;
43  
        fS originalContent = content;
44  
45  
        O user = userConcept(e.getAuthor());
46  
        long userID = e.getAuthor().getIdLong();
47  
        S userName = e.getAuthor().getName();
48  
        
49  
        S crud = dm_gazelle_linesCRUD();
50  
        O channel = dm_call(crud, 'uniqChannel, msg.getChannel().getIdLong());
51  
        dm_call(crud, 'cset, channel, litobjectarray(name := msg.getChannel().getName()));
52  
        
53  
        O lineConcept = dm_call(crud, 'uniqConcept, litObjectArrayAsObject(msgID := e.getMessage().getIdLong()));
54  
        dm_call(crud, 'cset, lineConcept, litobjectarray(
55  
          text := content,
56  
          +bot, +isPrivate,
57  
          author := user, +channel));
58  
        
59  
        ret if bot;
60  
        
61  
        final MessageChannel ch = e.getChannel();
62  
        long channelID = ch.getIdLong();
63  
        new Matches m;
64  
        
65  
        try {
66  
        
67  
          if (swicOneOf(content, "!eval ", "!fresh ", "!real-eval", "!safe-eval ") || eqic(content, "!fresh")) {
68  
            O safetyCheck = null;
69  
            bool authed = dm_discord_userCanEval(userID);
70  
            if (swic(content, "!safe-eval ", m)) {
71  
              content = "!eval " + m.rest();
72  
              authed = false;
73  
            }
74  
            
75  
            if (!authed) {
76  
              //ret with postInChannel(ch, "Sorry, you're not authed to eval");
77  
              safetyCheck = func(S code) -> S {
78  
                S safety = joinWithComma(getCodeFragmentSafety(code));
79  
                if (eq(safety, 'safe)) ret "";
80  
                S s = "Sorry. Safety level is " + safety;
81  
                Set<S> unknown = codeAnalysis_getUnknownIdentifiers(code);
82  
                if (nempty(unknown)) s += ". Unknown identifiers: " + joinWithComma(unknown);
83  
                ret s;
84  
              };
85  
            }
86  
            temp tempSetTL(currentChannel, ch);
87  
            temp tempSetTL(respondingTo, msg);
88  
            ret with dm_bot_execEvalCmd(voidfunc(S s) {
89  
              if (s != null)
90  
                postInChannel(ch, shorten(s, 1000))
91  
            }, content, +safetyCheck);
92  
          }
93  
          
94  
          if (eqic(content, "!rule")) {
95  
            LS lines = linesInChannelBy(channelID, userID);
96  
            if (contains(nextToLast(lines), "=>"))
97  
              content = "!rule " + nextToLast(lines);
98  
            else {
99  
              if (l(lines) < 3) fail("Too few lines");
100  
              content = "!rule " + nextToNextToLast(lines) + " => " + nextToLast(lines);
101  
            }
102  
          }
103  
          
104  
          if (swic_trim(content, "!rule ", m)) {
105  
            S s = m.rest();
106  
            S opt = leadingSquareBracketStuff(s);
107  
            s = dropActuallyLeadingSquareBracketStuff(s);
108  
            if (startsWith(s, "=>"))
109  
              s = assertNotNull("No last line in channel", nextToLast(linesInChannelBy(channelID, userID))) + "\n" + s;
110  
            LS comments = ll("discord");
111  
            if (cic(pairB(tok_splitAtDoubleArrow_pair(s)), userName)) {
112  
              s = optCurly(userName) + " says: " + s;
113  
              comments.add("with user name");
114  
            }
115  
            if (match("with *", opt, m) && remoteMechListMirror_isPublic($1))
116  
              comments.add("use helper table mech list " + quote($1));
117  
            s = replace(s, " + ", "\n+ ");
118  
            dm_gazelle_addRuleWithComment(s, lines_rtrim(comments));
119  
            ret with postInChannel(ch, dm_gazelle_addRuleWithComment_msg!);
120  
          }
121  
          
122  
          GazelleEvalContext ctx = dm_gazelle_stdEvalContext(dm_gazelle_allRulesWithComment("discord"));
123  
          ctx.engine.dropRulesWhere(r -> cicOneOf(r.comments, "in 1 = statement", "in = statement") || l(r.in) > 1);
124  
          gazelle_addHelperTablesToRules(ctx.engine);
125  
          
126  
          GazelleTree tree1 = new(ctx, content);
127  
          L<GazelleTree> l = dm_gazelle_getChildren(tree1);
128  
          
129  
          GazelleTree tree2 = new(ctx, optCurly(userName) + " says: " + content);
130  
          l.addAll(dm_gazelle_getChildren(tree2));
131  
          
132  
          if (skipBad) l = [GazelleTree t : l | neq(t.prediction, 'bad)];
133  
          if (empty(l)) ret;
134  
          gazelle_sortChildren(l);
135  
                    
136  
          int idx = 0;
137  
          for (final GazelleTree t : takeFirst(10, l)) {
138  
            final int _idx = idx++;
139  
            fS out = tok_dropCurlyBrackets(t.line);
140  
            print(">> " + t);
141  
            postInChannelWithDelete(ch, out, voidfunc(Message msg2) {
142  
              ReasoningForLine reasoning = nu(ReasoningForLine,
143  
                outMsgID := msg2.getIdLong(),
144  
                outText := out,
145  
                inMsgID := msg.getIdLong(),
146  
                inUserID := userID,
147  
                inChannelID := channelID,
148  
                inText := originalContent,
149  
                tree := t,
150  
                treeIndex := _idx);
151  
              saveGZStructToFileVerbose(javaxDataDir("Discord Reasonings/" + reasoning.outMsgID + ".gz"), reasoning);
152  
            });
153  
          }
154  
        } catch print error {
155  
          postInChannel(ch, exceptionToStringShort(error));
156  
        }
157  
      }
158  
      
159  
      public void onMessageReactionAdd(MessageReactionAddEvent e) pcall {
160  
        ret if !enabled || !licensed();
161  
        MessageReaction r = e.getReaction();
162  
        bool bot = e.getUser().isBot();
163  
        long msgID = r.getMessageIdLong();
164  
        add(msgsReactedTo, msgID);
165  
        print("User " + e.getUser() + (bot ? " (bot)" : "") + " reacted to message " + msgID + " with " + r.getReactionEmote());
166  
        if (bot) ret;
167  
168  
        S crud = dm_gazelle_linesCRUD();
169  
        O lineConcept = dm_call(crud, 'uniqConcept, litObjectArrayAsObject(+msgID));
170  
        L reactions = cast get(lineConcept, 'reactions);
171  
        print("lineConcept=" + lineConcept);
172  
        print("reactions=" + reactions);
173  
        O reaction = dm_call(crud, 'nuReaction, litObjectArrayAsObject(
174  
          user := userConcept(e.getUser()),
175  
          emoji := r.getReactionEmote().getName(),
176  
          created := now()));
177  
        print("reaction=" + reaction);
178  
          
179  
        dm_call(crud, 'cset, lineConcept, litobjectarray(
180  
          reactions := listPlus(reactions, reaction)));
181  
          
182  
        dm_discord_gatherFeedbackFromLine(dm_discord_importLine(lineConcept));
183  
      }
184  
    });
185  
    
186  
    dm_registerAs('discordBot);
187  
    print("Started bot");
188  
  }
189  
  
190  
  void cleanMeUp {
191  
    if (bot != null) pcall {
192  
      print("Shutting down bot");
193  
      bot.shutdown();
194  
      print("Bot shut down");
195  
    }
196  
    bot = null;
197  
  }
198  
  
199  
  O userConcept(User user) {
200  
    S crud = dm_gazelle_linesCRUD();
201  
    O userConcept = dm_call(crud, 'uniqUser, user.getIdLong());
202  
    dm_call(crud, 'cset, userConcept, litobjectarray(name := user.getName()));
203  
    ret userConcept;
204  
  }
205  
  
206  
  // API
207  
  
208  
  void postInChannel(long channelID, S msg) {
209  
    postInChannel(bot.getTextChannelById(channelID), msg);
210  
  }
211  
  
212  
  void postInChannel(MessageChannel channel, S msg) {
213  
    channel.sendMessage(msg).queue();
214  
  }
215  
  
216  
  void postInChannel(S channel, S msg) {
217  
    long id = dm_discord_channelID(channel);
218  
    if (id == 0) fail("Channel not found: " + channel);
219  
    postInChannel(id, msg);
220  
  }
221  
  
222  
  void postInChannelWithDelete(MessageChannel channel, S msg, VF1<Message> onPost) {
223  
    channel.sendMessage(msg).queue(msg2 -> {
224  
      pcallF(onPost, msg2);
225  
      final long msgId = msg2.getIdLong();
226  
      print("I sent msg: " + msgId);
227  
      if (doDelete) doLater(deleteDelay, r {
228  
        ret if contains(msgsReactedTo, msgId);
229  
        print("Deleting msg " + msgId);
230  
        msg2.delete().queue();
231  
      });
232  
    });
233  
  }
234  
  
235  
  void postImage(S url) {
236  
    postImage(currentChannel!, url);
237  
  }
238  
  
239  
  void postImage(S url, S title) {
240  
    postImage(currentChannel!, url, title);
241  
  }
242  
  
243  
  void postImage(MessageChannel channel, S url) {
244  
    postImage(channel, url, "");
245  
  }
246  
  
247  
  void postImage(MessageChannel channel, S url, S description) {
248  
    channel.sendMessage(
249  
      new EmbedBuilder()
250  
        .setImage(absoluteURL(url))
251  
        //.setTitle(unnull(title))
252  
        .setDescription(unnull(description))
253  
        .setColor(imageEmbedMysteriousLineColor)
254  
        .build()).queue();
255  
  }
256  
  
257  
  LS linesInChannelBy(long channelID, long userID) {
258  
    ret collect text(dm_discord_linesInChannelBy(channelID, userID));
259  
  }
260  
  
261  
  MessageChannel currentChannel() { ret currentChannel!; }
262  
  Message respondingTo() { ret respondingTo!; }
263  
}

Author comment

Began life as a copy of #1021659

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1021671
Snippet name: Discord Bot [Gazelle-based, v8, old]
Eternal ID of this version: #1021671/50
Text MD5: 47bcb32aa614932c4ec423046e08977c
Transpilation MD5: 4b98a873f21d814d378f93a54c83fbfd
Author: stefan
Category: javax / discord
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-03-23 05:01:40
Source code size: 9825 bytes / 263 lines
Pitched / IR pitched: No / No
Views / Downloads: 343 / 1077
Version history: 49 change(s)
Referenced in: [show references]