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

64
LINES

< > BotCompany Repo | #1021622 // Discord Bot [v5, send lines separately]

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

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

!7

set flag DynModule.

cmodule DiscordBot > DynPrintLogAndEnabled {
  transient JDA bot;
  transient new Set<Long> msgsReactedTo;
  transient double deleteDelay = 60.0;
  
  start {
    logModuleOutput();
    // TODO: log JDA output
    bot = discordBot(new ListenerAdapter {
      public void onMessageReceived(MessageReceivedEvent e) pcall {
        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 = trim(e.getMessage().getContentRaw());
        if (empty(content)) ret;
        
        GazelleEvalContext ctx = dm_gazelle_stdEvalContext(dm_gazelle_allRulesWithComment("discord"));
        GazelleTree tree = new(ctx, content);
        L<GazelleTree> l = dm_gazelle_getChildren(tree);
        if (empty(l)) ret;
        
        for (GazelleTree t : takeFirst(10, l))
          e.getChannel().sendMessage(t.line).queue(msg -> {
            final long msgId = msg.getIdLong();
            print("I sent msg: " + msgId);
            doLater(deleteDelay, r {
              ret if contains(msgsReactedTo, msgId);
              print("Deleting msg " + msg.getIdLong());
              msg.delete().queue();
            });
          });
      }
      
      public void onMessageReactionAdd(MessageReactionAddEvent e) {
        ret if !enabled || !licensed();
        MessageReaction r = e.getReaction();
        bool bot = e.getUser().isBot();
        long msgId = r.getMessageIdLong();
        add(msgsReactedTo, msgId);
        print("User " + e.getUser() + (bot ? " (bot)" : "") + " reacted to message " + msgId + " 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 #1021621

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: #1021622
Snippet name: Discord Bot [v5, send lines separately]
Eternal ID of this version: #1021622/4
Text MD5: c5e2b85b59f4bb5074dc79644728e575
Transpilation MD5: 0b84c2a1a30ad8abc897addb35cda8cb
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 23:13:21
Source code size: 2220 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 255 / 331
Version history: 3 change(s)
Referenced in: [show references]