From 706c2a8e218fea0f900c648c510201533604ab21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Melaine=20G=C3=A9rard?= Date: Sun, 17 Nov 2024 22:25:48 +0100 Subject: [PATCH] =?UTF-8?q?:sparkles:=20#86=20et=20#17=20-=20=20Am=C3=A9li?= =?UTF-8?q?oration=20message=20de=20bienvenue=20+=20Ajout=20message=20d'au?= =?UTF-8?q?=20revoir?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../camelia/studio/gachamelia/Gachamelia.java | 34 ----------- .../listeners/GuildMemberJoinListener.java | 34 +++++++---- .../listeners/GuildMemberLeaveListener.java | 60 +++++++++++++++++++ .../gachamelia/listeners/ReadyListener.java | 32 ++++++++++ .../gachamelia/managers/ListenerManager.java | 2 + .../studio/gachamelia/models/ByeMessage.java | 46 ++++++++++++++ .../studio/gachamelia/models/Rank.java | 27 ++++++++- .../repossitories/RankRepository.java | 25 ++++++++ .../gachamelia/services/RankService.java | 16 +++++ 9 files changed, 231 insertions(+), 45 deletions(-) create mode 100644 src/main/java/org/camelia/studio/gachamelia/listeners/GuildMemberLeaveListener.java create mode 100644 src/main/java/org/camelia/studio/gachamelia/models/ByeMessage.java diff --git a/src/main/java/org/camelia/studio/gachamelia/Gachamelia.java b/src/main/java/org/camelia/studio/gachamelia/Gachamelia.java index fab1ebd..e64628f 100644 --- a/src/main/java/org/camelia/studio/gachamelia/Gachamelia.java +++ b/src/main/java/org/camelia/studio/gachamelia/Gachamelia.java @@ -1,16 +1,10 @@ package org.camelia.studio.gachamelia; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; -import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.requests.GatewayIntent; import org.camelia.studio.gachamelia.db.HibernateConfig; import org.camelia.studio.gachamelia.listeners.ReadyListener; import org.camelia.studio.gachamelia.managers.ListenerManager; -import org.camelia.studio.gachamelia.models.User; -import org.camelia.studio.gachamelia.repossitories.RankRepository; -import org.camelia.studio.gachamelia.services.RankService; -import org.camelia.studio.gachamelia.services.UserService; import org.camelia.studio.gachamelia.utils.Configuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,11 +28,6 @@ public class Gachamelia { new ListenerManager().registerListeners(jda); - - - // Initialisation de la base de données - initDatabase(); - Runtime.getRuntime().addShutdownHook(new Thread(() -> { HibernateConfig.shutdown(); jda.shutdown(); @@ -53,27 +42,4 @@ public class Gachamelia { return jda; } - public static void initDatabase() { - if (!RankRepository.getInstance().findAll().isEmpty()) { - Guild guild = jda.getGuildById(Configuration.getInstance().getDotenv().get("GUILD_ID")); - if (guild != null) { - List members = guild.getMembers(); - - for (Member member : members) { - User user = UserService.getInstance().getOrCreateUser(member.getId()); - - if (user.getRank() == null) { - user.setRank(RankService.getInstance().getRandomRank()); - UserService.getInstance().updateUser(user); - } - - logger.info("Utilisateur {} initialisé", member.getUser().getAsTag()); - } - } - } else { - logger.error("Aucun rang n'a été trouvé dans la base de données"); - System.exit(1); - } - } - } \ No newline at end of file diff --git a/src/main/java/org/camelia/studio/gachamelia/listeners/GuildMemberJoinListener.java b/src/main/java/org/camelia/studio/gachamelia/listeners/GuildMemberJoinListener.java index 058a988..da3f4d7 100644 --- a/src/main/java/org/camelia/studio/gachamelia/listeners/GuildMemberJoinListener.java +++ b/src/main/java/org/camelia/studio/gachamelia/listeners/GuildMemberJoinListener.java @@ -11,10 +11,8 @@ import org.camelia.studio.gachamelia.models.WelcomeMessage; import org.camelia.studio.gachamelia.services.RankService; import org.camelia.studio.gachamelia.services.UserService; import org.camelia.studio.gachamelia.utils.Configuration; -import org.camelia.studio.gachamelia.utils.MessageUtils; import java.awt.*; -import java.util.Map; public class GuildMemberJoinListener extends ListenerAdapter { @@ -26,10 +24,7 @@ public class GuildMemberJoinListener extends ListenerAdapter { WelcomeMessage welcomeMessage = RankService.getInstance().getRandomWelcomeMessage(user.getRank()); TextChannel channel = event.getGuild().getTextChannelById(Configuration.getInstance().getDotenv().get("WELCOME_CHANNEL", "0")); - String description = MessageUtils.insertPlaceholders(welcomeMessage.getMessage(), Map.of( - "username", member.getEffectiveName(), - "rank", user.getRank().getName() - )); + Role role = event.getGuild().getRoleById(user.getRank().getDiscordId()); Color color = new Color(0, 0, 0); @@ -39,13 +34,32 @@ public class GuildMemberJoinListener extends ListenerAdapter { color = role.getColor(); } + + StringBuilder description = new StringBuilder(); + description.append("Bravo ! Vous venez d'invoquer ") + .append(member.getAsMention()).append(" !\n") + .append("Il s'agit d'un personnage de rareté ") + .append(user.getRank().getName()).append(" ! ") + .append(welcomeMessage.getMessage()); + + description.append("\n\n") + .append("__Caractéristiques principales__ :\n") + .append("• Rôle « ").append("N/A").append(" ».").append("\n") + .append("• Élément « ").append("N/A").append(" ».").append("\n") + ; + EmbedBuilder embedBuilder = new EmbedBuilder() - .setTitle("Bienvenue sur le serveur " + event.getGuild().getName() + " !") + .setTitle(event.getMember().getEffectiveName() + " vient d'être invoqué !") .setDescription(description) - .setThumbnail(member.getUser().getAvatarUrl()) + .setThumbnail(member.getUser().getEffectiveAvatarUrl()) .setTimestamp(event.getMember().getTimeJoined()) - .setColor(color) - ; + .setFooter( + "Gachamélia v%s « %s »".formatted( + Configuration.getInstance().getDotenv().get("APP_VERSION", "0.0.1"), + Configuration.getInstance().getDotenv().get("APP_DESCRIPTION", "J'ai posé un pied à terre.") + ), + event.getJDA().getSelfUser().getAvatarUrl()) + .setColor(color); if (channel != null) { diff --git a/src/main/java/org/camelia/studio/gachamelia/listeners/GuildMemberLeaveListener.java b/src/main/java/org/camelia/studio/gachamelia/listeners/GuildMemberLeaveListener.java new file mode 100644 index 0000000..3146e6e --- /dev/null +++ b/src/main/java/org/camelia/studio/gachamelia/listeners/GuildMemberLeaveListener.java @@ -0,0 +1,60 @@ +package org.camelia.studio.gachamelia.listeners; + +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.camelia.studio.gachamelia.models.ByeMessage; +import org.camelia.studio.gachamelia.models.User; +import org.camelia.studio.gachamelia.services.RankService; +import org.camelia.studio.gachamelia.services.UserService; +import org.camelia.studio.gachamelia.utils.Configuration; + +import java.awt.*; +import java.time.Instant; + + +public class GuildMemberLeaveListener extends ListenerAdapter { + + @Override + public void onGuildMemberRemove(GuildMemberRemoveEvent event) { + + net.dv8tion.jda.api.entities.User discordUser = event.getUser(); + User user = UserService.getInstance().getOrCreateUser(discordUser.getId()); + + ByeMessage byeMessage = RankService.getInstance().getRandomByeMessage(user.getRank()); + + TextChannel channel = event.getGuild().getTextChannelById(Configuration.getInstance().getDotenv().get("WELCOME_CHANNEL", "0")); + + + Role role = event.getGuild().getRoleById(user.getRank().getDiscordId()); + Color color = new Color(0, 0, 0); + + if (role != null) { + color = role.getColor(); + } + + + StringBuilder description = new StringBuilder(); + description.append(byeMessage.getMessage().replaceAll("%username%", "**" + discordUser.getEffectiveName() + "**")); + + EmbedBuilder embedBuilder = new EmbedBuilder() + .setTitle(user.getRank().getByeTitle() != null ? user.getRank().getByeTitle() : "Au revoir, %s !".formatted(discordUser.getEffectiveName())) + .setDescription(description) + .setThumbnail(discordUser.getEffectiveAvatarUrl()) + .setTimestamp(Instant.now()) + .setFooter( + "Gachamélia v%s « %s »".formatted( + Configuration.getInstance().getDotenv().get("APP_VERSION", "0.0.1"), + Configuration.getInstance().getDotenv().get("APP_DESCRIPTION", "J'ai posé un pied à terre.") + ), + event.getJDA().getSelfUser().getAvatarUrl()) + .setColor(color); + + + if (channel != null) { + channel.sendMessageEmbeds(embedBuilder.build()).queue(); + } + } +} diff --git a/src/main/java/org/camelia/studio/gachamelia/listeners/ReadyListener.java b/src/main/java/org/camelia/studio/gachamelia/listeners/ReadyListener.java index 5a14270..87adaae 100644 --- a/src/main/java/org/camelia/studio/gachamelia/listeners/ReadyListener.java +++ b/src/main/java/org/camelia/studio/gachamelia/listeners/ReadyListener.java @@ -1,7 +1,15 @@ package org.camelia.studio.gachamelia.listeners; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.events.session.ReadyEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.camelia.studio.gachamelia.models.User; +import org.camelia.studio.gachamelia.repossitories.RankRepository; +import org.camelia.studio.gachamelia.services.RankService; +import org.camelia.studio.gachamelia.services.UserService; +import org.camelia.studio.gachamelia.utils.Configuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,5 +20,29 @@ public class ReadyListener extends ListenerAdapter { @Override public void onReady(ReadyEvent event) { logger.info("Connecté en tant que {}", event.getJDA().getSelfUser().getAsTag()); + initDatabase(event.getJDA()); + } + + private void initDatabase(JDA jda) { + if (!RankRepository.getInstance().findAll().isEmpty()) { + Guild guild = jda.getGuildById(Configuration.getInstance().getDotenv().get("GUILD_ID")); + if (guild != null) { + guild.loadMembers().onSuccess(members -> { + for (Member member : members) { + User user = UserService.getInstance().getOrCreateUser(member.getId()); + + if (user.getRank() == null) { + user.setRank(RankService.getInstance().getRandomRank()); + UserService.getInstance().updateUser(user); + } + + logger.info("Utilisateur {} initialisé", member.getUser().getAsTag()); + } + }); + } + } else { + logger.error("Aucun rang n'a été trouvé dans la base de données"); + System.exit(1); + } } } diff --git a/src/main/java/org/camelia/studio/gachamelia/managers/ListenerManager.java b/src/main/java/org/camelia/studio/gachamelia/managers/ListenerManager.java index b066a18..c87a35c 100644 --- a/src/main/java/org/camelia/studio/gachamelia/managers/ListenerManager.java +++ b/src/main/java/org/camelia/studio/gachamelia/managers/ListenerManager.java @@ -3,6 +3,7 @@ package org.camelia.studio.gachamelia.managers; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.camelia.studio.gachamelia.listeners.GuildMemberJoinListener; +import org.camelia.studio.gachamelia.listeners.GuildMemberLeaveListener; import org.camelia.studio.gachamelia.listeners.SlashCommandListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,6 +19,7 @@ public class ListenerManager { addListener(new SlashCommandListener()); addListener(new GuildMemberJoinListener()); + addListener(new GuildMemberLeaveListener()); } public void registerListeners(JDA jda) { for (ListenerAdapter listenerAdapter : listener) { diff --git a/src/main/java/org/camelia/studio/gachamelia/models/ByeMessage.java b/src/main/java/org/camelia/studio/gachamelia/models/ByeMessage.java new file mode 100644 index 0000000..dd90d42 --- /dev/null +++ b/src/main/java/org/camelia/studio/gachamelia/models/ByeMessage.java @@ -0,0 +1,46 @@ +package org.camelia.studio.gachamelia.models; + +import jakarta.persistence.*; +import org.camelia.studio.gachamelia.interfaces.IEntity; + +@Entity +@Table(name = "bye_messages") +public class ByeMessage implements IEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne + private Rank rank; + + @Column(nullable = false) + private String message; + + public ByeMessage() { + } + + public ByeMessage(Rank rank, String message) { + this.rank = rank; + this.message = message; + } + + public Long getId() { + return id; + } + + public Rank getRank() { + return rank; + } + + public void setRank(Rank rank) { + this.rank = rank; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/main/java/org/camelia/studio/gachamelia/models/Rank.java b/src/main/java/org/camelia/studio/gachamelia/models/Rank.java index e1a1d7e..c6284ed 100644 --- a/src/main/java/org/camelia/studio/gachamelia/models/Rank.java +++ b/src/main/java/org/camelia/studio/gachamelia/models/Rank.java @@ -15,7 +15,7 @@ public class Rank implements IEntity { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @Column(name = "discordId", nullable = false, length = 255, unique = true) + @Column(name = "discordId", nullable = false, unique = true) private String discordId; @Column(nullable = false) @@ -30,6 +30,9 @@ public class Rank implements IEntity { @OneToMany(mappedBy = "rank") private List welcomeMessages; + @OneToMany(mappedBy = "rank") + private List byeMessages; + @CreationTimestamp @Column(name = "createdAt") private LocalDateTime createdAt; @@ -38,6 +41,9 @@ public class Rank implements IEntity { @Column(name = "updatedAt") private LocalDateTime updatedAt; + @Column(name = "byeTitle") + private String byeTitle; + public Rank(String discordId, String name, int percentage) { this.discordId = discordId; @@ -45,6 +51,13 @@ public class Rank implements IEntity { this.percentage = percentage; } + public Rank(String discordId, String name, int percentage, String byeTitle) { + this.discordId = discordId; + this.name = name; + this.percentage = percentage; + this.byeTitle = byeTitle; + } + public Rank() { } @@ -91,4 +104,16 @@ public class Rank implements IEntity { public List getWelcomeMessages() { return welcomeMessages; } + + public List getByeMessages() { + return byeMessages; + } + + public String getByeTitle() { + return byeTitle; + } + + public void setByeTitle(String byeTitle) { + this.byeTitle = byeTitle; + } } diff --git a/src/main/java/org/camelia/studio/gachamelia/repossitories/RankRepository.java b/src/main/java/org/camelia/studio/gachamelia/repossitories/RankRepository.java index 961be83..6533311 100644 --- a/src/main/java/org/camelia/studio/gachamelia/repossitories/RankRepository.java +++ b/src/main/java/org/camelia/studio/gachamelia/repossitories/RankRepository.java @@ -2,6 +2,7 @@ package org.camelia.studio.gachamelia.repossitories; import org.camelia.studio.gachamelia.db.HibernateConfig; +import org.camelia.studio.gachamelia.models.ByeMessage; import org.camelia.studio.gachamelia.models.Rank; import org.camelia.studio.gachamelia.models.WelcomeMessage; import org.hibernate.Session; @@ -81,4 +82,28 @@ public class RankRepository { } } } + + public ByeMessage getRandomByeMessage(Rank rank) { + try (Session session = sessionFactory.openSession()) { + session.beginTransaction(); + try { + Rank refreshedRank = session.get(Rank.class, rank.getId()); + List byeMessages = refreshedRank.getByeMessages(); + + if (byeMessages.isEmpty()) { + return null; + } + + ByeMessage message = byeMessages.get( + ThreadLocalRandom.current().nextInt(byeMessages.size()) + ); + + session.getTransaction().commit(); + return message; + } catch (Exception e) { + session.getTransaction().rollback(); + throw e; + } + } + } } diff --git a/src/main/java/org/camelia/studio/gachamelia/services/RankService.java b/src/main/java/org/camelia/studio/gachamelia/services/RankService.java index 21aa605..8df195d 100644 --- a/src/main/java/org/camelia/studio/gachamelia/services/RankService.java +++ b/src/main/java/org/camelia/studio/gachamelia/services/RankService.java @@ -1,5 +1,6 @@ package org.camelia.studio.gachamelia.services; +import org.camelia.studio.gachamelia.models.ByeMessage; import org.camelia.studio.gachamelia.models.Rank; import org.camelia.studio.gachamelia.models.WelcomeMessage; import org.camelia.studio.gachamelia.repossitories.RankRepository; @@ -29,6 +30,17 @@ public class RankService { return rank; } + public Rank getOrCreateRank(String name, String discordId, int percentage, String byeTitle) { + Rank rank = RankRepository.getInstance().findByName(name); + + if (rank == null) { + rank = new Rank(discordId, name, percentage, byeTitle); + RankRepository.getInstance().save(rank); + } + + return rank; + } + public List getAllRanks() { return RankRepository.getInstance().findAll(); } @@ -53,4 +65,8 @@ public class RankService { public WelcomeMessage getRandomWelcomeMessage(Rank rank) { return RankRepository.getInstance().getRandomWelcomeMessage(rank); } + + public ByeMessage getRandomByeMessage(Rank rank) { + return RankRepository.getInstance().getRandomByeMessage(rank); + } }