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).

!7

cmodule DiscordBot > DynPrintLogAndEnabled {
  transient JDA bot;
  
  start {
    logModuleOutput();
    // TODO: log JDA output
    bot = discordBot(new ListenerAdapter {
      public void onMessageReceived(MessageReceivedEvent e) {
        ret if !enabled || !licensed();
        ret if e.getAuthor().isBot();
        
        print("Channel type: " + e.getChannelType());
        bool isPrivate = e.getChannelType() == ChannelType.PRIVATE;
        print("Msg from " + e.getAuthor().getName() + ": " + e.getMessage().getContentDisplay());
        S content = e.getMessage().getContentRaw();
        new Matches m;
        S s = "";
        if (isPrivate) s = content;
        else if (swic(content, "gazelle", m)) s = m.rest();
        if (empty(s)) ret;
        s = "Bah! " + trim(s);
        print("Sending: " + s);
        fbool delete = ewic(content, "delete");
        e.getChannel().sendMessage(s).queue(msg -> {
          print("I sent msg: " + msg.getIdLong());
          if (delete) doLater(5.0, r {
            msg.delete().queue();
          });
        });
      }
      
      public void onMessageReactionAdd(MessageReactionAddEvent e) {
        ret if !enabled || !licensed();
        MessageReaction r = e.getReaction();
        bool bot = e.getUser().isBot();
        print("User " + e.getUser() + (bot ? " (bot)" : "") + " reacted to message " + r.getMessageIdLong() + " with " + r.getReactionEmote());
        if (bot) ret;
        e.getChannel().sendMessage("You just " + r.getReactionEmote().getName() + " ed").queue();
      }
    });
    
    print("Started bot");
  }
  
  void cleanMeUp {
    if (bot != null) pcall {
      print("Shutting down bot");
      bot.shutdown();
      print("Bot shut down");
    }
    bot = null;
  }
}

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: 345 / 500
Version history: 12 change(s)
Referenced in: [show references]