diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/handler/guild/CustomGuild.java b/src/main/java/com/beanbeanjuice/cafebot/utility/handler/guild/CustomGuild.java index 9fc73a26..a59d78b1 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/handler/guild/CustomGuild.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/handler/guild/CustomGuild.java @@ -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. @@ -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 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(); } } diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/section/settings/DailyChannelHandler.java b/src/main/java/com/beanbeanjuice/cafebot/utility/section/settings/DailyChannelHandler.java index 452b656c..ab7fae24 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/section/settings/DailyChannelHandler.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/section/settings/DailyChannelHandler.java @@ -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(); + } + } + }