This commit is contained in:
parent
efe11b77f7
commit
706c2a8e21
@ -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<Member> 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -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) {
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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<WelcomeMessage> welcomeMessages;
|
||||
|
||||
@OneToMany(mappedBy = "rank")
|
||||
private List<ByeMessage> 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<WelcomeMessage> getWelcomeMessages() {
|
||||
return welcomeMessages;
|
||||
}
|
||||
|
||||
public List<ByeMessage> getByeMessages() {
|
||||
return byeMessages;
|
||||
}
|
||||
|
||||
public String getByeTitle() {
|
||||
return byeTitle;
|
||||
}
|
||||
|
||||
public void setByeTitle(String byeTitle) {
|
||||
this.byeTitle = byeTitle;
|
||||
}
|
||||
}
|
||||
|
@ -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<ByeMessage> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<Rank> 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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user