Skip to content

Commit

Permalink
Refactored the DailyChannelHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
beanbeanjuice committed Jun 20, 2024
1 parent 13e0d73 commit 21d0e84
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

/**
* A {@link CustomGuild} that contains {@link net.dv8tion.jda.api.entities.Guild Guild} information.
Expand Down Expand Up @@ -162,15 +159,11 @@ public Boolean setVentingChannelID(@NotNull String ventingChannelID) {
return false;
}

/**
* @return The daily {@link TextChannel} for the {@link Guild}. Null if does not exist.
*/
@Nullable
public TextChannel getDailyChannel() {
public Optional<TextChannel> getDailyChannel() {
try {
return GuildHandler.getGuild(guildID).getTextChannelById(customChannelIDs.get(CustomChannel.DAILY));
return Optional.ofNullable(GuildHandler.getGuild(guildID).getTextChannelById(customChannelIDs.get(CustomChannel.DAILY)));
} catch (NullPointerException e) {
return null;
return Optional.empty();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,29 @@ public static void start() {
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
GuildHandler.getGuilds().forEach((guildID, customGuild) -> {
TextChannel dailyChannel = customGuild.getDailyChannel();

// If the channel does exist.
if (dailyChannel != null) {

try {

// Creates a copy of the channel.
dailyChannel.createCopy().queue(channel -> {

// If the channel was successfully changed, then delete it. Else, delete the copied channel.
if (GuildHandler.getCustomGuild(guildID).setDailyChannelID(channel.getId())) {
dailyChannel.delete().queue();
} else {
channel.delete().queue();
}
});
} catch (PermissionException e) {
dailyChannel.sendMessageEmbeds(Helper.errorEmbed(
"Missing Permission",
e.getMessage()
)).queue();
}
}
});
GuildHandler.getGuilds().forEach(
(guildID, customGuild) ->
customGuild.getDailyChannel().ifPresent((dailyChannel) ->
resetChannel(guildID, dailyChannel)));
}
}, today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));
}

private static void resetChannel(final String guildID, final TextChannel dailyChannel) {
try {
// Creates a copy of the channel.
dailyChannel.createCopy().queue(channel -> {

// If the channel was successfully changed, then delete it. Else, delete the copied channel.
if (GuildHandler.getCustomGuild(guildID).setDailyChannelID(channel.getId())) dailyChannel.delete().queue();
else channel.delete().queue();
});
} catch (PermissionException e) {
dailyChannel.sendMessageEmbeds(Helper.errorEmbed(
"Missing Permission",
e.getMessage()
)).queue();
}
}

}

0 comments on commit 21d0e84

Please sign in to comment.