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

66
LINES

< > BotCompany Repo | #1021621 // Discord Bot [v4, integrates Gazelle]

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

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

!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;
        
        //final new Flag dontDelete;
        S answer = lines_rtrim(collect line(l));
        e.getChannel().sendMessage(answer).queue(msg -> {
          final long msgId = msg.getIdLong();
          print("I sent msg: " + msgId);
          doLater(deleteDelay, r {
            //ret if dontDelete!;
            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 #1021619

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1021621
Snippet name: Discord Bot [v4, integrates Gazelle]
Eternal ID of this version: #1021621/9
Text MD5: a6c30af2bf791ef9ed8ab17ab03d0d0e
Transpilation MD5: 6a73cbfcae706b4436e73da7548c044c
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 13:18:59
Source code size: 2277 bytes / 66 lines
Pitched / IR pitched: No / No
Views / Downloads: 376 / 470
Version history: 8 change(s)
Referenced in: [show references]