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

55
LINES

< > BotCompany Repo | #1021619 // Discord Bot Spike [v3, notice reactions, now a module for Stefan's OS]

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

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

1  
!7
2  
3  
cmodule DiscordBot > DynPrintLogAndEnabled {
4  
  transient JDA bot;
5  
  
6  
  start {
7  
    logModuleOutput();
8  
    // TODO: log JDA output
9  
    bot = discordBot(new ListenerAdapter {
10  
      public void onMessageReceived(MessageReceivedEvent e) {
11  
        ret if !enabled || !licensed();
12  
        ret if e.getAuthor().isBot();
13  
        
14  
        print("Channel type: " + e.getChannelType());
15  
        bool isPrivate = e.getChannelType() == ChannelType.PRIVATE;
16  
        print("Msg from " + e.getAuthor().getName() + ": " + e.getMessage().getContentDisplay());
17  
        S content = e.getMessage().getContentRaw();
18  
        new Matches m;
19  
        S s = "";
20  
        if (isPrivate) s = content;
21  
        else if (swic(content, "gazelle", m)) s = m.rest();
22  
        if (empty(s)) ret;
23  
        s = "Bah! " + trim(s);
24  
        print("Sending: " + s);
25  
        fbool delete = ewic(content, "delete");
26  
        e.getChannel().sendMessage(s).queue(msg -> {
27  
          print("I sent msg: " + msg.getIdLong());
28  
          if (delete) doLater(5.0, r {
29  
            msg.delete().queue();
30  
          });
31  
        });
32  
      }
33  
      
34  
      public void onMessageReactionAdd(MessageReactionAddEvent e) {
35  
        ret if !enabled || !licensed();
36  
        MessageReaction r = e.getReaction();
37  
        bool bot = e.getUser().isBot();
38  
        print("User " + e.getUser() + (bot ? " (bot)" : "") + " reacted to message " + r.getMessageIdLong() + " with " + r.getReactionEmote());
39  
        if (bot) ret;
40  
        e.getChannel().sendMessage("You just " + r.getReactionEmote().getName() + " ed").queue();
41  
      }
42  
    });
43  
    
44  
    print("Started bot");
45  
  }
46  
  
47  
  void cleanMeUp {
48  
    if (bot != null) pcall {
49  
      print("Shutting down bot");
50  
      bot.shutdown();
51  
      print("Bot shut down");
52  
    }
53  
    bot = null;
54  
  }
55  
}

Author comment

Began life as a copy of #1021618

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1021619
Snippet name: Discord Bot Spike [v3, notice reactions, now a module for Stefan's OS]
Eternal ID of this version: #1021619/13
Text MD5: b89b157acff9ee0d3fd04ae7782ad7ed
Transpilation MD5: 68b79cd9bd99bf62e1adfac9f06ebec8
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 12:55:59
Source code size: 1817 bytes / 55 lines
Pitched / IR pitched: No / No
Views / Downloads: 346 / 503
Version history: 12 change(s)
Referenced in: [show references]