move util to service
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/promote/production Build is passing

This commit is contained in:
2025-09-05 01:35:15 +02:00
parent f80a49995b
commit 29f486566b
7 changed files with 29 additions and 29 deletions

View File

@@ -20,7 +20,7 @@ import wtf.beatrice.hidekobot.runnables.HeartBeatTask;
import wtf.beatrice.hidekobot.runnables.RandomOrgSeedTask;
import wtf.beatrice.hidekobot.runnables.StatusUpdateTask;
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.RandomUtil;
import wtf.beatrice.hidekobot.util.Services;
@@ -70,10 +70,10 @@ public class HidekoBot
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);
Services services = new wtf.beatrice.hidekobot.util.Services(
commandUtil,
commandService,
databaseService
);
Cache.setServices(services);
@@ -206,7 +206,7 @@ public class HidekoBot
final boolean finalForceUpdateCommands = forceUpdateCommands;
try (ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor())
{
executor.schedule(() -> commandUtil.updateSlashCommands(finalForceUpdateCommands),
executor.schedule(() -> commandService.updateSlashCommands(finalForceUpdateCommands),
1, TimeUnit.SECONDS);
}
@@ -215,7 +215,7 @@ public class HidekoBot
// start scheduled runnables
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
HeartBeatTask heartBeatTask = new HeartBeatTask();

View File

@@ -259,7 +259,7 @@ public class Trivia
}
// 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);
String categoryName = pickedOption.getLabel();
@@ -293,7 +293,7 @@ public class Trivia
TriviaTask triviaTask = new TriviaTask(author, channel, category,
Cache.getServices().databaseService(), Cache.getServices().commandUtil());
Cache.getServices().databaseService(), Cache.getServices().commandService());
ScheduledFuture<?> future =
Cache.getTaskScheduler().scheduleAtFixedRate(triviaTask,
0,

View File

@@ -8,18 +8,18 @@ import org.springframework.stereotype.Component;
import wtf.beatrice.hidekobot.commands.base.CoinFlip;
import wtf.beatrice.hidekobot.commands.base.Trivia;
import wtf.beatrice.hidekobot.commands.base.UrbanDictionary;
import wtf.beatrice.hidekobot.util.CommandUtil;
import wtf.beatrice.hidekobot.services.CommandService;
@Component
public class ButtonInteractionListener extends ListenerAdapter
{
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
@@ -33,7 +33,7 @@ public class ButtonInteractionListener extends ListenerAdapter
case "coinflip_reflip" -> CoinFlip.buttonReFlip(event);
// generic dismiss button
case "generic_dismiss" -> commandUtil.delete(event);
case "generic_dismiss" -> commandService.deleteUserLinkedMessage(event);
// urban dictionary navigation
case "urban_nextpage" -> UrbanDictionary.changePage(event, UrbanDictionary.ChangeType.NEXT);

View File

@@ -4,7 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import wtf.beatrice.hidekobot.Cache;
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.format.DateTimeFormatter;
@@ -14,17 +14,17 @@ public class ExpiredMessageTask implements Runnable
{
private final DatabaseService databaseService;
private final CommandUtil commandUtil;
private final CommandService commandService;
private final DateTimeFormatter formatter;
private static final Logger LOGGER = LoggerFactory.getLogger(ExpiredMessageTask.class);
public ExpiredMessageTask(DatabaseService databaseService,
CommandUtil commandUtil)
CommandService commandService)
{
this.databaseService = databaseService;
this.commandUtil = commandUtil;
this.commandService = commandService;
String format = Cache.getExpiryTimestampFormat();
formatter = DateTimeFormatter.ofPattern(format);
}
@@ -58,7 +58,7 @@ public class ExpiredMessageTask implements Runnable
if (now.isAfter(expiryDate))
{
if (Cache.isVerbose()) LOGGER.info("expired: {}", messageId);
commandUtil.disableExpired(messageId);
commandService.disableExpired(messageId);
}
}

View File

@@ -14,7 +14,7 @@ import wtf.beatrice.hidekobot.objects.fun.TriviaCategory;
import wtf.beatrice.hidekobot.objects.fun.TriviaQuestion;
import wtf.beatrice.hidekobot.objects.fun.TriviaScore;
import wtf.beatrice.hidekobot.services.DatabaseService;
import wtf.beatrice.hidekobot.util.CommandUtil;
import wtf.beatrice.hidekobot.services.CommandService;
import java.util.*;
import java.util.concurrent.ScheduledFuture;
@@ -23,7 +23,7 @@ public class TriviaTask implements Runnable
{
private final DatabaseService databaseService;
private final CommandUtil commandUtil;
private final CommandService commandService;
private final User author;
private final MessageChannel channel;
@@ -42,13 +42,13 @@ public class TriviaTask implements Runnable
MessageChannel channel,
TriviaCategory category,
DatabaseService databaseService,
CommandUtil commandUtil)
CommandService commandService)
{
this.author = author;
this.channel = channel;
this.category = category;
this.databaseService = databaseService;
this.commandUtil = commandUtil;
this.commandService = commandService;
triviaJson = Trivia.fetchJson(Trivia.getTriviaLink(category.categoryId()));
questions = Trivia.parseQuestions(triviaJson); //todo: null check, rate limiting...
@@ -66,7 +66,7 @@ public class TriviaTask implements Runnable
if (previousMessage != null)
{
// 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();

View File

@@ -1,4 +1,4 @@
package wtf.beatrice.hidekobot.util;
package wtf.beatrice.hidekobot.services;
import net.dv8tion.jda.api.JDA;
@@ -19,20 +19,19 @@ import org.springframework.stereotype.Component;
import wtf.beatrice.hidekobot.Cache;
import wtf.beatrice.hidekobot.HidekoBot;
import wtf.beatrice.hidekobot.objects.commands.SlashCommand;
import wtf.beatrice.hidekobot.services.DatabaseService;
import java.util.ArrayList;
import java.util.List;
@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;
public CommandUtil(@Autowired DatabaseService databaseService)
public CommandService(@Autowired DatabaseService databaseService)
{
this.databaseService = databaseService;
}
@@ -43,7 +42,7 @@ public class CommandUtil
*
* @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
if (!databaseService.isUserTrackedFor(event.getUser().getId(), event.getMessageId()))

View File

@@ -1,7 +1,8 @@
package wtf.beatrice.hidekobot.util;
import wtf.beatrice.hidekobot.services.CommandService;
import wtf.beatrice.hidekobot.services.DatabaseService;
public record Services(CommandUtil commandUtil, DatabaseService databaseService)
public record Services(CommandService commandService, DatabaseService databaseService)
{
}