✨ Ajout staff bypass
This commit is contained in:
parent
f323417322
commit
729d4fc611
@ -5,4 +5,7 @@ DB_URL=jdbc:postgresql://localhost:5433/gachamelia
|
||||
DB_USER=postgres
|
||||
DB_PASSWORD=gachamelia
|
||||
|
||||
WELCOME_CHANNEL=
|
||||
WELCOME_CHANNEL=
|
||||
STAFF_ROLE=
|
||||
APP_VERSION=0.0.1
|
||||
APP_DESCRIPTION="J'ai posé un pied à terre."
|
@ -12,6 +12,7 @@ import org.camelia.studio.gachamelia.models.User;
|
||||
import org.camelia.studio.gachamelia.models.UserStat;
|
||||
import org.camelia.studio.gachamelia.repositories.StatRepository;
|
||||
import org.camelia.studio.gachamelia.services.UserService;
|
||||
import org.camelia.studio.gachamelia.utils.EmbedUtils;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
@ -49,7 +50,7 @@ public class FichePersoCommand implements ISlashCommand {
|
||||
return;
|
||||
}
|
||||
|
||||
EmbedBuilder embedGeneralite = new EmbedBuilder();
|
||||
EmbedBuilder embedGeneralite = EmbedUtils.createDefaultEmbed(event.getJDA());
|
||||
User user = UserService.getInstance().getOrCreateUser(member.getId());
|
||||
Role role = event.getGuild().getRoleById(user.getRank().getDiscordId());
|
||||
Color color = role != null ? role.getColor() : Color.WHITE;
|
||||
|
@ -12,6 +12,7 @@ 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.EmbedUtils;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
@ -48,17 +49,11 @@ public class GuildMemberJoinListener extends ListenerAdapter {
|
||||
.append("• Élément « ").append(user.getElement().getName()).append(" ».").append("\n")
|
||||
;
|
||||
|
||||
EmbedBuilder embedBuilder = new EmbedBuilder()
|
||||
EmbedBuilder embedBuilder = EmbedUtils.createDefaultEmbed(event.getJDA())
|
||||
.setTitle(event.getMember().getEffectiveName() + " vient d'être invoqué !")
|
||||
.setDescription(description)
|
||||
.setThumbnail(member.getUser().getEffectiveAvatarUrl())
|
||||
.setTimestamp(event.getMember().getTimeJoined())
|
||||
.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);
|
||||
|
||||
|
||||
|
@ -11,8 +11,9 @@ 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 org.camelia.studio.gachamelia.utils.EmbedUtils;
|
||||
|
||||
import java.awt.*;
|
||||
import java.time.Instant;
|
||||
|
||||
|
||||
public class GuildMemberLeaveListener extends ListenerAdapter {
|
||||
@ -39,17 +40,10 @@ public class GuildMemberLeaveListener extends ListenerAdapter {
|
||||
StringBuilder description = new StringBuilder();
|
||||
description.append(byeMessage.getMessage().replace("%username%", "**" + discordUser.getEffectiveName() + "**"));
|
||||
|
||||
EmbedBuilder embedBuilder = new EmbedBuilder()
|
||||
EmbedBuilder embedBuilder = EmbedUtils.createDefaultEmbed(event.getJDA())
|
||||
.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);
|
||||
|
||||
|
||||
|
@ -0,0 +1,32 @@
|
||||
package org.camelia.studio.gachamelia.listeners;
|
||||
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleAddEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.camelia.studio.gachamelia.models.Rank;
|
||||
import org.camelia.studio.gachamelia.models.User;
|
||||
import org.camelia.studio.gachamelia.repositories.RankRepository;
|
||||
import org.camelia.studio.gachamelia.services.UserService;
|
||||
import org.camelia.studio.gachamelia.utils.Configuration;
|
||||
|
||||
public class GuildMemberRoleChangeListener extends ListenerAdapter {
|
||||
@Override
|
||||
public void onGuildMemberRoleAdd(GuildMemberRoleAddEvent event) {
|
||||
Member member = event.getMember();
|
||||
Role staffRole = event.getGuild().getRoleById(Configuration.getInstance().getDotenv().get("STAFF_ROLE"));
|
||||
Rank rankStaff = RankRepository.getInstance().getRankStaff();
|
||||
User user = UserService.getInstance().getOrCreateUser(member.getId());
|
||||
|
||||
if (staffRole != null && member.getRoles().contains(staffRole) && (user.getRank() == null || !user.getRank().isStaff())) {
|
||||
user.setRank(rankStaff);
|
||||
UserService.getInstance().updateUser(user);
|
||||
|
||||
Role discordRole = event.getGuild().getRoleById(rankStaff.getDiscordId());
|
||||
|
||||
if (discordRole != null) {
|
||||
event.getGuild().addRoleToMember(member, discordRole).queue();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -4,8 +4,10 @@ import jakarta.annotation.Nonnull;
|
||||
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.entities.Role;
|
||||
import net.dv8tion.jda.api.events.session.ReadyEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import org.camelia.studio.gachamelia.models.Rank;
|
||||
import org.camelia.studio.gachamelia.models.Stat;
|
||||
import org.camelia.studio.gachamelia.models.User;
|
||||
import org.camelia.studio.gachamelia.models.UserStat;
|
||||
@ -36,11 +38,25 @@ public class ReadyListener extends ListenerAdapter {
|
||||
private void initDatabase(JDA jda) {
|
||||
if (!RankRepository.getInstance().findAll().isEmpty() || !RoleRepository.getInstance().findAll().isEmpty() || !ElementRepository.getInstance().findAll().isEmpty() || !StatRepository.getInstance().findAll().isEmpty()) {
|
||||
Guild guild = jda.getGuildById(Configuration.getInstance().getDotenv().get("GUILD_ID"));
|
||||
Rank rankStaff = RankRepository.getInstance().getRankStaff();
|
||||
|
||||
if (rankStaff == null) {
|
||||
logger.error("Aucun rang staff n'a été trouvé dans la base de données");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
if (guild != null) {
|
||||
guild.loadMembers().onSuccess(members -> {
|
||||
for (Member member : members) {
|
||||
User user = UserService.getInstance().getOrCreateUser(member.getId());
|
||||
|
||||
Role role = guild.getRoleById(Configuration.getInstance().getDotenv().get("STAFF_ROLE"));
|
||||
|
||||
if (role != null && member.getRoles().contains(role) && (user.getRank() == null || !user.getRank().isStaff())) {
|
||||
user.setRank(rankStaff);
|
||||
UserService.getInstance().updateUser(user);
|
||||
}
|
||||
|
||||
if (user.getRank() == null) {
|
||||
user.setRank(RankService.getInstance().getRandomRank());
|
||||
UserService.getInstance().updateUser(user);
|
||||
@ -69,6 +85,13 @@ public class ReadyListener extends ListenerAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
Rank rank = user.getRank();
|
||||
Role discordRole = guild.getRoleById(rank.getDiscordId());
|
||||
|
||||
if (discordRole != null) {
|
||||
guild.addRoleToMember(member, discordRole).queue();
|
||||
}
|
||||
|
||||
logger.info("Utilisateur {} initialisé", member.getUser().getAsTag());
|
||||
}
|
||||
});
|
||||
|
@ -2,6 +2,7 @@ package org.camelia.studio.gachamelia.models;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.camelia.studio.gachamelia.interfaces.IEntity;
|
||||
import org.hibernate.annotations.ColumnDefault;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import org.hibernate.annotations.UpdateTimestamp;
|
||||
|
||||
@ -45,6 +46,10 @@ public class Rank implements IEntity {
|
||||
@Column(name = "byeTitle")
|
||||
private String byeTitle;
|
||||
|
||||
@Column(name = "is_staff", nullable = false)
|
||||
@ColumnDefault("false")
|
||||
private boolean staff;
|
||||
|
||||
@OneToMany(mappedBy = "id.rank", fetch = FetchType.LAZY)
|
||||
private final List<RankStat> rankStats = new ArrayList<>();
|
||||
|
||||
@ -52,7 +57,13 @@ public class Rank implements IEntity {
|
||||
return rankStats;
|
||||
}
|
||||
|
||||
public boolean isStaff() {
|
||||
return this.staff;
|
||||
}
|
||||
|
||||
public void setStaff(boolean staff) {
|
||||
this.staff = staff;
|
||||
}
|
||||
|
||||
public Rank(String discordId, String name, int percentage) {
|
||||
this.discordId = discordId;
|
||||
|
@ -106,4 +106,11 @@ public class RankRepository {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Rank getRankStaff() {
|
||||
try (Session session = sessionFactory.openSession()) {
|
||||
return session.createQuery("FROM Rank WHERE staff = true", Rank.class)
|
||||
.uniqueResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
package org.camelia.studio.gachamelia.utils;
|
||||
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
public class EmbedUtils {
|
||||
public static EmbedBuilder createDefaultEmbed(JDA jda) {
|
||||
return new EmbedBuilder()
|
||||
.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.")
|
||||
),
|
||||
jda.getSelfUser().getAvatarUrl()
|
||||
)
|
||||
.setColor(0x2F3136)
|
||||
;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user