22
33import cartoland .utilities .CommandBlocksHandle ;
44import cartoland .utilities .FileHandle ;
5+ import net .dv8tion .jda .api .entities .Member ;
56import net .dv8tion .jda .api .entities .User ;
7+ import net .dv8tion .jda .api .entities .emoji .Emoji ;
68import net .dv8tion .jda .api .events .session .ReadyEvent ;
79import net .dv8tion .jda .api .hooks .ListenerAdapter ;
10+ import net .dv8tion .jda .api .requests .RestAction ;
11+ import net .dv8tion .jda .api .requests .restaction .CacheRestAction ;
812import org .jetbrains .annotations .NotNull ;
913
1014import java .time .Duration ;
1115import java .time .LocalDateTime ;
12- import java .util .concurrent .Executors ;
16+ import java .time .OffsetDateTime ;
17+ import java .util .ArrayList ;
18+ import java .util .List ;
19+ import java .util .Map ;
1320import java .util .concurrent .TimeUnit ;
1421
1522import static cartoland .utilities .IDAndEntities .*;
@@ -73,6 +80,8 @@ public void onReady(@NotNull ReadyEvent event)
7380
7481 ohBoy3AM (); //好棒 三點了
7582
83+ idleFormPost12PM (); //中午十二點時處理未解決的論壇貼文
84+
7685 initialIDAndName (); //初始化idAndName
7786
7887 String logString = "Cartoland Bot is now online." ;
@@ -107,23 +116,62 @@ private void ohBoy3AM()
107116
108117 long secondsUntil3AM = Duration .between (now , threeAM ).getSeconds ();
109118
110- threeAMService = Executors .newScheduledThreadPool (1 );
111- threeAMHandle = threeAMService .scheduleAtFixedRate (
119+ threeAMTask = scheduleExecutor .scheduleAtFixedRate (
112120 () -> undergroundChannel .sendMessage ("https://imgur.com/EGO35hf" ).queue (),
113121 secondsUntil3AM , TimeUnit .DAYS .toSeconds (1 ), TimeUnit .SECONDS );
114122 }
115123
116- private void initialIDAndName ()
124+ private final Emoji reminder_ribbon = Emoji .fromUnicode ("🎗️" );
125+
126+ private void idleFormPost12PM ()
117127 {
118- CommandBlocksHandle .getMap ().forEach ((userIDString , blocks ) ->
128+ LocalDateTime now = LocalDateTime .now ();
129+ LocalDateTime twelvePM = now .withHour (12 ).withMinute (0 ).withSecond (0 );
130+
131+ if (now .compareTo (twelvePM ) > 0 )
132+ twelvePM = twelvePM .plusDays (1L );
133+
134+ long secondsUntil12PM = Duration .between (now , twelvePM ).getSeconds ();
135+
136+ twelvePMTask = scheduleExecutor .scheduleAtFixedRate (() -> questionsChannel .getThreadChannels ().forEach (forumPost ->
119137 {
120- long userID = Long .parseLong (userIDString );
121- User user = jda .getUserById (userID );
122- if (user != null )
123- idAndNames .put (userID , user .getAsTag ());
124- else
125- jda .retrieveUserById (userID ).queue (getUser -> idAndNames .put (userID , getUser .getAsTag ()));
126- });
138+ if (forumPost .isArchived ())
139+ return ;
140+
141+ forumPost .retrieveMessageById (forumPost .getLatestMessageIdLong ()).queue (lastMessage ->
142+ {
143+ Member messageCreatorMember = lastMessage .getMember ();
144+ if (messageCreatorMember == null )
145+ return ;
146+ User messageCreatorUser = messageCreatorMember .getUser ();
147+ if (messageCreatorUser .isBot () || messageCreatorUser .isSystem ())
148+ return ;
149+
150+ OffsetDateTime lastMessageCreateTime = lastMessage .getTimeCreated ();
151+ long hoursFromNowToLastMessage = Duration .between (lastMessageCreateTime , OffsetDateTime .now ()).toHours ();
152+ if (hoursFromNowToLastMessage < 24 )
153+ return ;
154+
155+ Member owner = forumPost .getOwner ();
156+ if (owner == null )
157+ return ;
158+
159+ String mentionOwner = owner .getAsMention ();
160+ forumPost .sendMessage (mentionOwner + ",你的問題解決了嗎?如果已經解決了,記得使用`:resolved:`表情符號關閉貼文。\n " +
161+ "如果還沒解決,可以嘗試在問題中加入更多資訊。" )
162+ .queue (message -> message .addReaction (reminder_ribbon ).queue ());
163+ });
164+
165+ }), secondsUntil12PM , TimeUnit .DAYS .toSeconds (1 ), TimeUnit .SECONDS );
166+ }
167+
168+ private void initialIDAndName ()
169+ {
170+ Map <String , Object > commandBlockMap = CommandBlocksHandle .getMap ();
171+ List <CacheRestAction <User >> retrieve = new ArrayList <>(commandBlockMap .size ());
172+ commandBlockMap .forEach ((userIDString , blocks ) -> retrieve .add (jda .retrieveUserById (userIDString )));
173+ if (retrieve .size () > 0 )
174+ RestAction .allOf (retrieve ).queue (users -> users .forEach (user -> idAndNames .put (user .getIdLong (), user .getAsTag ())));
127175 CommandBlocksHandle .changed = true ;
128176 }
129177}
0 commit comments