Skip to content

Commit

Permalink
refactor: ♻️ refactor adaptors
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Jul 7, 2024
1 parent 5ad0012 commit 303831a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,44 @@ public DiscordAdaptor(ChatHub chatHub) {

@Override
public void start() {
if (config.isDiscordEnabled()) {
// logger
chatHub.getLogger().info("Discord enabled");

// build jda
jda = JDABuilder.createLight(config.getDiscordToken())
.enableIntents(
GatewayIntent.MESSAGE_CONTENT
)
.addEventListeners(new DiscordReceiver(chatHub))
.build();

// commands
jda.updateCommands().addCommands(
Commands
.slash("list", "List online players")
.setGuildOnly(true)
).queue();

// await jda ready
try {
jda.awaitReady();
} catch (InterruptedException e) {
chatHub.getLogger().error("Discord jda failed to start: " + e.getMessage());
}

// get channel
channel = jda.getTextChannelById(config.getDiscordChannelId());
// logger
chatHub.getLogger().info("Discord enabled");

// build jda
jda = JDABuilder.createLight(config.getDiscordToken())
.enableIntents(
GatewayIntent.MESSAGE_CONTENT
)
.addEventListeners(new DiscordReceiver(chatHub))
.build();

// commands
jda.updateCommands().addCommands(
Commands
.slash("list", "List online players")
.setGuildOnly(true)
).queue();

// await jda ready
try {
jda.awaitReady();
} catch (InterruptedException e) {
chatHub.getLogger().error("Discord jda failed to start: " + e.getMessage());
}

// get channel
channel = jda.getTextChannelById(config.getDiscordChannelId());
}

@Override
public void stop() {
if (config.isDiscordEnabled() && jda != null) {
if (jda != null) {
jda.shutdownNow();
}
}

@Override
public void sendPublicMessage(String message) {
if (config.isDiscordEnabled()) {
new Thread(() -> channel.sendMessage(message).queue()).start();
}
new Thread(() -> channel.sendMessage(message).queue()).start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,26 @@ public KookAdaptor(ChatHub chatHub) {

@Override
public void start() {
if (config.isKookEnabled()) {
chatHub.getLogger().info("Kook enabled");
kookReceiver.start();
if (config.getKookDaemonEnabled())
kookDaemon.start();
}
chatHub.getLogger().info("Kook enabled");
kookReceiver.start();
if (config.getKookDaemonEnabled())
kookDaemon.start();
}

@Override
public void stop() {
if (config.isKookEnabled()) {
kookReceiver.shutdown();
if (config.getKookDaemonEnabled())
kookDaemon.shutdown();
}
kookReceiver.shutdown();
if (config.getKookDaemonEnabled())
kookDaemon.shutdown();
}

@Override
public void restart() {
if (config.isKookEnabled()) {
kookReceiver.restart();
}
kookReceiver.restart();
}

@Override
public void sendPublicMessage(String message) {
if (config.isKookEnabled()) {
new Thread(() -> kookAPI.sendMessage(message)).start();
}
new Thread(() -> kookAPI.sendMessage(message)).start();
}
}

0 comments on commit 303831a

Please sign in to comment.