move util to service
This commit is contained in:
@@ -20,7 +20,7 @@ import wtf.beatrice.hidekobot.runnables.HeartBeatTask;
|
|||||||
import wtf.beatrice.hidekobot.runnables.RandomOrgSeedTask;
|
import wtf.beatrice.hidekobot.runnables.RandomOrgSeedTask;
|
||||||
import wtf.beatrice.hidekobot.runnables.StatusUpdateTask;
|
import wtf.beatrice.hidekobot.runnables.StatusUpdateTask;
|
||||||
import wtf.beatrice.hidekobot.services.DatabaseService;
|
import wtf.beatrice.hidekobot.services.DatabaseService;
|
||||||
import wtf.beatrice.hidekobot.util.CommandUtil;
|
import wtf.beatrice.hidekobot.services.CommandService;
|
||||||
import wtf.beatrice.hidekobot.util.FormatUtil;
|
import wtf.beatrice.hidekobot.util.FormatUtil;
|
||||||
import wtf.beatrice.hidekobot.util.RandomUtil;
|
import wtf.beatrice.hidekobot.util.RandomUtil;
|
||||||
import wtf.beatrice.hidekobot.util.Services;
|
import wtf.beatrice.hidekobot.util.Services;
|
||||||
@@ -70,10 +70,10 @@ public class HidekoBot
|
|||||||
|
|
||||||
ConfigurableApplicationContext context = SpringApplication.run(HidekoBot.class, args);
|
ConfigurableApplicationContext context = SpringApplication.run(HidekoBot.class, args);
|
||||||
|
|
||||||
CommandUtil commandUtil = context.getBean(CommandUtil.class);
|
CommandService commandService = context.getBean(CommandService.class);
|
||||||
DatabaseService databaseService = context.getBean(DatabaseService.class);
|
DatabaseService databaseService = context.getBean(DatabaseService.class);
|
||||||
Services services = new wtf.beatrice.hidekobot.util.Services(
|
Services services = new wtf.beatrice.hidekobot.util.Services(
|
||||||
commandUtil,
|
commandService,
|
||||||
databaseService
|
databaseService
|
||||||
);
|
);
|
||||||
Cache.setServices(services);
|
Cache.setServices(services);
|
||||||
@@ -206,7 +206,7 @@ public class HidekoBot
|
|||||||
final boolean finalForceUpdateCommands = forceUpdateCommands;
|
final boolean finalForceUpdateCommands = forceUpdateCommands;
|
||||||
try (ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor())
|
try (ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor())
|
||||||
{
|
{
|
||||||
executor.schedule(() -> commandUtil.updateSlashCommands(finalForceUpdateCommands),
|
executor.schedule(() -> commandService.updateSlashCommands(finalForceUpdateCommands),
|
||||||
1, TimeUnit.SECONDS);
|
1, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +215,7 @@ public class HidekoBot
|
|||||||
|
|
||||||
// start scheduled runnables
|
// start scheduled runnables
|
||||||
ScheduledExecutorService scheduler = Cache.getTaskScheduler();
|
ScheduledExecutorService scheduler = Cache.getTaskScheduler();
|
||||||
ExpiredMessageTask expiredMessageTask = new ExpiredMessageTask(services.databaseService(), services.commandUtil());
|
ExpiredMessageTask expiredMessageTask = new ExpiredMessageTask(services.databaseService(), services.commandService());
|
||||||
scheduler.scheduleAtFixedRate(expiredMessageTask, 5L, 5L, TimeUnit.SECONDS); //every 5 seconds
|
scheduler.scheduleAtFixedRate(expiredMessageTask, 5L, 5L, TimeUnit.SECONDS); //every 5 seconds
|
||||||
|
|
||||||
HeartBeatTask heartBeatTask = new HeartBeatTask();
|
HeartBeatTask heartBeatTask = new HeartBeatTask();
|
||||||
|
@@ -259,7 +259,7 @@ public class Trivia
|
|||||||
}
|
}
|
||||||
|
|
||||||
// todo: we shouldn't use this method, since it messes with the database... look at coin reflip
|
// todo: we shouldn't use this method, since it messes with the database... look at coin reflip
|
||||||
Cache.getServices().commandUtil().disableExpired(event.getMessageId());
|
Cache.getServices().commandService().disableExpired(event.getMessageId());
|
||||||
|
|
||||||
SelectOption pickedOption = event.getInteraction().getSelectedOptions().get(0);
|
SelectOption pickedOption = event.getInteraction().getSelectedOptions().get(0);
|
||||||
String categoryName = pickedOption.getLabel();
|
String categoryName = pickedOption.getLabel();
|
||||||
@@ -293,7 +293,7 @@ public class Trivia
|
|||||||
|
|
||||||
|
|
||||||
TriviaTask triviaTask = new TriviaTask(author, channel, category,
|
TriviaTask triviaTask = new TriviaTask(author, channel, category,
|
||||||
Cache.getServices().databaseService(), Cache.getServices().commandUtil());
|
Cache.getServices().databaseService(), Cache.getServices().commandService());
|
||||||
ScheduledFuture<?> future =
|
ScheduledFuture<?> future =
|
||||||
Cache.getTaskScheduler().scheduleAtFixedRate(triviaTask,
|
Cache.getTaskScheduler().scheduleAtFixedRate(triviaTask,
|
||||||
0,
|
0,
|
||||||
|
@@ -8,18 +8,18 @@ import org.springframework.stereotype.Component;
|
|||||||
import wtf.beatrice.hidekobot.commands.base.CoinFlip;
|
import wtf.beatrice.hidekobot.commands.base.CoinFlip;
|
||||||
import wtf.beatrice.hidekobot.commands.base.Trivia;
|
import wtf.beatrice.hidekobot.commands.base.Trivia;
|
||||||
import wtf.beatrice.hidekobot.commands.base.UrbanDictionary;
|
import wtf.beatrice.hidekobot.commands.base.UrbanDictionary;
|
||||||
import wtf.beatrice.hidekobot.util.CommandUtil;
|
import wtf.beatrice.hidekobot.services.CommandService;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class ButtonInteractionListener extends ListenerAdapter
|
public class ButtonInteractionListener extends ListenerAdapter
|
||||||
{
|
{
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ButtonInteractionListener.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(ButtonInteractionListener.class);
|
||||||
|
|
||||||
private final CommandUtil commandUtil;
|
private final CommandService commandService;
|
||||||
|
|
||||||
public ButtonInteractionListener(CommandUtil commandUtil)
|
public ButtonInteractionListener(CommandService commandService)
|
||||||
{
|
{
|
||||||
this.commandUtil = commandUtil;
|
this.commandService = commandService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -33,7 +33,7 @@ public class ButtonInteractionListener extends ListenerAdapter
|
|||||||
case "coinflip_reflip" -> CoinFlip.buttonReFlip(event);
|
case "coinflip_reflip" -> CoinFlip.buttonReFlip(event);
|
||||||
|
|
||||||
// generic dismiss button
|
// generic dismiss button
|
||||||
case "generic_dismiss" -> commandUtil.delete(event);
|
case "generic_dismiss" -> commandService.deleteUserLinkedMessage(event);
|
||||||
|
|
||||||
// urban dictionary navigation
|
// urban dictionary navigation
|
||||||
case "urban_nextpage" -> UrbanDictionary.changePage(event, UrbanDictionary.ChangeType.NEXT);
|
case "urban_nextpage" -> UrbanDictionary.changePage(event, UrbanDictionary.ChangeType.NEXT);
|
||||||
|
@@ -4,7 +4,7 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import wtf.beatrice.hidekobot.Cache;
|
import wtf.beatrice.hidekobot.Cache;
|
||||||
import wtf.beatrice.hidekobot.services.DatabaseService;
|
import wtf.beatrice.hidekobot.services.DatabaseService;
|
||||||
import wtf.beatrice.hidekobot.util.CommandUtil;
|
import wtf.beatrice.hidekobot.services.CommandService;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
@@ -14,17 +14,17 @@ public class ExpiredMessageTask implements Runnable
|
|||||||
{
|
{
|
||||||
|
|
||||||
private final DatabaseService databaseService;
|
private final DatabaseService databaseService;
|
||||||
private final CommandUtil commandUtil;
|
private final CommandService commandService;
|
||||||
|
|
||||||
private final DateTimeFormatter formatter;
|
private final DateTimeFormatter formatter;
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(ExpiredMessageTask.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(ExpiredMessageTask.class);
|
||||||
|
|
||||||
|
|
||||||
public ExpiredMessageTask(DatabaseService databaseService,
|
public ExpiredMessageTask(DatabaseService databaseService,
|
||||||
CommandUtil commandUtil)
|
CommandService commandService)
|
||||||
{
|
{
|
||||||
this.databaseService = databaseService;
|
this.databaseService = databaseService;
|
||||||
this.commandUtil = commandUtil;
|
this.commandService = commandService;
|
||||||
String format = Cache.getExpiryTimestampFormat();
|
String format = Cache.getExpiryTimestampFormat();
|
||||||
formatter = DateTimeFormatter.ofPattern(format);
|
formatter = DateTimeFormatter.ofPattern(format);
|
||||||
}
|
}
|
||||||
@@ -58,7 +58,7 @@ public class ExpiredMessageTask implements Runnable
|
|||||||
if (now.isAfter(expiryDate))
|
if (now.isAfter(expiryDate))
|
||||||
{
|
{
|
||||||
if (Cache.isVerbose()) LOGGER.info("expired: {}", messageId);
|
if (Cache.isVerbose()) LOGGER.info("expired: {}", messageId);
|
||||||
commandUtil.disableExpired(messageId);
|
commandService.disableExpired(messageId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -14,7 +14,7 @@ import wtf.beatrice.hidekobot.objects.fun.TriviaCategory;
|
|||||||
import wtf.beatrice.hidekobot.objects.fun.TriviaQuestion;
|
import wtf.beatrice.hidekobot.objects.fun.TriviaQuestion;
|
||||||
import wtf.beatrice.hidekobot.objects.fun.TriviaScore;
|
import wtf.beatrice.hidekobot.objects.fun.TriviaScore;
|
||||||
import wtf.beatrice.hidekobot.services.DatabaseService;
|
import wtf.beatrice.hidekobot.services.DatabaseService;
|
||||||
import wtf.beatrice.hidekobot.util.CommandUtil;
|
import wtf.beatrice.hidekobot.services.CommandService;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
@@ -23,7 +23,7 @@ public class TriviaTask implements Runnable
|
|||||||
{
|
{
|
||||||
|
|
||||||
private final DatabaseService databaseService;
|
private final DatabaseService databaseService;
|
||||||
private final CommandUtil commandUtil;
|
private final CommandService commandService;
|
||||||
|
|
||||||
private final User author;
|
private final User author;
|
||||||
private final MessageChannel channel;
|
private final MessageChannel channel;
|
||||||
@@ -42,13 +42,13 @@ public class TriviaTask implements Runnable
|
|||||||
MessageChannel channel,
|
MessageChannel channel,
|
||||||
TriviaCategory category,
|
TriviaCategory category,
|
||||||
DatabaseService databaseService,
|
DatabaseService databaseService,
|
||||||
CommandUtil commandUtil)
|
CommandService commandService)
|
||||||
{
|
{
|
||||||
this.author = author;
|
this.author = author;
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
this.category = category;
|
this.category = category;
|
||||||
this.databaseService = databaseService;
|
this.databaseService = databaseService;
|
||||||
this.commandUtil = commandUtil;
|
this.commandService = commandService;
|
||||||
|
|
||||||
triviaJson = Trivia.fetchJson(Trivia.getTriviaLink(category.categoryId()));
|
triviaJson = Trivia.fetchJson(Trivia.getTriviaLink(category.categoryId()));
|
||||||
questions = Trivia.parseQuestions(triviaJson); //todo: null check, rate limiting...
|
questions = Trivia.parseQuestions(triviaJson); //todo: null check, rate limiting...
|
||||||
@@ -66,7 +66,7 @@ public class TriviaTask implements Runnable
|
|||||||
if (previousMessage != null)
|
if (previousMessage != null)
|
||||||
{
|
{
|
||||||
// todo: we shouldn't use this method, since it messes with the database... look at coin reflip
|
// todo: we shouldn't use this method, since it messes with the database... look at coin reflip
|
||||||
commandUtil.disableExpired(previousMessage.getId());
|
commandService.disableExpired(previousMessage.getId());
|
||||||
|
|
||||||
String previousCorrectAnswer = questions.get(iteration - 1).correctAnswer();
|
String previousCorrectAnswer = questions.get(iteration - 1).correctAnswer();
|
||||||
|
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
package wtf.beatrice.hidekobot.util;
|
package wtf.beatrice.hidekobot.services;
|
||||||
|
|
||||||
|
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
@@ -19,20 +19,19 @@ import org.springframework.stereotype.Component;
|
|||||||
import wtf.beatrice.hidekobot.Cache;
|
import wtf.beatrice.hidekobot.Cache;
|
||||||
import wtf.beatrice.hidekobot.HidekoBot;
|
import wtf.beatrice.hidekobot.HidekoBot;
|
||||||
import wtf.beatrice.hidekobot.objects.commands.SlashCommand;
|
import wtf.beatrice.hidekobot.objects.commands.SlashCommand;
|
||||||
import wtf.beatrice.hidekobot.services.DatabaseService;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class CommandUtil
|
public class CommandService
|
||||||
{
|
{
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(CommandUtil.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(CommandService.class);
|
||||||
|
|
||||||
private final DatabaseService databaseService;
|
private final DatabaseService databaseService;
|
||||||
|
|
||||||
public CommandUtil(@Autowired DatabaseService databaseService)
|
public CommandService(@Autowired DatabaseService databaseService)
|
||||||
{
|
{
|
||||||
this.databaseService = databaseService;
|
this.databaseService = databaseService;
|
||||||
}
|
}
|
||||||
@@ -43,7 +42,7 @@ public class CommandUtil
|
|||||||
*
|
*
|
||||||
* @param event the button interaction event.
|
* @param event the button interaction event.
|
||||||
*/
|
*/
|
||||||
public void delete(ButtonInteractionEvent event)
|
public void deleteUserLinkedMessage(ButtonInteractionEvent event)
|
||||||
{
|
{
|
||||||
// check if the user interacting is the same one who ran the command
|
// check if the user interacting is the same one who ran the command
|
||||||
if (!databaseService.isUserTrackedFor(event.getUser().getId(), event.getMessageId()))
|
if (!databaseService.isUserTrackedFor(event.getUser().getId(), event.getMessageId()))
|
@@ -1,7 +1,8 @@
|
|||||||
package wtf.beatrice.hidekobot.util;
|
package wtf.beatrice.hidekobot.util;
|
||||||
|
|
||||||
|
import wtf.beatrice.hidekobot.services.CommandService;
|
||||||
import wtf.beatrice.hidekobot.services.DatabaseService;
|
import wtf.beatrice.hidekobot.services.DatabaseService;
|
||||||
|
|
||||||
public record Services(CommandUtil commandUtil, DatabaseService databaseService)
|
public record Services(CommandService commandService, DatabaseService databaseService)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user