diff --git a/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java b/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java index 0c3349f..700909c 100644 --- a/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java +++ b/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java @@ -47,6 +47,7 @@ public class SteamwarDiscordBotConfig { public static String TICKET_CREATED; public static String TICKET_LOG; public static Map TICKET_TYPES; + public static Map RANKS; public static void loadConfig(Configuration config) { TOKEN = config.getString("token"); @@ -96,5 +97,11 @@ public class SteamwarDiscordBotConfig { type.getString("color"), type.getString("pre"))); } + + RANKS = new HashMap<>(); + Configuration ranksSections = config.getSection("ranks"); + for (String type : ranksSections.getKeys()) { + RANKS.put(type, ranksSections.getString(type)); + } } } diff --git a/src/de/steamwar/bungeecore/bot/util/DiscordRanks.java b/src/de/steamwar/bungeecore/bot/util/DiscordRanks.java new file mode 100644 index 0000000..495d227 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/util/DiscordRanks.java @@ -0,0 +1,58 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bungeecore.bot.util; + +import de.steamwar.bungeecore.bot.SteamwarDiscordBot; +import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; +import de.steamwar.bungeecore.sql.SteamwarUser; +import de.steamwar.bungeecore.sql.UserGroup; +import lombok.experimental.UtilityClass; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Role; + +import java.util.HashSet; +import java.util.Set; + +@UtilityClass +public class DiscordRanks { + + public void update(SteamwarUser steamwarUser) { + if (steamwarUser.getDiscordId() == null) { + return; + } + Guild guild = SteamwarDiscordBot.instance().getJda().getGuildById(SteamwarDiscordBotConfig.GUILD); + Member member = guild.getMemberById(steamwarUser.getDiscordId()); + if (member == null) { + return; + } + Set toRemove = new HashSet<>(SteamwarDiscordBotConfig.RANKS.keySet()); + if (steamwarUser.getUserGroup() != UserGroup.Member) { + toRemove.remove(steamwarUser.getUserGroup().name().toLowerCase()); + } + for (String s : toRemove) { + String roleId = SteamwarDiscordBotConfig.RANKS.get(s); + guild.removeRoleFromMember(member, guild.getRoleById(roleId)).complete(); + } + if (steamwarUser.getUserGroup() != UserGroup.Member) { + guild.addRoleToMember(member, guild.getRoleById(SteamwarDiscordBotConfig.RANKS.get(steamwarUser.getUserGroup().name()))); + } + } +} diff --git a/src/de/steamwar/bungeecore/listeners/ConnectionListener.java b/src/de/steamwar/bungeecore/listeners/ConnectionListener.java index ef04b14..9506ea2 100644 --- a/src/de/steamwar/bungeecore/listeners/ConnectionListener.java +++ b/src/de/steamwar/bungeecore/listeners/ConnectionListener.java @@ -23,6 +23,7 @@ import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.Message; import de.steamwar.bungeecore.Servertype; import de.steamwar.bungeecore.Subserver; +import de.steamwar.bungeecore.bot.util.DiscordRanks; import de.steamwar.bungeecore.commands.ChallengeCommand; import de.steamwar.bungeecore.commands.CheckCommand; import de.steamwar.bungeecore.commands.MsgCommand; @@ -94,6 +95,8 @@ public class ConnectionListener extends BasicListener { Message.broadcast("JOIN_FIRST", player.getName()); newPlayers.remove(player.getUniqueId()); } + + DiscordRanks.update(user); } /** To redirect players to the lobby in case of server closure. */