From ef64b11907962d04b35c03083bf3418814fa62c6 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 26 Jun 2024 12:31:44 +0100 Subject: [PATCH] added configurable message to birthdays --- .../cc/fascinated/bat/command/BatCommand.java | 1 - .../features/birthday/BirthdayFeature.java | 3 - .../birthday/command/BirthdayCommand.java | 1 + .../birthday/command/ChannelSubCommand.java | 4 +- .../birthday/command/MessageSubCommand.java | 85 +++++++++++++++++++ .../birthday/command/RemoveSubCommand.java | 6 -- .../birthday/profile/BirthdayProfile.java | 38 +++++++-- .../command/scoresaber/ResetSubCommand.java | 2 - .../fascinated/bat/service/GuildService.java | 2 - .../fascinated/bat/service/UserService.java | 2 - 10 files changed, 119 insertions(+), 25 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/features/birthday/command/MessageSubCommand.java diff --git a/src/main/java/cc/fascinated/bat/command/BatCommand.java b/src/main/java/cc/fascinated/bat/command/BatCommand.java index 9874d2e..670243e 100644 --- a/src/main/java/cc/fascinated/bat/command/BatCommand.java +++ b/src/main/java/cc/fascinated/bat/command/BatCommand.java @@ -1,6 +1,5 @@ package cc.fascinated.bat.command; -import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NonNull; import lombok.Setter; diff --git a/src/main/java/cc/fascinated/bat/features/birthday/BirthdayFeature.java b/src/main/java/cc/fascinated/bat/features/birthday/BirthdayFeature.java index 9fbcc9b..07b5585 100644 --- a/src/main/java/cc/fascinated/bat/features/birthday/BirthdayFeature.java +++ b/src/main/java/cc/fascinated/bat/features/birthday/BirthdayFeature.java @@ -1,6 +1,5 @@ package cc.fascinated.bat.features.birthday; -import cc.fascinated.bat.common.TimerUtils; import cc.fascinated.bat.features.Feature; import cc.fascinated.bat.features.birthday.command.BirthdayCommand; import cc.fascinated.bat.features.birthday.profile.BirthdayProfile; @@ -12,8 +11,6 @@ import org.springframework.context.ApplicationContext; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; -import java.util.concurrent.TimeUnit; - /** * @author Fascinated (fascinated7) */ diff --git a/src/main/java/cc/fascinated/bat/features/birthday/command/BirthdayCommand.java b/src/main/java/cc/fascinated/bat/features/birthday/command/BirthdayCommand.java index d979d35..0232ca0 100644 --- a/src/main/java/cc/fascinated/bat/features/birthday/command/BirthdayCommand.java +++ b/src/main/java/cc/fascinated/bat/features/birthday/command/BirthdayCommand.java @@ -18,5 +18,6 @@ public class BirthdayCommand extends BatCommand { super.addSubCommand("set", context.getBean(SetSubCommand.class)); super.addSubCommand("remove", context.getBean(RemoveSubCommand.class)); super.addSubCommand("channel", context.getBean(ChannelSubCommand.class)); + super.addSubCommand("message", context.getBean(MessageSubCommand.class)); } } diff --git a/src/main/java/cc/fascinated/bat/features/birthday/command/ChannelSubCommand.java b/src/main/java/cc/fascinated/bat/features/birthday/command/ChannelSubCommand.java index cad0aad..a81da9e 100644 --- a/src/main/java/cc/fascinated/bat/features/birthday/command/ChannelSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/birthday/command/ChannelSubCommand.java @@ -4,11 +4,11 @@ import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.TextChannelUtils; import cc.fascinated.bat.features.birthday.profile.BirthdayProfile; -import cc.fascinated.bat.features.scoresaber.profile.GuildUserScoreFeedProfile; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.service.GuildService; import lombok.NonNull; +import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; @@ -28,7 +28,7 @@ public class ChannelSubCommand extends BatSubCommand { @Autowired public ChannelSubCommand(GuildService guildService) { - super("channel", "Sets the birthday notification channel"); + super("channel", "Sets the birthday notification channel", Permission.MANAGE_SERVER); super.addOption(OptionType.CHANNEL, "channel", "The channel birthdays will be sent in", false); this.guildService = guildService; } diff --git a/src/main/java/cc/fascinated/bat/features/birthday/command/MessageSubCommand.java b/src/main/java/cc/fascinated/bat/features/birthday/command/MessageSubCommand.java new file mode 100644 index 0000000..a465ea7 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/features/birthday/command/MessageSubCommand.java @@ -0,0 +1,85 @@ +package cc.fascinated.bat.features.birthday.command; + +import cc.fascinated.bat.command.BatSubCommand; +import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.features.birthday.profile.BirthdayProfile; +import cc.fascinated.bat.model.BatGuild; +import cc.fascinated.bat.model.BatUser; +import cc.fascinated.bat.service.GuildService; +import lombok.NonNull; +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import net.dv8tion.jda.api.interactions.commands.OptionMapping; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + + +/** + * @author Fascinated (fascinated7) + */ +@Component("birthday:message.sub") +public class MessageSubCommand extends BatSubCommand { + private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("dd/MM/yyyy"); + private final GuildService guildService; + + @Autowired + public MessageSubCommand(GuildService guildService) { + super("message", "Changes the message that is sent when it is a user's birthday", Permission.MANAGE_SERVER); + super.addOption(OptionType.STRING, "message", "The message that is sent. (Placeholders: {user}, {age})", true); + this.guildService = guildService; + } + + @Override + public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { + BirthdayProfile profile = guild.getProfile(BirthdayProfile.class); + + OptionMapping messageOption = interaction.getOption("message"); + if (messageOption == null) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("You must provide a message") + .build()).queue(); + return; + } + + String message = messageOption.getAsString(); + if (message.length() > 2000) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("The message must be less than 2000 characters") + .build()).queue(); + return; + } + if (!message.contains("{user}") || !message.contains("{age}")) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("The message must contain the placeholders {user} and {age}") + .build()).queue(); + return; + } + + profile.setMessage(message); + guildService.saveGuild(guild); + + interaction.replyEmbeds(EmbedUtils.successEmbed() + .setDescription("You have updated the birthday message!\n\n**Message:** %s".formatted(profile.getBirthdayMessage(user.getDiscordUser()))) + .build()).queue(); + } + + /** + * Parses a birthday from the string + * + * @param birthday the date to parse + * @return the birthday + */ + private Date parseBirthday(String birthday) { + try { + return FORMATTER.parse(birthday); + } catch (ParseException ignored) {} + return null; + } +} diff --git a/src/main/java/cc/fascinated/bat/features/birthday/command/RemoveSubCommand.java b/src/main/java/cc/fascinated/bat/features/birthday/command/RemoveSubCommand.java index 4a7a9a3..d323321 100644 --- a/src/main/java/cc/fascinated/bat/features/birthday/command/RemoveSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/birthday/command/RemoveSubCommand.java @@ -9,16 +9,10 @@ import cc.fascinated.bat.service.GuildService; import lombok.NonNull; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; -import net.dv8tion.jda.api.interactions.commands.OptionMapping; -import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; - /** * @author Fascinated (fascinated7) diff --git a/src/main/java/cc/fascinated/bat/features/birthday/profile/BirthdayProfile.java b/src/main/java/cc/fascinated/bat/features/birthday/profile/BirthdayProfile.java index 07e6a8f..3e64aa6 100644 --- a/src/main/java/cc/fascinated/bat/features/birthday/profile/BirthdayProfile.java +++ b/src/main/java/cc/fascinated/bat/features/birthday/profile/BirthdayProfile.java @@ -6,6 +6,7 @@ import lombok.Getter; import lombok.Setter; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import java.util.*; @@ -13,8 +14,11 @@ import java.util.*; /** * @author Fascinated (fascinated7) */ -@Getter @Setter +@Getter +@Setter public class BirthdayProfile extends Profile { + private static final String DEFAULT_MESSAGE = "Happy Birthday {user} :tada: :birthday: You are now {age} years old!"; + /** * The list of birthdays that are being tracked */ @@ -25,6 +29,11 @@ public class BirthdayProfile extends Profile { */ private String channelId; + /** + * The message to send when it is a user's birthday + */ + private String message = DEFAULT_MESSAGE; + public BirthdayProfile() { super("birthday"); } @@ -32,7 +41,7 @@ public class BirthdayProfile extends Profile { /** * Adds a birthday to be tracked * - * @param userId the id of the user to track + * @param userId the id of the user to track * @param birthday the birthday of the user */ public void addBirthday(String userId, Date birthday) { @@ -103,6 +112,12 @@ public class BirthdayProfile extends Profile { return age; } + /** + * Validates the profiles configuration + * + * @param guild the guild to validate + * @return if the profile is valid + */ public boolean validate(BatGuild guild) { if (birthdays == null) { birthdays = new HashMap<>(); @@ -181,7 +196,7 @@ public class BirthdayProfile extends Profile { /** * Notifies a user that it is their birthday * - * @param guild the guild + * @param guild the guild * @param userId the id of the user */ private void notifyBirthday(BatGuild guild, String userId) { @@ -196,10 +211,19 @@ public class BirthdayProfile extends Profile { channelId = null; return; } - channel.sendMessage("Happy Birthday %s :tada: :birthday: You are now %s years old!".formatted( - member.getAsMention(), - calculateAge(userId) - )).queue(); + channel.sendMessage(getBirthdayMessage(member.getUser())).queue(); + } + + /** + * Gets the birthday message for a user + * + * @param user the user to get the message for + * @return the birthday message + */ + public String getBirthdayMessage(User user) { + return message + .replace("{user}", user.getAsMention()) + .replace("{age}", String.valueOf(calculateAge(user.getId()))); } @Override diff --git a/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/ResetSubCommand.java b/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/ResetSubCommand.java index a49f8ee..52075f2 100644 --- a/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/ResetSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/scoresaber/command/scoresaber/ResetSubCommand.java @@ -2,11 +2,9 @@ package cc.fascinated.bat.features.scoresaber.command.scoresaber; import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.common.EmbedUtils; -import cc.fascinated.bat.features.scoresaber.profile.GuildUserScoreFeedProfile; import cc.fascinated.bat.features.scoresaber.profile.UserScoreSaberProfile; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; -import cc.fascinated.bat.service.ScoreSaberService; import cc.fascinated.bat.service.UserService; import lombok.NonNull; import net.dv8tion.jda.api.entities.Member; diff --git a/src/main/java/cc/fascinated/bat/service/GuildService.java b/src/main/java/cc/fascinated/bat/service/GuildService.java index aa6826c..17ed833 100644 --- a/src/main/java/cc/fascinated/bat/service/GuildService.java +++ b/src/main/java/cc/fascinated/bat/service/GuildService.java @@ -7,8 +7,6 @@ import lombok.extern.log4j.Log4j2; import net.dv8tion.jda.api.events.guild.GuildJoinEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.CachePut; -import org.springframework.cache.annotation.Cacheable; import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Service; diff --git a/src/main/java/cc/fascinated/bat/service/UserService.java b/src/main/java/cc/fascinated/bat/service/UserService.java index 56daf47..2c2ceb4 100644 --- a/src/main/java/cc/fascinated/bat/service/UserService.java +++ b/src/main/java/cc/fascinated/bat/service/UserService.java @@ -5,8 +5,6 @@ import cc.fascinated.bat.repository.UserRepository; import lombok.NonNull; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.CachePut; -import org.springframework.cache.annotation.Cacheable; import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Service;