1010import net .javadiscord .javabot2 .command .SlashCommandListener ;
1111import net .javadiscord .javabot2 .config .BotConfig ;
1212import net .javadiscord .javabot2 .db .DbHelper ;
13- import net .javadiscord .javabot2 .systems . moderation . ModerationService ;
13+ import net .javadiscord .javabot2 .tasks . ScheduledTasks ;
1414import org .javacord .api .DiscordApi ;
1515import org .javacord .api .DiscordApiBuilder ;
1616import org .javacord .api .entity .intent .Intent ;
17+ import org .quartz .SchedulerException ;
1718
1819import java .nio .file .Path ;
1920import java .time .ZoneOffset ;
2021import java .util .TimeZone ;
2122import java .util .concurrent .Executors ;
2223import java .util .concurrent .ScheduledExecutorService ;
23- import java .util .concurrent .TimeUnit ;
2424
2525/**
2626 * The main program entry point.
@@ -32,22 +32,6 @@ public class Bot {
3232 */
3333 public static HikariDataSource hikariDataSource ;
3434
35- /**
36- * A thread-safe MongoDB client that can be used to interact with MongoDB.
37- * @deprecated Use the relational data source for all future persistence
38- * needs; it promotes more organized code that's less prone to failures.
39- */
40- @ Deprecated
41- public static MongoClient mongoClient ;
42-
43- /**
44- * The single Mongo database where all bot data is stored.
45- * @deprecated Use the relational data source for all future persistence
46- * needs; it promotes more organized code that's less prone to failures.
47- */
48- @ Deprecated
49- public static MongoDatabase mongoDb ;
50-
5135 /**
5236 * The bot's configuration.
5337 */
@@ -81,7 +65,13 @@ public static void main(String[] args) {
8165 "commands/moderation.yaml"
8266 );
8367 api .addSlashCommandCreateListener (commandListener );
84- initScheduledTasks (api );
68+ try {
69+ ScheduledTasks .init (api );
70+ log .info ("Initialized scheduled tasks." );
71+ } catch (SchedulerException e ) {
72+ log .error ("Could not initialize all scheduled tasks." , e );
73+ api .disconnect ().join ();
74+ }
8575 }
8676
8777 /**
@@ -95,24 +85,15 @@ private static void initDataSources() {
9585 throw new IllegalStateException ("Missing required Discord bot token! Please edit config/systems.json to add it, then run again." );
9686 }
9787 hikariDataSource = DbHelper .initDataSource (config );
98- mongoDb = initMongoDatabase ();
9988 }
10089
90+ @ Deprecated
10191 private static MongoDatabase initMongoDatabase () {
102- mongoClient = new MongoClient (new MongoClientURI (config .getSystems ().getMongoDatabaseUrl ()));
92+ var mongoClient = new MongoClient (new MongoClientURI (config .getSystems ().getMongoDatabaseUrl ()));
10393 var db = mongoClient .getDatabase ("javabot" );
10494 var warnCollection = db .getCollection ("warn" );
10595 warnCollection .createIndex (Indexes .ascending ("userId" ), new IndexOptions ().unique (false ));
10696 warnCollection .createIndex (Indexes .descending ("createdAt" ), new IndexOptions ().unique (false ));
10797 return db ;
10898 }
109-
110- private static void initScheduledTasks (DiscordApi api ) {
111- // Regularly check for and unmute users whose mutes have expired.
112- asyncPool .scheduleAtFixedRate (() -> {
113- for (var server : api .getServers ()) {
114- new ModerationService (api , config .get (server ).getModeration ()).unmuteExpired ();
115- }
116- }, 1L , 1L , TimeUnit .MINUTES );
117- }
11899}
0 commit comments