added configurable message to birthdays
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 36s

This commit is contained in:
Lee 2024-06-26 12:31:44 +01:00
parent 6d593246e8
commit ef64b11907
10 changed files with 119 additions and 25 deletions

@ -1,6 +1,5 @@
package cc.fascinated.bat.command; package cc.fascinated.bat.command;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import lombok.Setter; import lombok.Setter;

@ -1,6 +1,5 @@
package cc.fascinated.bat.features.birthday; package cc.fascinated.bat.features.birthday;
import cc.fascinated.bat.common.TimerUtils;
import cc.fascinated.bat.features.Feature; import cc.fascinated.bat.features.Feature;
import cc.fascinated.bat.features.birthday.command.BirthdayCommand; import cc.fascinated.bat.features.birthday.command.BirthdayCommand;
import cc.fascinated.bat.features.birthday.profile.BirthdayProfile; 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.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */

@ -18,5 +18,6 @@ public class BirthdayCommand extends BatCommand {
super.addSubCommand("set", context.getBean(SetSubCommand.class)); super.addSubCommand("set", context.getBean(SetSubCommand.class));
super.addSubCommand("remove", context.getBean(RemoveSubCommand.class)); super.addSubCommand("remove", context.getBean(RemoveSubCommand.class));
super.addSubCommand("channel", context.getBean(ChannelSubCommand.class)); super.addSubCommand("channel", context.getBean(ChannelSubCommand.class));
super.addSubCommand("message", context.getBean(MessageSubCommand.class));
} }
} }

@ -4,11 +4,11 @@ import cc.fascinated.bat.command.BatSubCommand;
import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.TextChannelUtils; import cc.fascinated.bat.common.TextChannelUtils;
import cc.fascinated.bat.features.birthday.profile.BirthdayProfile; 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.BatGuild;
import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.model.BatUser;
import cc.fascinated.bat.service.GuildService; import cc.fascinated.bat.service.GuildService;
import lombok.NonNull; import lombok.NonNull;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
@ -28,7 +28,7 @@ public class ChannelSubCommand extends BatSubCommand {
@Autowired @Autowired
public ChannelSubCommand(GuildService guildService) { 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); super.addOption(OptionType.CHANNEL, "channel", "The channel birthdays will be sent in", false);
this.guildService = guildService; this.guildService = guildService;
} }

@ -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;
}
}

@ -9,16 +9,10 @@ import cc.fascinated.bat.service.GuildService;
import lombok.NonNull; import lombok.NonNull;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; 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 net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)

@ -6,6 +6,7 @@ import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import java.util.*; import java.util.*;
@ -13,8 +14,11 @@ import java.util.*;
/** /**
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@Getter @Setter @Getter
@Setter
public class BirthdayProfile extends Profile { 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 * The list of birthdays that are being tracked
*/ */
@ -25,6 +29,11 @@ public class BirthdayProfile extends Profile {
*/ */
private String channelId; private String channelId;
/**
* The message to send when it is a user's birthday
*/
private String message = DEFAULT_MESSAGE;
public BirthdayProfile() { public BirthdayProfile() {
super("birthday"); super("birthday");
} }
@ -103,6 +112,12 @@ public class BirthdayProfile extends Profile {
return age; return age;
} }
/**
* Validates the profiles configuration
*
* @param guild the guild to validate
* @return if the profile is valid
*/
public boolean validate(BatGuild guild) { public boolean validate(BatGuild guild) {
if (birthdays == null) { if (birthdays == null) {
birthdays = new HashMap<>(); birthdays = new HashMap<>();
@ -196,10 +211,19 @@ public class BirthdayProfile extends Profile {
channelId = null; channelId = null;
return; return;
} }
channel.sendMessage("Happy Birthday %s :tada: :birthday: You are now %s years old!".formatted( channel.sendMessage(getBirthdayMessage(member.getUser())).queue();
member.getAsMention(), }
calculateAge(userId)
)).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 @Override

@ -2,11 +2,9 @@ package cc.fascinated.bat.features.scoresaber.command.scoresaber;
import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.command.BatSubCommand;
import cc.fascinated.bat.common.EmbedUtils; 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.features.scoresaber.profile.UserScoreSaberProfile;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.model.BatUser;
import cc.fascinated.bat.service.ScoreSaberService;
import cc.fascinated.bat.service.UserService; import cc.fascinated.bat.service.UserService;
import lombok.NonNull; import lombok.NonNull;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;

@ -7,8 +7,6 @@ import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent; import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired; 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.context.annotation.DependsOn;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;

@ -5,8 +5,6 @@ import cc.fascinated.bat.repository.UserRepository;
import lombok.NonNull; import lombok.NonNull;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; 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.context.annotation.DependsOn;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;