From 23e4228489916820a7a1e189c3ad510f5595bc32 Mon Sep 17 00:00:00 2001 From: Sky Date: Sun, 20 Oct 2024 14:29:49 +0200 Subject: [PATCH] :bug: Fixed a lot of wrong-thread execution bugs --- build.gradle | 2 +- .../disky/api/events/EventListener.java | 6 +++++ .../java/info/itsthesky/disky/core/Bot.java | 4 ++++ .../elements/properties/DiscordName.java | 7 ++---- .../elements/properties/DynamicElements.java | 16 ++++++------- .../properties/profile/ProfileBanner.java | 7 ++---- .../structures/slash/BotReadyWaiter.java | 23 +++++++++++++++++++ .../structures/slash/StructSlashCommand.java | 18 +++++++-------- .../slash/models/ParsedCommand.java | 9 ++++++++ 9 files changed, 63 insertions(+), 29 deletions(-) create mode 100644 src/main/java/info/itsthesky/disky/elements/structures/slash/BotReadyWaiter.java diff --git a/build.gradle b/build.gradle index a29427d2..4caf4dc7 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ group = 'info.itsthesky' // Semantic Versioning def major = '4' def minor = '20' -def patch = '0' +def patch = '1' def channel = '' def channelVersion = '' diff --git a/src/main/java/info/itsthesky/disky/api/events/EventListener.java b/src/main/java/info/itsthesky/disky/api/events/EventListener.java index d80c23ea..ecdd1539 100644 --- a/src/main/java/info/itsthesky/disky/api/events/EventListener.java +++ b/src/main/java/info/itsthesky/disky/api/events/EventListener.java @@ -1,6 +1,7 @@ package info.itsthesky.disky.api.events; import info.itsthesky.disky.DiSky; +import info.itsthesky.disky.core.Bot; import info.itsthesky.disky.managers.BotManager; import net.dv8tion.jda.api.audit.ActionType; import net.dv8tion.jda.api.events.GenericEvent; @@ -55,6 +56,11 @@ public static void removeListener(EventListener listener) { DiSky.getManager().execute(bot -> bot.getInstance().removeEventListener(listener)); } + public static void registerAll(Bot bot) { + listeners.forEach(listener -> bot.getInstance().removeEventListener(listener)); + listeners.forEach(listener -> bot.getInstance().addEventListener(listener)); + } + @Override public void onGuildAuditLogEntryCreate(GuildAuditLogEntryCreateEvent event) { DiSky.debug("received log event " + event.getEntry().getType() + " by DiSky."); diff --git a/src/main/java/info/itsthesky/disky/core/Bot.java b/src/main/java/info/itsthesky/disky/core/Bot.java index 85ee75b7..9d857705 100644 --- a/src/main/java/info/itsthesky/disky/core/Bot.java +++ b/src/main/java/info/itsthesky/disky/core/Bot.java @@ -4,7 +4,9 @@ import ch.njol.skript.util.Timespan; import info.itsthesky.disky.BotApplication; import info.itsthesky.disky.DiSky; +import info.itsthesky.disky.api.events.EventListener; import info.itsthesky.disky.api.events.SimpleDiSkyEvent; +import info.itsthesky.disky.elements.structures.slash.BotReadyWaiter; import info.itsthesky.disky.elements.structures.slash.SlashManager; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; @@ -48,6 +50,8 @@ public Bot(String name, JDA instance, BotOptions options, @Nullable BotApplicati this.options = options; this.slashManager = SlashManager.getManager(this); + BotReadyWaiter.onBotLoaded(this); + EventListener.registerAll(this); } public String getName() { diff --git a/src/main/java/info/itsthesky/disky/elements/properties/DiscordName.java b/src/main/java/info/itsthesky/disky/elements/properties/DiscordName.java index 512f80de..86306e78 100644 --- a/src/main/java/info/itsthesky/disky/elements/properties/DiscordName.java +++ b/src/main/java/info/itsthesky/disky/elements/properties/DiscordName.java @@ -85,11 +85,8 @@ private void change(Event event, Object[] delta, Changer.ChangeMode mode, boolea if (action != null) { try { - if (async) { - action.queue(); - } else { - action.complete(); - } + if (async) action.complete(); + else action.queue(); } catch (Exception ex) { DiSkyRuntimeHandler.error(ex, node); } diff --git a/src/main/java/info/itsthesky/disky/elements/properties/DynamicElements.java b/src/main/java/info/itsthesky/disky/elements/properties/DynamicElements.java index 2373b900..f3fbe275 100644 --- a/src/main/java/info/itsthesky/disky/elements/properties/DynamicElements.java +++ b/src/main/java/info/itsthesky/disky/elements/properties/DynamicElements.java @@ -41,8 +41,8 @@ public static void registerThreadProperties() { ? entity.getManager().setLocked(value) : entity.getManager().setLocked(false); - if (async) rest.queue(); - else rest.complete(); + if (async) rest.complete(); + else rest.queue(); } ); @@ -54,8 +54,8 @@ public static void registerThreadProperties() { ? entity.getManager().setArchived(value) : entity.getManager().setArchived(false); - if (async) rest.queue(); - else rest.complete(); + if (async) rest.complete(); + else rest.queue(); } ); @@ -67,8 +67,8 @@ public static void registerThreadProperties() { ? entity.getManager().setPinned(value) : entity.getManager().setPinned(false); - if (async) rest.queue(); - else rest.complete(); + if (async) rest.complete(); + else rest.queue(); } ); @@ -80,8 +80,8 @@ public static void registerThreadProperties() { ? entity.getManager().setInvitable(value) : entity.getManager().setInvitable(false); - if (async) rest.queue(); - else rest.complete(); + if (async) rest.complete(); + else rest.queue(); } ); diff --git a/src/main/java/info/itsthesky/disky/elements/properties/profile/ProfileBanner.java b/src/main/java/info/itsthesky/disky/elements/properties/profile/ProfileBanner.java index 8dd5d58b..10eaab10 100644 --- a/src/main/java/info/itsthesky/disky/elements/properties/profile/ProfileBanner.java +++ b/src/main/java/info/itsthesky/disky/elements/properties/profile/ProfileBanner.java @@ -99,11 +99,8 @@ public void change(Event event, Object[] delta, Changer.ChangeMode mode, boolean final AccountManager manager = ((Bot) entity).getInstance().getSelfUser().getManager(); final RestAction action = manager.setBanner(banner); try { - if (async) { - action.queue(); - } else { - action.complete(); - } + if (async) action.complete(); + else action.queue(); } catch (Exception ex) { DiSkyRuntimeHandler.error(ex, node); } diff --git a/src/main/java/info/itsthesky/disky/elements/structures/slash/BotReadyWaiter.java b/src/main/java/info/itsthesky/disky/elements/structures/slash/BotReadyWaiter.java new file mode 100644 index 00000000..9ca6cf3a --- /dev/null +++ b/src/main/java/info/itsthesky/disky/elements/structures/slash/BotReadyWaiter.java @@ -0,0 +1,23 @@ +package info.itsthesky.disky.elements.structures.slash; + +import info.itsthesky.disky.core.Bot; +import info.itsthesky.disky.elements.structures.slash.models.ParsedCommand; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public final class BotReadyWaiter { + + public final static Map> WaitingCommands = new HashMap<>(); + + public static void onBotLoaded(Bot bot) { + final var commands = WaitingCommands.get(bot.getName()); + if (commands == null || commands.isEmpty()) + return; + + final var slashManager = bot.getSlashManager(); + commands.forEach(slashManager::registerCommand); + } + +} diff --git a/src/main/java/info/itsthesky/disky/elements/structures/slash/StructSlashCommand.java b/src/main/java/info/itsthesky/disky/elements/structures/slash/StructSlashCommand.java index f9352b9d..db367e60 100644 --- a/src/main/java/info/itsthesky/disky/elements/structures/slash/StructSlashCommand.java +++ b/src/main/java/info/itsthesky/disky/elements/structures/slash/StructSlashCommand.java @@ -200,7 +200,7 @@ public boolean load() { DiSky.debug("Group: " + (parsedCommand.getGroup() == null ? "None" : parsedCommand.getGroup().getName())); DiSky.debug("------------------- Places -------------------"); - DiSky.debug("Bot: " + parsedCommand.getBot().getName()); + DiSky.debug("Pre-bot (name): " + parsedCommand.getRawBot()); for (String guild : parsedCommand.getGuilds()) DiSky.debug("- Guild: " + guild); @@ -228,9 +228,13 @@ public boolean load() { DiSky.debug("------------------- End -------------------"); //endregion - parsedCommand.getBot().getSlashManager().registerCommand(parsedCommand); - REMOVED_COMMANDS.removeIf(info -> info.getCommand().equals(parsedCommand.getName())); + final var bot = DiSky.getManager().getBotByName(parsedCommand.getRawBot()); + if (bot == null) + BotReadyWaiter.WaitingCommands.computeIfAbsent(parsedCommand.getRawBot(), k -> new ArrayList<>()).add(parsedCommand); + else + bot.getSlashManager().registerCommand(parsedCommand); + REMOVED_COMMANDS.removeIf(info -> info.getCommand().equals(parsedCommand.getName())); return true; } @@ -480,13 +484,7 @@ public boolean parsePlaces() { return false; } - final Bot bot = DiSky.getManager().getBotByName(rawBot); - if (bot == null) { - getParser().setNode(structure); - Skript.error("Invalid bot name: " + rawBot); - return false; - } - parsedCommand.setBot(bot); + parsedCommand.setRawBot(rawBot); if (!rawGuilds.isEmpty()) { final String[] guildIds = rawGuilds.split(LIST.pattern()); diff --git a/src/main/java/info/itsthesky/disky/elements/structures/slash/models/ParsedCommand.java b/src/main/java/info/itsthesky/disky/elements/structures/slash/models/ParsedCommand.java index 72418ff8..13ef297e 100644 --- a/src/main/java/info/itsthesky/disky/elements/structures/slash/models/ParsedCommand.java +++ b/src/main/java/info/itsthesky/disky/elements/structures/slash/models/ParsedCommand.java @@ -27,6 +27,7 @@ public class ParsedCommand { private List enabledFor = new ArrayList<>(); private boolean disabledByDefault = false; + private String rawBot; // it's bot's name private Bot bot; private List guilds = new ArrayList<>(); @@ -126,6 +127,14 @@ public void setTrigger(Trigger trigger) { this.trigger = trigger; } + public String getRawBot() { + return rawBot; + } + + public void setRawBot(String rawBot) { + this.rawBot = rawBot; + } + public long getCooldown() { return cooldown; }