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

119
LINES

< > BotCompany Repo | #1021630 // Discord Bot [v6, store lines in DB]

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

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

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  
        
17  
        bool bot = e.getAuthor().isBot();
18  
        
19  
        Message msg = e.getMessage();
20  
        
21  
        print("Channel type: " + e.getChannelType());
22  
        bool isPrivate = e.getChannelType() == ChannelType.PRIVATE;
23  
        print("Msg from " + e.getAuthor().getName() + ": " + e.getMessage().getContentDisplay());
24  
        S content = trim(msg.getContentRaw());
25  
        if (empty(content)) ret;
26  
27  
        O user = userConcept(e.getAuthor());
28  
        
29  
        S crud = dm_gazelle_linesCRUD();
30  
        O channel = dm_call(crud, 'uniqChannel, msg.getChannel().getIdLong());
31  
        dm_call(crud, 'cset, channel, litobjectarray(name := msg.getChannel().getName()));
32  
        
33  
        O lineConcept = dm_call(crud, 'uniqConcept, litObjectArrayAsObject(msgID := e.getMessage().getIdLong()));
34  
        dm_call(crud, 'cset, lineConcept, litobjectarray(
35  
          text := content,
36  
          +bot, +isPrivate,
37  
          author := user, +channel));
38  
        
39  
        ret if bot;
40  
        
41  
        GazelleEvalContext ctx = dm_gazelle_stdEvalContext(dm_gazelle_allRulesWithComment("discord"));
42  
        GazelleTree tree = new(ctx, content);
43  
        L<GazelleTree> l = dm_gazelle_getChildren(tree);
44  
        if (empty(l)) ret;
45  
        
46  
        for (GazelleTree t : takeFirst(10, l))
47  
          postInChannelWithDelete(e.getChannel(), t.line);
48  
      }
49  
      
50  
      public void onMessageReactionAdd(MessageReactionAddEvent e) pcall {
51  
        ret if !enabled || !licensed();
52  
        MessageReaction r = e.getReaction();
53  
        bool bot = e.getUser().isBot();
54  
        long msgID = r.getMessageIdLong();
55  
        add(msgsReactedTo, msgID);
56  
        print("User " + e.getUser() + (bot ? " (bot)" : "") + " reacted to message " + msgID + " with " + r.getReactionEmote());
57  
        if (bot) ret;
58  
59  
        S crud = dm_gazelle_linesCRUD();
60  
        O lineConcept = dm_call(crud, 'uniqConcept, litObjectArrayAsObject(+msgID));
61  
        L reactions = cast get(lineConcept, 'reactions);
62  
        print("lineConcept=" + lineConcept);
63  
        print("reactions=" + reactions);
64  
        O reaction = dm_call(crud, 'nuReaction, litObjectArrayAsObject(
65  
          user := userConcept(e.getUser()),
66  
          emoji := r.getReactionEmote().getName(),
67  
          created := now()));
68  
        print("reaction=" + reaction);
69  
          
70  
        dm_call(crud, 'cset, lineConcept, litobjectarray(
71  
          reactions := listPlus(reactions, reaction)));
72  
      }
73  
    });
74  
    
75  
    dm_registerAs('discordBot);
76  
    
77  
    print("Started bot");
78  
  }
79  
  
80  
  void cleanMeUp {
81  
    if (bot != null) pcall {
82  
      print("Shutting down bot");
83  
      bot.shutdown();
84  
      print("Bot shut down");
85  
    }
86  
    bot = null;
87  
  }
88  
  
89  
  O userConcept(User user) {
90  
    S crud = dm_gazelle_linesCRUD();
91  
    O userConcept = dm_call(crud, 'uniqUser, user.getIdLong());
92  
    dm_call(crud, 'cset, userConcept, litobjectarray(name := user.getName()));
93  
    ret userConcept;
94  
  }
95  
  
96  
  // API
97  
  
98  
  void postInChannel(long channelID, S msg) {
99  
    bot.getTextChannelById(channelID).sendMessage(msg).queue();
100  
  }
101  
  
102  
  void postInChannel(S channel, S msg) {
103  
    long id = dm_discord_channelID(channel);
104  
    if (id == 0) fail("Channel not found: " + channel);
105  
    postInChannel(id, msg);
106  
  }
107  
  
108  
  void postInChannelWithDelete(MessageChannel channel, S msg) {
109  
    channel.sendMessage(msg).queue(msg2 -> {
110  
      final long msgId = msg2.getIdLong();
111  
      print("I sent msg: " + msgId);
112  
      doLater(deleteDelay, r {
113  
        ret if contains(msgsReactedTo, msgId);
114  
        print("Deleting msg " + msgId);
115  
        msg2.delete().queue();
116  
      });
117  
    });
118  
  }
119  
}

Author comment

Began life as a copy of #1021622

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: #1021630
Snippet name: Discord Bot [v6, store lines in DB]
Eternal ID of this version: #1021630/24
Text MD5: 9f35ee8ea57389d3886a5e7fa2e5aa32
Transpilation MD5: 43e04ae57bf8614cfda072888c9ae991
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-23 21:34:52
Source code size: 4069 bytes / 119 lines
Pitched / IR pitched: No / No
Views / Downloads: 358 / 660
Version history: 23 change(s)
Referenced in: [show references]