-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There is also now a helper that resets the daily channels when needed.
- Loading branch information
1 parent
2b3f5e6
commit cca3f70
Showing
5 changed files
with
226 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/com/beanbeanjuice/cafebot/commands/settings/daily/DailyCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}; | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/beanbeanjuice/cafebot/commands/settings/daily/DailyRemoveSubCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."; | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/beanbeanjuice/cafebot/commands/settings/daily/DailySetSubCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}; | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/beanbeanjuice/cafebot/utility/helper/DailyChannelHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
}); | ||
} | ||
|
||
} |