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

64
LINES

< > BotCompany Repo | #1021622 // Discord Bot [v5, send lines separately]

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

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

1  
!7
2  
3  
set flag DynModule.
4  
5  
cmodule DiscordBot > DynPrintLogAndEnabled {
6  
  transient JDA bot;
7  
  transient new Set<Long> msgsReactedTo;
8  
  transient double deleteDelay = 60.0;
9  
  
10  
  start {
11  
    logModuleOutput();
12  
    // TODO: log JDA output
13  
    bot = discordBot(new ListenerAdapter {
14  
      public void onMessageReceived(MessageReceivedEvent e) pcall {
15  
        ret if !enabled || !licensed();
16  
        ret if e.getAuthor().isBot();
17  
        
18  
        print("Channel type: " + e.getChannelType());
19  
        bool isPrivate = e.getChannelType() == ChannelType.PRIVATE;
20  
        print("Msg from " + e.getAuthor().getName() + ": " + e.getMessage().getContentDisplay());
21  
        S content = trim(e.getMessage().getContentRaw());
22  
        if (empty(content)) ret;
23  
        
24  
        GazelleEvalContext ctx = dm_gazelle_stdEvalContext(dm_gazelle_allRulesWithComment("discord"));
25  
        GazelleTree tree = new(ctx, content);
26  
        L<GazelleTree> l = dm_gazelle_getChildren(tree);
27  
        if (empty(l)) ret;
28  
        
29  
        for (GazelleTree t : takeFirst(10, l))
30  
          e.getChannel().sendMessage(t.line).queue(msg -> {
31  
            final long msgId = msg.getIdLong();
32  
            print("I sent msg: " + msgId);
33  
            doLater(deleteDelay, r {
34  
              ret if contains(msgsReactedTo, msgId);
35  
              print("Deleting msg " + msg.getIdLong());
36  
              msg.delete().queue();
37  
            });
38  
          });
39  
      }
40  
      
41  
      public void onMessageReactionAdd(MessageReactionAddEvent e) {
42  
        ret if !enabled || !licensed();
43  
        MessageReaction r = e.getReaction();
44  
        bool bot = e.getUser().isBot();
45  
        long msgId = r.getMessageIdLong();
46  
        add(msgsReactedTo, msgId);
47  
        print("User " + e.getUser() + (bot ? " (bot)" : "") + " reacted to message " + msgId + " with " + r.getReactionEmote());
48  
        if (bot) ret;
49  
        //e.getChannel().sendMessage("You just " + r.getReactionEmote().getName() + " ed").queue();
50  
      }
51  
    });
52  
    
53  
    print("Started bot");
54  
  }
55  
  
56  
  void cleanMeUp {
57  
    if (bot != null) pcall {
58  
      print("Shutting down bot");
59  
      bot.shutdown();
60  
      print("Bot shut down");
61  
    }
62  
    bot = null;
63  
  }
64  
}

Author comment

Began life as a copy of #1021621

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1021622
Snippet name: Discord Bot [v5, send lines separately]
Eternal ID of this version: #1021622/4
Text MD5: c5e2b85b59f4bb5074dc79644728e575
Transpilation MD5: 0b84c2a1a30ad8abc897addb35cda8cb
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-02-22 23:13:21
Source code size: 2220 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 323 / 419
Version history: 3 change(s)
Referenced in: [show references]