Skip to content

Commit

Permalink
🐛 Fixed a lot of wrong-thread execution bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed Oct 20, 2024
1 parent c3eabd0 commit 23e4228
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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.");
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/info/itsthesky/disky/core/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
);

Expand All @@ -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();
}
);

Expand All @@ -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();
}
);

Expand All @@ -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();
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, List<ParsedCommand>> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ParsedCommand {
private List<Permission> enabledFor = new ArrayList<>();
private boolean disabledByDefault = false;

private String rawBot; // it's bot's name
private Bot bot;
private List<String> guilds = new ArrayList<>();

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 23e4228

Please sign in to comment.