mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-25 14:06:24 +00:00
added bean configuration for event waiter to be used for paginator
fixed initial setup for server fixed command in a server configuration
This commit is contained in:
@@ -2,17 +2,38 @@ package dev.sheldan.abstracto.core.config;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
|
||||
import dev.sheldan.abstracto.core.service.BotService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
@Configuration
|
||||
public class CoreConfig {
|
||||
|
||||
@Autowired
|
||||
private BotService botService;
|
||||
|
||||
@Value("${abstracto.eventWaiter.threads}")
|
||||
private Integer threadCount;
|
||||
|
||||
@Bean
|
||||
public Gson gson() {
|
||||
return new GsonBuilder()
|
||||
.registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeAdapter())
|
||||
.setPrettyPrinting().create();
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public EventWaiter eventWaiter() {
|
||||
ScheduledExecutorService scheduledExecutorService =
|
||||
Executors.newScheduledThreadPool(threadCount);
|
||||
return new EventWaiter(scheduledExecutorService, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package dev.sheldan.abstracto.core.listener;
|
||||
|
||||
import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
|
||||
import dev.sheldan.abstracto.core.service.BotService;
|
||||
import dev.sheldan.abstracto.core.service.StartupServiceBean;
|
||||
import dev.sheldan.abstracto.scheduling.service.SchedulerService;
|
||||
import net.dv8tion.jda.api.events.ReadyEvent;
|
||||
@@ -22,11 +24,18 @@ public class ReadyListener extends ListenerAdapter {
|
||||
@Autowired
|
||||
private SchedulerService schedulerService;
|
||||
|
||||
@Autowired
|
||||
private EventWaiter eventWaiter;
|
||||
|
||||
@Autowired
|
||||
private BotService botService;
|
||||
|
||||
@Override
|
||||
public void onReady(@Nonnull ReadyEvent event) {
|
||||
if(synchronize){
|
||||
startup.synchronize();
|
||||
}
|
||||
botService.getInstance().addEventListener(eventWaiter);
|
||||
schedulerService.startScheduler();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package dev.sheldan.abstracto.core.listener;
|
||||
|
||||
import dev.sheldan.abstracto.core.models.database.AServer;
|
||||
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
|
||||
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class ServerJoinListener extends ListenerAdapter {
|
||||
|
||||
@Autowired
|
||||
private List<ServerConfigListener> configListeners;
|
||||
|
||||
@Autowired
|
||||
private ServerManagementService serverManagementService;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void onGuildJoin(@Nonnull GuildJoinEvent event) {
|
||||
AServer server = serverManagementService.loadOrCreate(event.getGuild().getIdLong());
|
||||
configListeners.forEach(serverConfigListener -> serverConfigListener.updateServerConfig(server));
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,6 @@ public class PaginatorServiceBean implements PaginatorService {
|
||||
public Paginator createPaginatorFromTemplate(String templateKey, Object model, EventWaiter waiter) {
|
||||
String embedConfig = templateService.renderTemplate(templateKey + "_paginator", model);
|
||||
PaginatorConfiguration configuration = gson.fromJson(embedConfig, PaginatorConfiguration.class);
|
||||
botService.getInstance().addEventListener(waiter);
|
||||
List<String> items = configuration.getItems();
|
||||
int itemsPerPage = findAppropriateCountPerPage(items);
|
||||
|
||||
@@ -41,9 +40,9 @@ public class PaginatorServiceBean implements PaginatorService {
|
||||
.setItems(configuration.getItems().toArray(new String[0]))
|
||||
.useNumberedItems(ObjectUtils.defaultIfNull(configuration.getUseNumberedItems(), false))
|
||||
.setEventWaiter(waiter)
|
||||
.waitOnSinglePage(true)
|
||||
.setTimeout(ObjectUtils.defaultIfNull(configuration.getTimeoutSeconds(), 120L), TimeUnit.SECONDS)
|
||||
.setFinalAction(message -> {
|
||||
botService.getInstance().removeEventListener(waiter);
|
||||
message.delete().queue();
|
||||
})
|
||||
.build();
|
||||
|
||||
@@ -3,4 +3,5 @@ abstracto.startup.synchronize=true
|
||||
abstracto.parameter.lowerBound=50
|
||||
|
||||
abstracto.features.core=true
|
||||
abstracto.prefix=!
|
||||
abstracto.prefix=!
|
||||
abstracto.eventWaiter.threads=10
|
||||
Reference in New Issue
Block a user