diff --git a/src/main/java/com/beanbeanjuice/cafebot/CafeBot.java b/src/main/java/com/beanbeanjuice/cafebot/CafeBot.java index 78faffcd..66c52fac 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/CafeBot.java +++ b/src/main/java/com/beanbeanjuice/cafebot/CafeBot.java @@ -19,6 +19,7 @@ import com.beanbeanjuice.cafebot.commands.interaction.*; import com.beanbeanjuice.cafebot.commands.moderation.ClearChatCommand; import com.beanbeanjuice.cafebot.commands.settings.AICommand; +import com.beanbeanjuice.cafebot.commands.settings.daily.DailyCommand; import com.beanbeanjuice.cafebot.commands.settings.goodbye.GoodbyeCommand; import com.beanbeanjuice.cafebot.commands.settings.update.UpdateCommand; import com.beanbeanjuice.cafebot.commands.settings.welcome.WelcomeCommand; @@ -27,6 +28,7 @@ import com.beanbeanjuice.cafebot.commands.twitch.TwitchCommand; import com.beanbeanjuice.cafebot.utility.api.GitHubVersionEndpointWrapper; import com.beanbeanjuice.cafebot.utility.commands.CommandHandler; +import com.beanbeanjuice.cafebot.utility.helper.DailyChannelHelper; import com.beanbeanjuice.cafebot.utility.helper.Helper; import com.beanbeanjuice.cafebot.utility.helper.UpdateCheckHelper; import com.beanbeanjuice.cafebot.utility.listeners.*; @@ -83,6 +85,9 @@ public class CafeBot { // Listeners @Getter private AIResponseListener aiResponseListener; + // Helpers + private DailyChannelHelper dailyChannelHelper; + // Handlers private CommandHandler commandHandler; @Getter private MenuHandler menuHandler; @@ -258,7 +263,8 @@ private void setupCommands() { new AICommand(this), new WelcomeCommand(this), new GoodbyeCommand(this), - new UpdateCommand(this) + new UpdateCommand(this), + new DailyCommand(this) // new EmbedCommand(this) ); @@ -269,6 +275,9 @@ private void setupCommands() { UpdateCheckHelper updateCheckHelper = new UpdateCheckHelper(this); updateCheckHelper.checkUpdate(); + + this.dailyChannelHelper = new DailyChannelHelper(this); + dailyChannelHelper.start(); } public void addEventListener(final ListenerAdapter listener) { diff --git a/src/main/java/com/beanbeanjuice/cafebot/commands/settings/daily/DailyCommand.java b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/daily/DailyCommand.java new file mode 100644 index 00000000..bb507882 --- /dev/null +++ b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/daily/DailyCommand.java @@ -0,0 +1,61 @@ +package com.beanbeanjuice.cafebot.commands.settings.daily; + +import com.beanbeanjuice.cafebot.CafeBot; +import com.beanbeanjuice.cafebot.utility.commands.Command; +import com.beanbeanjuice.cafebot.utility.commands.CommandCategory; +import com.beanbeanjuice.cafebot.utility.commands.ICommand; +import com.beanbeanjuice.cafebot.utility.commands.ISubCommand; +import net.dv8tion.jda.api.Permission; + +public class DailyCommand extends Command implements ICommand { + + public DailyCommand(final CafeBot cafeBot) { + super(cafeBot); + } + + @Override + public String getName() { + return "daily"; + } + + @Override + public String getDescription() { + return "Make a channel reset daily!"; + } + + @Override + public CommandCategory getCategory() { + return CommandCategory.SETTINGS; + } + + @Override + public Permission[] getPermissions() { + return new Permission[] { + Permission.MANAGE_CHANNEL + }; + } + + @Override + public boolean isEphemeral() { + return true; + } + + @Override + public boolean isNSFW() { + return false; + } + + @Override + public boolean allowDM() { + return false; + } + + @Override + public ISubCommand[] getSubCommands() { + return new ISubCommand[] { + new DailySetSubCommand(cafeBot), + new DailyRemoveSubCommand(cafeBot) + }; + } + +} diff --git a/src/main/java/com/beanbeanjuice/cafebot/commands/settings/daily/DailyRemoveSubCommand.java b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/daily/DailyRemoveSubCommand.java new file mode 100644 index 00000000..20477b84 --- /dev/null +++ b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/daily/DailyRemoveSubCommand.java @@ -0,0 +1,44 @@ +package com.beanbeanjuice.cafebot.commands.settings.daily; + +import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformationType; +import com.beanbeanjuice.cafebot.CafeBot; +import com.beanbeanjuice.cafebot.utility.commands.Command; +import com.beanbeanjuice.cafebot.utility.commands.ISubCommand; +import com.beanbeanjuice.cafebot.utility.helper.Helper; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; + +public class DailyRemoveSubCommand extends Command implements ISubCommand { + + public DailyRemoveSubCommand(final CafeBot cafeBot) { + super(cafeBot); + } + + @Override + public void handle(SlashCommandInteractionEvent event) { + this.cafeBot.getCafeAPI().getGuildsEndpoint().updateGuildInformation(event.getGuild().getId(), GuildInformationType.DAILY_CHANNEL_ID, "0") + .thenAcceptAsync((ignored) -> { + event.getHook().sendMessageEmbeds(Helper.successEmbed( + "Daily Channel Removed", + "The daily channel has been successfully removed. The channel will no longer auto-reset daily." + )).queue(); + }) + .exceptionallyAsync((e) -> { + event.getHook().sendMessageEmbeds(Helper.errorEmbed( + "Error Removing Daily Channel", + String.format("There was an error removing the daily channel: %s", e.getMessage()) + )).queue(); + return null; + }); + } + + @Override + public String getName() { + return "remove"; + } + + @Override + public String getDescription() { + return "Remove the daily channel."; + } + +} diff --git a/src/main/java/com/beanbeanjuice/cafebot/commands/settings/daily/DailySetSubCommand.java b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/daily/DailySetSubCommand.java new file mode 100644 index 00000000..c91dcba8 --- /dev/null +++ b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/daily/DailySetSubCommand.java @@ -0,0 +1,60 @@ +package com.beanbeanjuice.cafebot.commands.settings.daily; + +import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformationType; +import com.beanbeanjuice.cafebot.CafeBot; +import com.beanbeanjuice.cafebot.utility.commands.Command; +import com.beanbeanjuice.cafebot.utility.commands.ISubCommand; +import com.beanbeanjuice.cafebot.utility.helper.Helper; +import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.commands.OptionMapping; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; + +import java.util.Optional; + +public class DailySetSubCommand extends Command implements ISubCommand { + + public DailySetSubCommand(final CafeBot cafeBot) { + super(cafeBot); + } + + @Override + public void handle(SlashCommandInteractionEvent event) { + Optional channelMapping = Optional.ofNullable(event.getOption("channel")); + GuildChannelUnion channel = channelMapping.map(OptionMapping::getAsChannel).orElse((GuildChannelUnion) event.getChannel()); + + this.cafeBot.getCafeAPI().getGuildsEndpoint().updateGuildInformation(event.getGuild().getId(), GuildInformationType.DAILY_CHANNEL_ID, channel.getId()) + .thenAcceptAsync((ignored) -> { + event.getHook().sendMessageEmbeds(Helper.successEmbed( + "Daily Channel Set", + String.format("The daily channel has been set to %s. This channel will reset daily!", channel.getAsMention()) + )).queue(); + }) + .exceptionallyAsync((e) -> { + event.getHook().sendMessageEmbeds(Helper.errorEmbed( + "Error Setting Daily Channel", + String.format("There was an error setting the daily channel: %s", e.getMessage()) + )).queue(); + return null; + }); + } + + @Override + public String getName() { + return "set"; + } + + @Override + public String getDescription() { + return "Set the daily channel!"; + } + + @Override + public OptionData[] getOptions() { + return new OptionData[] { + new OptionData(OptionType.CHANNEL, "channel", "The channel you want to set the daily channel to.", false) + }; + } + +} diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/helper/DailyChannelHelper.java b/src/main/java/com/beanbeanjuice/cafebot/utility/helper/DailyChannelHelper.java new file mode 100644 index 00000000..75f50a11 --- /dev/null +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/helper/DailyChannelHelper.java @@ -0,0 +1,51 @@ +package com.beanbeanjuice.cafebot.utility.helper; + +import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformationType; +import com.beanbeanjuice.cafebot.CafeBot; +import com.beanbeanjuice.cafebot.utility.logging.LogLevel; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion; + +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.TimeUnit; + +public class DailyChannelHelper { + + private final CafeBot cafeBot; + + public DailyChannelHelper(final CafeBot cafeBot) { + this.cafeBot = cafeBot; + } + + public void start() { + TimerTask task = new TimerTask() { + @Override + public void run() { + cafeBot.getLogger().log(DailyChannelHelper.class, LogLevel.INFO, "Resetting daily channels..."); + handleDailyResets(); + } + }; + + Timer timer = new Timer(); + timer.scheduleAtFixedRate(task, TimeUnit.DAYS.toMillis(1), TimeUnit.DAYS.toMillis(1)); + } + + private void handleDailyResets() { + this.cafeBot.getCafeAPI().getGuildsEndpoint().getAllGuildInformation().thenAcceptAsync((guildsMap) -> { + guildsMap.forEach((guildId, guildInfo) -> { + String dailyChannelID = guildInfo.getSetting(GuildInformationType.DAILY_CHANNEL_ID); + + Guild guild = this.cafeBot.getJDA().getGuildById(guildId); + if (guild == null) return; + + TextChannel channel = guild.getTextChannelById(dailyChannelID); + if (channel == null) return; + + channel.createCopy().queue((ignored) -> channel.delete().queue()); + }); + }); + } + +}