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

53
LINES

< > BotCompany Repo | #1025906 // DiscordScanner + storeMsg + storeMsgs [Include]

JavaX fragment (include)

// requires Msg concept

class DiscordScanner {
  MessageChannel channel;
  MessageHistory history;
  int scanned;
  bool incremental, postResult = true;
  Runnable whenDone;

  run {
    if (history == null) history = channel.getHistory();
    history.retrievePast(100).queue(msgs -> {
      temp enter();
      print("Got msgs: " + l(msgs));
      if (empty(msgs)) {
        if (postResult)
          postInChannel(channel, "Done scanning " + discordChannelRef(channel.getIdLong()) + " (" + nMessages(scanned) + ")");
        callF(whenDone);
        ret;
      }
      scanned += l(msgs);
      if (storeMsgs(msgs) && incremental) {
        if (postResult)
          postInChannel(channel, "Stopping incremental scan of " + discordChannelRef(channel.getIdLong()) + " after " + nMessages(scanned));
        callF(whenDone);
        ret;
      }
      run();
    }, onQueueError);
  }
}

// returns true if seen
Pair<Msg, Bool> storeMsg(Message msg) {
  if (msg == null) null;
  Msg m, bool isNew = unpair uniq2(Msg, msgID := msg.getIdLong());
  cset(m,
    channelID := msg.getTextChannel().getIdLong(),
    userID := msg.getAuthor().getIdLong(),
    date := msg.getCreationTime().toEpochSecond(),
    edited := msg.isEdited() ? msg.getEditedTime().toEpochSecond() : 0,
    text := msg.getContentRaw());
  ret pair(m, !isNew);
}

// returns true if any seen
bool storeMsgs(L<Message> l) {
  bool anySeen;
  for (Message msg : l) {
    if (pairB(storeMsg(msg))) set anySeen;
  }
  ret anySeen;
}

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1025906
Snippet name: DiscordScanner + storeMsg + storeMsgs [Include]
Eternal ID of this version: #1025906/11
Text MD5: ccc727f454a0adbd8b07c7b61274cabb
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-29 12:49:56
Source code size: 1548 bytes / 53 lines
Pitched / IR pitched: No / No
Views / Downloads: 125 / 307
Version history: 10 change(s)
Referenced in: [show references]