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

142
LINES

< > BotCompany Repo | #1022869 // Discord Module [just Discord reading & posting, OLD, use v2 instead]

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

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

1  
!7
2  
3  
cmodule DiscordModule > DynPrintLogAndEnabled {
4  
  switchable S token;
5  
  
6  
  transient JDA bot;
7  
  transient Color imageEmbedMysteriousLineColor = colorFromHex("36393f");
8  
9  
  start {
10  
    if (!enabled) ret with print("Not enabled");
11  
    
12  
    vm_cleanPrints(); // avoid Swing Unicode problem
13  
    logModuleOutput(); // good idea for chat bots
14  
15  
    // TODO: log JDA output going to System.out/err
16  
    bot = discordBot(new ListenerAdapter {
17  
      public void onMessageUpdate(MessageUpdateEvent e) pcall {
18  
        temp enter();
19  
        ret if !enabled || !licensed();
20  
        
21  
        long msgID = e.getMessage().getIdLong();
22  
        S content = e.getMessage().getContentRaw();
23  
        O lineConcept = dm_discord_lineForMsgID_unimported(msgID);
24  
        S rendered = msgID + ": " + content;
25  
        if (lineConcept == null)
26  
          ret with print("Weird: Message edited, but not found: " + rendered);
27  
        call(lineConcept, '_setField, editedText := content);
28  
        print("Message edited: " + rendered);
29  
      }
30  
      
31  
      public void onMessageReceived(MessageReceivedEvent e) pcall {
32  
        temp enter();
33  
        ret if !enabled || !licensed();
34  
        
35  
        bool bot = e.getAuthor().isBot();
36  
        long msgID = e.getMessage().getIdLong();
37  
        long userID = e.getAuthor().getIdLong();
38  
        Member member = e.getMember();
39  
        S userName = member == null ? null : member.getNickname(); // the changeable nick name - null sometimes?
40  
        if (userName == null && member != null) userName = member.getEffectiveName();
41  
        if (userName == null) userName = e.getAuthor().getName();
42  
        
43  
        final Message msg = e.getMessage();
44  
        
45  
        long channelID = e.getChannel().getIdLong();
46  
        print("Channel type: " + e.getChannelType());
47  
        bool isPrivate = e.getChannelType() == ChannelType.PRIVATE;
48  
        S content = trim(msg.getContentRaw());
49  
        print("Msg from " + userName + ": " + content);
50  
        if (empty(content)) ret;
51  
        
52  
        vmBus_send('incomingDiscordMessage,
53  
          litmapparams(fromBot := bot, module := dm_me(), +msgID, +userID, +userName,
54  
            +content, +isPrivate, +channelID));
55  
      }
56  
      
57  
      public void onMessageReactionAdd(MessageReactionAddEvent e) pcall {
58  
        temp enter();
59  
        ret if !enabled || !licensed();
60  
        MessageReaction r = e.getReaction();
61  
        bool bot = e.getUser().isBot();
62  
        long msgID = r.getMessageIdLong();
63  
        S emoji = r.getReactionEmote().getName();
64  
        
65  
        vmBus_send('incomingDiscordReaction, litmapparams(
66  
          fromBot := bot, module := dm_me(), +msgID, +emoji 
67  
        ));
68  
      }
69  
    }, +token);
70  
    
71  
    dm_registerAs('liveDiscordModule);
72  
  }
73  
  
74  
  void cleanMeUp {
75  
    if (bot != null) pcall {
76  
      print("Shutting down bot");
77  
      bot.shutdown();
78  
      print("Bot shut down");
79  
    }
80  
    bot = null;
81  
  }
82  
  
83  
  O userConcept(User user) {
84  
    S crud = dm_gazelle_linesCRUD();
85  
    O userConcept = dm_call(crud, 'uniqUser, user.getIdLong());
86  
    dm_call(crud, 'cset, userConcept, litobjectarray(name := user.getName()));
87  
    ret userConcept;
88  
  }
89  
  
90  
  // API
91  
  
92  
  MessageChannel getChannel(long channelID) {
93  
    ret bot.getTextChannelById(channelID);
94  
  }
95  
  
96  
  void postInChannel(long channelID, S msg) {
97  
    postInChannel(getChannel(channelID), msg);
98  
  }
99  
  
100  
  void postInChannel(MessageChannel channel, S msg) {
101  
    S postID = cast dm_call(gazelle_postedLinesCRUD(), 'postingLine, channel.getId(), msg);
102  
    channel.sendMessage(msg).queue(m -> {
103  
      dm_call(gazelle_postedLinesCRUD(), 'donePosting, postID, "discord msg " + m.getId());
104  
    });
105  
  }
106  
  
107  
  void postInChannel(S channel, S msg) {
108  
    long id = dm_discord_channelID(channel);
109  
    if (id == 0) fail("Channel not found: " + channel);
110  
    postInChannel(id, msg);
111  
  }
112  
  
113  
  void postInChannel(MessageChannel channel, S msg, IVF1<Message> onPost) {
114  
    S postID = cast dm_call(gazelle_postedLinesCRUD(), 'postingLine, channel.getId(), msg);
115  
    channel.sendMessage(msg).queue(msg2 -> {
116  
      dm_pcall(gazelle_postedLinesCRUD(), 'donePosting, postID, "discord msg " + msg2.getId());
117  
      pcallF(onPost, msg2);
118  
      final long msgId = msg2.getIdLong();
119  
      print("I sent msg: " + msgId);
120  
    }, error -> _handleException(error));
121  
  }
122  
  
123  
  void postImage(MessageChannel channel, S url, S description) {
124  
    channel.sendMessage(
125  
      new EmbedBuilder()
126  
        .setImage(absoluteURL(url))
127  
        //.setTitle(unnull(title))
128  
        .setDescription(unnull(description))
129  
        .setColor(imageEmbedMysteriousLineColor)
130  
        .build()).queue(); // TODO: posted lines
131  
  }
132  
  
133  
  void editMessage(long channelID, long msgID, S text) {
134  
    getChannel(channelID).editMessageById(str(msgID), text).queue();
135  
  }
136  
  
137  
  void sendPM(long userID, S text) {
138  
    bot.getUserById(userID).openPrivateChannel().queue(channel -> {
139  
      channel.sendMessage(text).queue();
140  
    });
141  
  }
142  
}

Author comment

Began life as a copy of #1022488

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: #1022869
Snippet name: Discord Module [just Discord reading & posting, OLD, use v2 instead]
Eternal ID of this version: #1022869/16
Text MD5: b09000e4904482afa8aea8329e44145b
Transpilation MD5: cee8bba54ee05997a1aee602a6cf66cf
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-06-21 20:28:44
Source code size: 5022 bytes / 142 lines
Pitched / IR pitched: No / No
Views / Downloads: 279 / 886
Version history: 15 change(s)
Referenced in: [show references]