Skip to content

Commit

Permalink
Added the Daily Channel Command
Browse files Browse the repository at this point in the history
There is also now a helper that resets the daily channels when needed.
  • Loading branch information
beanbeanjuice committed Jul 16, 2024
1 parent 2b3f5e6 commit cca3f70
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/com/beanbeanjuice/cafebot/CafeBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -83,6 +85,9 @@ public class CafeBot {
// Listeners
@Getter private AIResponseListener aiResponseListener;

// Helpers
private DailyChannelHelper dailyChannelHelper;

// Handlers
private CommandHandler commandHandler;
@Getter private MenuHandler menuHandler;
Expand Down Expand Up @@ -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)
);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
};
}

}
Original file line number Diff line number Diff line change
@@ -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.";
}

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

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

}

0 comments on commit cca3f70

Please sign in to comment.