1 | // requires Msg concept |
2 | |
3 | class DiscordScanner { |
4 | MessageChannel channel; |
5 | MessageHistory history; |
6 | int scanned; |
7 | bool incremental, postResult = true; |
8 | Runnable whenDone; |
9 | |
10 | run { |
11 | if (history == null) history = channel.getHistory(); |
12 | history.retrievePast(100).queue(msgs -> { |
13 | temp enter(); |
14 | print("Got msgs: " + l(msgs)); |
15 | if (empty(msgs)) { |
16 | if (postResult) |
17 | postInChannel(channel, "Done scanning " + discordChannelRef(channel.getIdLong()) + " (" + nMessages(scanned) + ")"); |
18 | callF(whenDone); |
19 | ret; |
20 | } |
21 | scanned += l(msgs); |
22 | if (storeMsgs(msgs) && incremental) { |
23 | if (postResult) |
24 | postInChannel(channel, "Stopping incremental scan of " + discordChannelRef(channel.getIdLong()) + " after " + nMessages(scanned)); |
25 | callF(whenDone); |
26 | ret; |
27 | } |
28 | run(); |
29 | }, onQueueError); |
30 | } |
31 | } |
32 | |
33 | // returns true if seen |
34 | Pair<Msg, Bool> storeMsg(Message msg) { |
35 | if (msg == null) null; |
36 | Msg m, bool isNew = unpair uniq2(Msg, msgID := msg.getIdLong()); |
37 | cset(m, |
38 | channelID := msg.getTextChannel().getIdLong(), |
39 | userID := msg.getAuthor().getIdLong(), |
40 | date := msg.getCreationTime().toEpochSecond(), |
41 | edited := msg.isEdited() ? msg.getEditedTime().toEpochSecond() : 0, |
42 | text := msg.getContentRaw()); |
43 | ret pair(m, !isNew); |
44 | } |
45 | |
46 | // returns true if any seen |
47 | bool storeMsgs(L<Message> l) { |
48 | bool anySeen; |
49 | for (Message msg : l) { |
50 | if (pairB(storeMsg(msg))) set anySeen; |
51 | } |
52 | ret anySeen; |
53 | } |
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: | 192 / 378 |
Version history: | 10 change(s) |
Referenced in: | [show references] |