From 8eab2c0899a75f6dc3f03cf5a64f4d59cc9322ae Mon Sep 17 00:00:00 2001 From: FerroEduardo <47820549+FerroEduardo@users.noreply.github.com> Date: Thu, 26 Oct 2023 00:17:27 -0300 Subject: [PATCH] fix: setup spring components and set static fields --- src/main/java/com/softawii/capivara/Main.java | 19 ++++------------ .../softawii/capivara/core/DroneManager.java | 5 +---- .../capivara/listeners/EchoGroup.java | 22 +++++++++++++------ .../capivara/listeners/VoiceGroup.java | 17 ++++++++++++-- 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/softawii/capivara/Main.java b/src/main/java/com/softawii/capivara/Main.java index bd0cec4..33feb53 100644 --- a/src/main/java/com/softawii/capivara/Main.java +++ b/src/main/java/com/softawii/capivara/Main.java @@ -1,10 +1,5 @@ package com.softawii.capivara; -import com.softawii.capivara.core.DroneManager; -import com.softawii.capivara.core.EmbedManager; -import com.softawii.capivara.core.VoiceManager; -import com.softawii.capivara.listeners.EchoGroup; -import com.softawii.capivara.listeners.VoiceGroup.Dynamic; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Activity; import org.apache.logging.log4j.LogManager; @@ -17,19 +12,13 @@ @SpringBootApplication public class Main { - private static final Logger LOGGER = LogManager.getLogger(Main.class); - public static ConfigurableApplicationContext context; + private static final Logger LOGGER = LogManager.getLogger(Main.class); public static void main(String[] args) { - context = SpringApplication.run(Main.class, args); - JDA jda = context.getBean(JDA.class); - BuildProperties buildProperties = context.getBean(BuildProperties.class); + ConfigurableApplicationContext context = SpringApplication.run(Main.class, args); + JDA jda = context.getBean(JDA.class); + BuildProperties buildProperties = context.getBean(BuildProperties.class); jda.getPresence().setPresence(Activity.of(Activity.ActivityType.PLAYING, buildProperties.getVersion()), true); LOGGER.info(buildProperties.getVersion() + " Bot is ready as " + jda.getSelfUser().getName()); - - // Beans - EchoGroup.embedManager = context.getBean(EmbedManager.class); - Dynamic.voiceManager = context.getBean(VoiceManager.class); - Dynamic.droneManager = context.getBean(DroneManager.class); } } diff --git a/src/main/java/com/softawii/capivara/core/DroneManager.java b/src/main/java/com/softawii/capivara/core/DroneManager.java index 2e773b4..db89d80 100644 --- a/src/main/java/com/softawii/capivara/core/DroneManager.java +++ b/src/main/java/com/softawii/capivara/core/DroneManager.java @@ -45,7 +45,6 @@ public class DroneManager { private final Logger LOGGER = LogManager.getLogger(VoiceManager.class); private final VoiceDroneService voiceDroneService; private final VoiceHiveService voiceHiveService; - private final VoiceManager voiceManager; private final String renameDrone = "drone-manager-rename"; private final String limitDrone = "drone-manager-limit"; @@ -54,11 +53,9 @@ public class DroneManager { private final Pattern digdinRegex; - - public DroneManager(VoiceDroneService voiceDroneService, VoiceHiveService voiceHiveService, VoiceManager voiceManager) { + public DroneManager(VoiceDroneService voiceDroneService, VoiceHiveService voiceHiveService) { this.voiceDroneService = voiceDroneService; this.voiceHiveService = voiceHiveService; - this.voiceManager = voiceManager; this.digdinRegex = Pattern.compile("(?\\w+mente).+(Digdin)"); } diff --git a/src/main/java/com/softawii/capivara/listeners/EchoGroup.java b/src/main/java/com/softawii/capivara/listeners/EchoGroup.java index 1702ec8..b90dea4 100644 --- a/src/main/java/com/softawii/capivara/listeners/EchoGroup.java +++ b/src/main/java/com/softawii/capivara/listeners/EchoGroup.java @@ -1,7 +1,6 @@ package com.softawii.capivara.listeners; -import com.softawii.capivara.Main; import com.softawii.capivara.core.EmbedManager; import com.softawii.capivara.exceptions.FieldLengthException; import com.softawii.capivara.exceptions.KeyNotFoundException; @@ -30,6 +29,8 @@ import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; import java.awt.*; import java.util.ArrayList; @@ -37,6 +38,7 @@ import java.util.Map; @IGroup(name = "echo", description = "Echo Group") +@Component public class EchoGroup { public static final String buttonEditMessage = "echo-edit-message"; @@ -68,7 +70,18 @@ public class EchoGroup { //endregion private static final Logger LOGGER = LogManager.getLogger(EchoGroup.class); - public static EmbedManager embedManager; + private static EmbedManager embedManager; + private static Curupira curupira; + + @Autowired + public void setCurupira(Curupira curupira) { + EchoGroup.curupira = curupira; + } + + @Autowired + public void setEmbedManager(EmbedManager embedManager) { + EchoGroup.embedManager = embedManager; + } @ICommand(name = "echo", description = "Quer enviar uma mensagem para o canal como se fosse o bot? Um anĂșncio? Pode me usar!", @@ -182,7 +195,6 @@ public static void title(ButtonInteractionEvent event) { } // Generating Embed Model - Curupira curupira = Main.context.getBean(Curupira.class); Modal.Builder modal = curupira.getModal(modalTitle); modal.setId(modal.getId() + ":" + id); @@ -247,7 +259,6 @@ public static void image(ButtonInteractionEvent event) { } // Generating Embed Model - Curupira curupira = Main.context.getBean(Curupira.class); Modal.Builder modal = curupira.getModal(modalImage); modal.setId(modal.getId() + ":" + id); @@ -304,7 +315,6 @@ public static void newField(ButtonInteractionEvent event) { } // Generating Embed Model - Curupira curupira = Main.context.getBean(Curupira.class); Modal.Builder modal = curupira.getModal(modalNewField); modal.setId(modal.getId() + ":" + id); @@ -386,7 +396,6 @@ public static void editFieldMenu(StringSelectInteractionEvent event) { return; } - Curupira curupira = Main.context.getBean(Curupira.class); Modal.Builder modal = curupira.getModal(modalEditField); modal.setId(modal.getId() + ":" + id + ":" + index); @@ -481,7 +490,6 @@ public static void removeFieldMenu(StringSelectInteractionEvent event) { @IButton(id = buttonEditMessage) public static void editMessage(ButtonInteractionEvent event) { String id = event.getComponentId().split(":")[1]; - Curupira curupira = Main.context.getBean(Curupira.class); Modal.Builder builder = curupira.getModal(modalEditMessage).setId(modalEditMessage + ":" + id); event.replyModal(builder.build()).queue(); } diff --git a/src/main/java/com/softawii/capivara/listeners/VoiceGroup.java b/src/main/java/com/softawii/capivara/listeners/VoiceGroup.java index 23999c6..ce9797d 100644 --- a/src/main/java/com/softawii/capivara/listeners/VoiceGroup.java +++ b/src/main/java/com/softawii/capivara/listeners/VoiceGroup.java @@ -27,6 +27,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; import java.awt.*; import java.util.Collections; @@ -139,6 +141,7 @@ public static void permanent(SlashCommandInteractionEvent event) { } @ISubGroup(name = "Dynamic", description = "Dynamic") + @Component public static class Dynamic extends ListenerAdapter { public static final long inviteDeadline = 1000L * 10L * 60L; // 600000 ms = 10 minutes @@ -150,8 +153,18 @@ public static class Dynamic extends ListenerAdapter { public static final String dronePublicPrivate = "voice-dynamic-drone-public-private"; public static final String dronePermTemp = "voice-dynamic-drone-permanent-temporary"; public static final String droneClaim = "voice-dynamic-drone-claim"; - public static VoiceManager voiceManager; - public static DroneManager droneManager; + private static VoiceManager voiceManager; + private static DroneManager droneManager; + + @Autowired + public void setVoiceManager(VoiceManager voiceManager) { + Dynamic.voiceManager = voiceManager; + } + + @Autowired + public void setDroneManager(DroneManager droneManager) { + Dynamic.droneManager = droneManager; + } // endregion