More syncing roles stuff
This commit is contained in:
parent
1aa1563ec1
commit
420d044153
|
|
@ -6,6 +6,7 @@ import com.alttd.proxydiscordlink.config.BotConfig;
|
||||||
import com.alttd.proxydiscordlink.config.Config;
|
import com.alttd.proxydiscordlink.config.Config;
|
||||||
import com.alttd.proxydiscordlink.database.Database;
|
import com.alttd.proxydiscordlink.database.Database;
|
||||||
import com.alttd.proxydiscordlink.database.DatabaseConnection;
|
import com.alttd.proxydiscordlink.database.DatabaseConnection;
|
||||||
|
import com.alttd.proxydiscordlink.minecraft.listeners.LuckpermsEvents;
|
||||||
import com.alttd.proxydiscordlink.minecraft.listeners.PlayerJoin;
|
import com.alttd.proxydiscordlink.minecraft.listeners.PlayerJoin;
|
||||||
import com.alttd.proxydiscordlink.minecraft.listeners.PlayerLeave;
|
import com.alttd.proxydiscordlink.minecraft.listeners.PlayerLeave;
|
||||||
import com.alttd.proxydiscordlink.util.ALogger;
|
import com.alttd.proxydiscordlink.util.ALogger;
|
||||||
|
|
@ -77,6 +78,7 @@ public class DiscordLink {
|
||||||
public void loadEvents() {
|
public void loadEvents() {
|
||||||
server.getEventManager().register(this, new PlayerJoin());
|
server.getEventManager().register(this, new PlayerJoin());
|
||||||
server.getEventManager().register(this, new PlayerLeave());
|
server.getEventManager().register(this, new PlayerLeave());
|
||||||
|
new LuckpermsEvents().listener();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadBot() {
|
public void loadBot() {
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,9 @@ import com.alttd.proxydiscordlink.util.ALogger;
|
||||||
import net.dv8tion.jda.api.EmbedBuilder;
|
import net.dv8tion.jda.api.EmbedBuilder;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.JDABuilder;
|
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.entities.Role;
|
||||||
import net.dv8tion.jda.api.entities.TextChannel;
|
import net.dv8tion.jda.api.entities.TextChannel;
|
||||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||||
import net.dv8tion.jda.api.utils.MemberCachePolicy;
|
import net.dv8tion.jda.api.utils.MemberCachePolicy;
|
||||||
|
|
@ -91,4 +94,32 @@ public class Bot {
|
||||||
ALogger.error("caught some exception, " + e);
|
ALogger.error("caught some exception, " + e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean addRole(long userId, long roleId, long guildId) {
|
||||||
|
Guild guild = jda.getGuildById(guildId);
|
||||||
|
if (guild == null)
|
||||||
|
return false;
|
||||||
|
Role role = guild.getRoleById(roleId);
|
||||||
|
if (role == null)
|
||||||
|
return false;
|
||||||
|
Member member = guild.getMemberById(userId);
|
||||||
|
if (member == null)
|
||||||
|
return false;
|
||||||
|
guild.addRoleToMember(member, role).queue();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean removeRole(long userId, long roleId, long guildId) {
|
||||||
|
Guild guild = jda.getGuildById(guildId);
|
||||||
|
if (guild == null)
|
||||||
|
return false;
|
||||||
|
Role role = guild.getRoleById(roleId);
|
||||||
|
if (role == null)
|
||||||
|
return false;
|
||||||
|
Member member = guild.getMemberById(userId);
|
||||||
|
if (member == null)
|
||||||
|
return false;
|
||||||
|
guild.removeRoleFromMember(member, role).queue();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.alttd.proxydiscordlink.bot.api;
|
||||||
|
|
||||||
|
import com.alttd.proxydiscordlink.DiscordLink;
|
||||||
|
|
||||||
|
public class DiscordModifyRole {
|
||||||
|
|
||||||
|
public static boolean discordAddRole(long userId, long roleId, long guildId) {
|
||||||
|
return DiscordLink.getPlugin().getBot().addRole(userId, roleId, guildId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -48,7 +48,7 @@ public class DiscordRoleListener extends ListenerAdapter {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
player.update(added_roles, true);
|
player.updateMinecraft(added_roles, true);
|
||||||
added_roles.forEach(discordRole -> {
|
added_roles.forEach(discordRole -> {
|
||||||
if (!discordRole.getAnnouncement().isEmpty()) {
|
if (!discordRole.getAnnouncement().isEmpty()) {
|
||||||
Component component = miniMessage.parse(
|
Component component = miniMessage.parse(
|
||||||
|
|
@ -74,6 +74,6 @@ public class DiscordRoleListener extends ListenerAdapter {
|
||||||
return;
|
return;
|
||||||
|
|
||||||
DiscordLink.getPlugin().getDatabase().getPlayer(event.getUser().getIdLong())
|
DiscordLink.getPlugin().getDatabase().getPlayer(event.getUser().getIdLong())
|
||||||
.update(removed_roles, false);
|
.updateMinecraft(removed_roles, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -166,10 +166,12 @@ public class BotConfig {
|
||||||
|
|
||||||
public static String BOT_TOKEN = "unconfigured";
|
public static String BOT_TOKEN = "unconfigured";
|
||||||
public static String COMMAND_CHANNEL = "unconfigured";
|
public static String COMMAND_CHANNEL = "unconfigured";
|
||||||
|
public static long GUILD_ID = -1;
|
||||||
|
|
||||||
private static void settings() {
|
private static void settings() {
|
||||||
BOT_TOKEN = getString("settings.token", BOT_TOKEN);
|
BOT_TOKEN = getString("settings.token", BOT_TOKEN);
|
||||||
COMMAND_CHANNEL = getString("settings.command_channel", COMMAND_CHANNEL);
|
COMMAND_CHANNEL = getString("settings.command_channel", COMMAND_CHANNEL);
|
||||||
|
GUILD_ID = getLong("settings.guild-id", GUILD_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String SL_MINIMUMRANK = "trainee";
|
public static String SL_MINIMUMRANK = "trainee";
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.alttd.proxydiscordlink.minecraft.listeners;
|
||||||
|
|
||||||
|
import com.alttd.proxydiscordlink.DiscordLink;
|
||||||
|
import com.alttd.proxydiscordlink.bot.objects.DiscordRole;
|
||||||
|
import com.alttd.proxydiscordlink.objects.DiscordLinkPlayer;
|
||||||
|
import com.alttd.proxydiscordlink.util.Utilities;
|
||||||
|
import net.luckperms.api.event.EventBus;
|
||||||
|
import net.luckperms.api.event.node.NodeAddEvent;
|
||||||
|
import net.luckperms.api.event.node.NodeRemoveEvent;
|
||||||
|
import net.luckperms.api.model.PermissionHolder;
|
||||||
|
import net.luckperms.api.model.user.User;
|
||||||
|
import net.luckperms.api.node.Node;
|
||||||
|
import net.luckperms.api.node.types.InheritanceNode;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public class LuckpermsEvents {
|
||||||
|
|
||||||
|
public void listener()
|
||||||
|
{
|
||||||
|
EventBus eventBus = Utilities.getLuckPerms().getEventBus();
|
||||||
|
|
||||||
|
eventBus.subscribe(DiscordLink.getPlugin(), NodeAddEvent.class, event -> updateRank(event.getTarget(), event.getNode(), true));
|
||||||
|
eventBus.subscribe(DiscordLink.getPlugin(), NodeRemoveEvent.class, event -> updateRank(event.getTarget(), event.getNode(), false));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateRank(PermissionHolder permissionHolder, Node node, boolean added)
|
||||||
|
{
|
||||||
|
if (!(node instanceof InheritanceNode inheritanceNode) || !(permissionHolder instanceof User user))
|
||||||
|
return;
|
||||||
|
Optional<DiscordRole> optional = DiscordRole.getDiscordRoles().stream()
|
||||||
|
.filter(discordRole -> inheritanceNode
|
||||||
|
.getGroupName()
|
||||||
|
.equalsIgnoreCase(discordRole.getLuckpermsName()))
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
|
if (optional.isEmpty())
|
||||||
|
return;
|
||||||
|
DiscordLinkPlayer discordLinkPlayer = DiscordLinkPlayer.getDiscordLinkPlayer(user.getUniqueId());
|
||||||
|
if (discordLinkPlayer == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
discordLinkPlayer.updateMinecraft(List.of(optional.get()), added);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,12 +1,15 @@
|
||||||
package com.alttd.proxydiscordlink.objects;
|
package com.alttd.proxydiscordlink.objects;
|
||||||
|
|
||||||
import com.alttd.proxydiscordlink.DiscordLink;
|
import com.alttd.proxydiscordlink.DiscordLink;
|
||||||
|
import com.alttd.proxydiscordlink.bot.api.DiscordModifyRole;
|
||||||
import com.alttd.proxydiscordlink.bot.objects.DiscordRole;
|
import com.alttd.proxydiscordlink.bot.objects.DiscordRole;
|
||||||
|
import com.alttd.proxydiscordlink.config.BotConfig;
|
||||||
import com.alttd.proxydiscordlink.database.Database;
|
import com.alttd.proxydiscordlink.database.Database;
|
||||||
import com.alttd.proxydiscordlink.util.ALogger;
|
import com.alttd.proxydiscordlink.util.ALogger;
|
||||||
import com.alttd.proxydiscordlink.util.Utilities;
|
import com.alttd.proxydiscordlink.util.Utilities;
|
||||||
import net.luckperms.api.model.user.User;
|
import net.luckperms.api.model.user.User;
|
||||||
import net.luckperms.api.node.NodeBuilder;
|
import net.luckperms.api.node.NodeBuilder;
|
||||||
|
import net.luckperms.api.node.types.InheritanceNode;
|
||||||
import net.luckperms.api.node.types.PermissionNode;
|
import net.luckperms.api.node.types.PermissionNode;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
@ -52,10 +55,25 @@ public class DiscordLinkPlayer {
|
||||||
return discordUsername;
|
return discordUsername;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(List<DiscordRole> roles, boolean added) {
|
public void updateDiscord(List<DiscordRole> roles, boolean added) {
|
||||||
|
for (DiscordRole role : roles) {
|
||||||
|
if (role.getDisplayName().equalsIgnoreCase("nitro"))
|
||||||
|
isNitro = added; //FIXME this should be a list instead of a bool (separate table for roles they have)
|
||||||
|
if (List.of("viceroy", "count", "duke", "archduke").contains(role.getDisplayName().toLowerCase()))
|
||||||
|
isDonor = added; //FIXME this should be a list instead of a bool (separate table for roles they have)
|
||||||
|
if (!role.isUpdateToMinecraft())
|
||||||
|
continue;
|
||||||
|
//TODO implement
|
||||||
|
}
|
||||||
|
roles.forEach(role -> DiscordLink.getPlugin().getBot().addRole(userId, role.getId(), BotConfig.GUILD_ID)); //TODO test
|
||||||
|
|
||||||
|
DiscordLink.getPlugin().getDatabase().syncPlayer(this);
|
||||||
|
//TODO implement
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateMinecraft(List<DiscordRole> roles, boolean added) {
|
||||||
User user = Utilities.getLuckPerms().getUserManager().getUser(getUuid());
|
User user = Utilities.getLuckPerms().getUserManager().getUser(getUuid());
|
||||||
if (user == null)
|
if (user == null) {
|
||||||
{
|
|
||||||
ALogger.error("Tried updating a user luckperms couldn't find: " + getUuid());
|
ALogger.error("Tried updating a user luckperms couldn't find: " + getUuid());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -67,12 +85,12 @@ public class DiscordLinkPlayer {
|
||||||
isDonor = added; //FIXME this should be a list instead of a bool (separate table for roles they have)
|
isDonor = added; //FIXME this should be a list instead of a bool (separate table for roles they have)
|
||||||
if (!role.isUpdateToMinecraft())
|
if (!role.isUpdateToMinecraft())
|
||||||
continue;
|
continue;
|
||||||
PermissionNode group = PermissionNode.builder("group." + role.getLuckpermsName()).build();
|
InheritanceNode group = InheritanceNode.builder(role.getLuckpermsName()).build();
|
||||||
if (!user.getNodes().contains(group))
|
|
||||||
if (added)
|
if (!user.getNodes().contains(group) && added)
|
||||||
user.getNodes().add(group);
|
user.data().add(group);
|
||||||
else
|
else if (!added)
|
||||||
user.getNodes().remove(group);
|
user.data().remove(group);
|
||||||
}
|
}
|
||||||
DiscordLink.getPlugin().getDatabase().syncPlayer(this);
|
DiscordLink.getPlugin().getDatabase().syncPlayer(this);
|
||||||
//TODO implement
|
//TODO implement
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user