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

66
LINES

< > BotCompany Repo | #1021621 // Discord Bot [v4, integrates Gazelle]

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

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

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  
        //final new Flag dontDelete;
30  
        S answer = lines_rtrim(collect line(l));
31  
        e.getChannel().sendMessage(answer).queue(msg -> {
32  
          final long msgId = msg.getIdLong();
33  
          print("I sent msg: " + msgId);
34  
          doLater(deleteDelay, r {
35  
            //ret if dontDelete!;
36  
            ret if contains(msgsReactedTo, msgId);
37  
            print("Deleting msg " + msg.getIdLong());
38  
            msg.delete().queue();
39  
          });
40  
        });
41  
      }
42  
      
43  
      public void onMessageReactionAdd(MessageReactionAddEvent e) {
44  
        ret if !enabled || !licensed();
45  
        MessageReaction r = e.getReaction();
46  
        bool bot = e.getUser().isBot();
47  
        long msgId = r.getMessageIdLong();
48  
        add(msgsReactedTo, msgId);
49  
        print("User " + e.getUser() + (bot ? " (bot)" : "") + " reacted to message " + msgId + " with " + r.getReactionEmote());
50  
        if (bot) ret;
51  
        //e.getChannel().sendMessage("You just " + r.getReactionEmote().getName() + " ed").queue();
52  
      }
53  
    });
54  
    
55  
    print("Started bot");
56  
  }
57  
  
58  
  void cleanMeUp {
59  
    if (bot != null) pcall {
60  
      print("Shutting down bot");
61  
      bot.shutdown();
62  
      print("Bot shut down");
63  
    }
64  
    bot = null;
65  
  }
66  
}

Author comment

Began life as a copy of #1021619

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: #1021621
Snippet name: Discord Bot [v4, integrates Gazelle]
Eternal ID of this version: #1021621/9
Text MD5: a6c30af2bf791ef9ed8ab17ab03d0d0e
Transpilation MD5: 6a73cbfcae706b4436e73da7548c044c
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 13:18:59
Source code size: 2277 bytes / 66 lines
Pitched / IR pitched: No / No
Views / Downloads: 265 / 346
Version history: 8 change(s)
Referenced in: [show references]