18
18
*/
19
19
package io .github .org .programming .bot ;
20
20
21
+ import io .github .org .programming .bot .commands .thread .ActiveQuestionsHandler ;
22
+ import io .github .org .programming .bot .commands .thread .AskThreadStatus ;
21
23
import io .github .org .programming .bot .config .BotConfig ;
22
24
import io .github .org .programming .database .Database ;
23
25
import net .dv8tion .jda .api .JDA ;
24
26
import net .dv8tion .jda .api .JDABuilder ;
25
27
import net .dv8tion .jda .api .OnlineStatus ;
26
- import net .dv8tion .jda .api .entities .Activity ;
27
- import net .dv8tion .jda .api .entities .Guild ;
28
- import net .dv8tion .jda .api .events .ReadyEvent ;
28
+ import net .dv8tion .jda .api .entities .*;
29
29
import net .dv8tion .jda .api .hooks .ListenerAdapter ;
30
30
import net .dv8tion .jda .api .requests .GatewayIntent ;
31
- import net .dv8tion .jda .api .utils . MemberCachePolicy ;
31
+ import net .dv8tion .jda .api .requests . restaction . pagination . ThreadChannelPaginationAction ;
32
32
import net .dv8tion .jda .api .utils .cache .CacheFlag ;
33
- import org .jetbrains .annotations .NotNull ;
34
33
import org .jooq .DSLContext ;
35
34
import org .jooq .SQLDialect ;
36
35
import org .jooq .impl .DSL ;
37
36
import org .slf4j .Logger ;
38
37
import org .slf4j .LoggerFactory ;
39
38
39
+ import java .time .Instant ;
40
+ import java .util .List ;
41
+ import java .util .Objects ;
40
42
import java .util .concurrent .ExecutorService ;
41
43
import java .util .concurrent .Executors ;
42
44
import java .util .concurrent .ScheduledExecutorService ;
45
+ import java .util .concurrent .TimeUnit ;
46
+
47
+ import static io .github .org .programming .bot .commands .thread .ActiveQuestionsHandler .*;
48
+ import static io .github .org .programming .bot .commands .thread .util .SupportedCategories .messageToSend ;
49
+ import static io .github .org .programming .database .thread .AskDatabase .deleteAskDatabaseWithTime ;
50
+ import static io .github .org .programming .database .thread .AskDatabase .getAskTimeStamps ;
43
51
44
52
public class ProgrammingBot extends ListenerAdapter {
45
53
private final Logger logger = LoggerFactory .getLogger (ProgrammingBot .class );
@@ -67,20 +75,63 @@ public ProgrammingBot(String[] args) throws Exception {
67
75
68
76
Guild guild = jda .awaitReady ().getGuildById (BotConfig .getGuildId ());
69
77
70
- jda .awaitReady ().addEventListener (new SlashCommandReg (jda , guild ), this );
78
+ jda .awaitReady ().addEventListener (new RegisterSlashCommands (jda , guild ), this );
71
79
72
80
logger .info ("Bot is ready in guild {}" , guild .getName ());
73
81
74
- if (Database .isConnected ())
75
- logger .info ("Database is connected" );
76
- else
77
- logger .error ("Database is not connected" );
82
+ scheduledExecutor .scheduleAtFixedRate (() -> {
83
+ checkIfAskActiveQuestionMessageExists (guild );
84
+ }, 0 , 1 , TimeUnit .DAYS );
85
+
86
+ // need to check this every minute
87
+ scheduledExecutor .scheduleAtFixedRate (() -> {
88
+ checkIfAskThreadTimeNeedsToBeRest (jda );
89
+ }, 0 , 1 , TimeUnit .SECONDS );
90
+
91
+ scheduledExecutor .scheduleAtFixedRate (() -> {
92
+ checkIfAskHelpThreadArchived (guild );
93
+ }, 0 , 1 , TimeUnit .SECONDS );
94
+ }
95
+
96
+ public synchronized void checkIfAskActiveQuestionMessageExists (Guild guild ) {
97
+ TextChannel activeQuestionsChannel =
98
+ guild .getTextChannelById (BotConfig .getActiveQuestionChannelId ());
99
+
100
+ if (activeQuestionsChannel == null ) {
101
+ throw new IllegalStateException ("Active questions channel not found" );
102
+ }
103
+
104
+ String messageId = getActiveQuestionMessage (guild .getId ());
105
+
106
+ if (messageId == null ) {
107
+ Message message = activeQuestionsChannel .sendMessage (messageToSend ()).complete ();
108
+ updateActiveQuestionMessage (guild .getId (), message .getId ());
109
+ ActiveQuestionsHandler .setMessage (message );
110
+ } else {
111
+ activeQuestionsChannel .retrieveMessageById (messageId )
112
+ .queue (this ::dealWithSuccess ,
113
+ e -> dealWithError (e , guild , messageId , activeQuestionsChannel ));
114
+ }
115
+ }
116
+
117
+ private void dealWithError (Throwable error , Guild guild , String messageId ,
118
+ TextChannel activeQuestionsChannel ) {
119
+ if (Objects .equals (error .getMessage (), "10008: Unknown Message" )) {
120
+ deleteActiveQuestionMessageId (guild .getId (), messageId );
121
+ activeQuestionsChannel .sendMessage (messageToSend ()).queue (success -> {
122
+ updateActiveQuestionMessage (guild .getId (), success .getId ());
123
+ ActiveQuestionsHandler .setMessage (success );
124
+ });
125
+ }
126
+ }
127
+
128
+ private void dealWithSuccess (Message message ) {
129
+ ActiveQuestionsHandler .setMessage (message );
78
130
}
79
131
80
132
public void onDatabase () {
81
133
context = DSL .using (Database .getConnection (), SQLDialect .POSTGRES );
82
134
83
-
84
135
if (Database .isConnected ()) {
85
136
logger .info ("Connected to database" );
86
137
} else {
@@ -95,10 +146,40 @@ public void onDatabase() {
95
146
logger .error ("Failed to close database" , e );
96
147
}
97
148
}));
98
-
99
149
}
100
150
101
151
public static DSLContext getContext () {
102
152
return context ;
103
153
}
154
+
155
+ public synchronized void checkIfAskThreadTimeNeedsToBeRest (JDA jda ) {
156
+ jda .getGuilds ().forEach (c -> {
157
+ List <Instant > oldTimeInstant = getAskTimeStamps (c .getId ());
158
+ // need to check if between 24 hours since last time asked
159
+ oldTimeInstant .forEach (i -> {
160
+ if (i .isAfter (Instant .now ().minusSeconds (86400 ))) {
161
+ deleteAskDatabaseWithTime (i , c .getId ());
162
+ }
163
+ });
164
+ });
165
+ }
166
+
167
+ public synchronized void checkIfAskHelpThreadArchived (Guild guild ) {
168
+ TextChannel channel = guild .getTextChannelById (BotConfig .getActiveQuestionChannelId ());
169
+ ThreadChannelPaginationAction threadChannelPaginationAction =
170
+ channel != null ? channel .retrieveArchivedPublicThreadChannels () : null ;
171
+
172
+ if (threadChannelPaginationAction == null ) {
173
+ logger .debug ("No archived thread channels found" );
174
+ return ;
175
+ }
176
+
177
+ List <ThreadChannel > archivedThreadChannels = threadChannelPaginationAction .complete ();
178
+
179
+ for (ThreadChannel c : archivedThreadChannels ) {
180
+ String name = c .getName ();
181
+ String category = name .substring (1 , name .indexOf ("]" )).toLowerCase ();
182
+ updateActiveQuestions (c , AskThreadStatus .CLOSED , category );
183
+ }
184
+ }
104
185
}
0 commit comments