Compare commits

...

11 Commits

Author SHA1 Message Date
Teriuihi e8c73ead90 Add Jenkinsfile for CI and update build.gradle.kts
Introduce a Jenkinsfile to automate the build, archive, and notification stages in a pipeline. Additionally, modify build.gradle.kts to standardize the shadowJar output filename and remove versioning details from the filename.
2024-08-09 23:04:01 +02:00
Teriuihi abd5578be4 Fix Bot class and update CheckLinkSync constants
Removed unused imports and redundant methods in Bot.java for cleaner code. Updated CheckLinkSync.java to use new constants from BotConfig.DISCORD.
2024-08-09 23:00:55 +02:00
Teriuihi 3b4a4660d0 Merge branch 'slash_commands'
# Conflicts:
#	src/main/java/com/alttd/proxydiscordlink/bot/Bot.java
#	src/main/java/com/alttd/proxydiscordlink/util/Utilities.java
2024-08-09 22:58:21 +02:00
Teriuihi b6630b2639 Update command syntax in Discord link message
Fixed the command syntax in the DISCORD_LINK message to use the correct run_command format. This ensures that users can link their Minecraft and Discord accounts by clicking the message.
2024-08-09 22:54:47 +02:00
Teriuihi a8f24e8d91 Update config file paths
Changed the hardcoded file paths for both Config and BotConfig initializations. The file paths now point to "/mnt/configs/DiscordLink", replacing the previous location in the user's home directory.
2024-07-19 21:12:29 +02:00
Len cb4601dbfe Expose a way to register ListenerAdapter to the discordbot 2023-08-01 00:47:23 +02:00
Teriuihi 645b019f8d Fixed nick command having copy pasted initializer from another command 2023-01-30 02:17:58 +01:00
Teriuihi a40a838e7a Don't respond to commands that might be handled by other bot instances 2023-01-04 02:31:35 +01:00
Teriuihi c144a71d47 Removed unused discord commands 2022-12-27 18:53:47 +01:00
Teriuihi 78b4b5d25a Switched to string (code can start with 0) and fixed using link instead of code for getting option 2022-12-27 18:53:38 +01:00
Stijn 82e16ed007 Added better syncing for nitro roles
Added a way to automatically sync linked status if something went wrong every few hours (in testing, currently only logs the actions it will take)
2022-09-12 21:48:25 +02:00
15 changed files with 249 additions and 71 deletions

20
Jenkinsfile vendored Normal file
View File

@ -0,0 +1,20 @@
pipeline {
agent any
stages {
stage('Gradle') {
steps {
sh 'bash gradlew shadowJar -x test'
}
}
stage('Archive') {
steps {
archiveArtifacts artifacts: 'build/libs/', followSymlinks: false
}
}
stage('discord') {
steps {
discordSend description: "Build: ${BUILD_NUMBER}", showChangeset: true, result: currentBuild.currentResult, title: currentBuild.fullProjectName, webhookURL: env.discordwebhook
}
}
}
}

View File

@ -7,7 +7,6 @@ plugins {
allprojects {
val build = System.getenv("BUILD_NUMBER") ?: "SNAPSHOT"
group = "com.alttd.proxydiscordlink"
version = "1.0.0-BETA-$build"
description = "A velocity plugin to link Discord and Minecraft accounts."
apply(plugin = "java")
@ -54,14 +53,8 @@ dependencies {
tasks {
shadowJar {
archiveFileName.set("${project.name}-${project.version}.jar")
// exclude("net.kyori.adventure")
// exclude("net.kyori.examination")
minimize {
//exclude(dependency("net.kyori:.*:.*"))
}
archiveFileName.set("discordLink.jar")
listOf(
// "net.kyori",
"net.dv8tion.jda"
).forEach { relocate(it, "${rootProject.group}.lib.$it") }
}

View File

@ -51,6 +51,10 @@ public class DiscordLink {
cache = new Cache();
}
//TODO set up a repeating task that checks if ppl with the link role are really linked
//TODO fix unlinking in game not unlinking in discord
//TODO fix nitro booster role not being removed in game (set up a command to force check?)
//TODO fix &sync
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
ALogger.init(logger);

View File

@ -1,8 +1,9 @@
package com.alttd.proxydiscordlink.bot;
import com.alttd.proxydiscordlink.JDAListener;
import com.alttd.proxydiscordlink.bot.commandManager.CommandManager;
import com.alttd.proxydiscordlink.DiscordLink;
import com.alttd.proxydiscordlink.bot.listeners.DiscordRoleListener;
import com.alttd.proxydiscordlink.bot.tasks.CheckLinkSync;
import com.alttd.proxydiscordlink.config.BotConfig;
import com.alttd.proxydiscordlink.util.ALogger;
import net.dv8tion.jda.api.EmbedBuilder;
@ -15,10 +16,12 @@ import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.exceptions.HierarchyException;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
@ -34,10 +37,15 @@ public class Bot {
.enableIntents(GatewayIntent.GUILD_MEMBERS)
.build();
jda.setAutoReconnect(true);
jda.awaitReady();
jda.addEventListener(
new DiscordRoleListener(),
new JDAListener(jda)); //This executes code after jda is done loading
} catch (Exception e) {
new JDAListener(jda));
DiscordLink.getPlugin().getProxy().getScheduler().buildTask(DiscordLink.getPlugin(), new CheckLinkSync())
.delay(120, TimeUnit.SECONDS)
.repeat(12, TimeUnit.HOURS)
.schedule();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@ -231,4 +239,20 @@ public class Bot {
ALogger.warn("Unable to ban " + member.getAsMention() + " : " + member.getId() + " from Discord they might be above me.");
}
}
public List<Member> getMembersWithRole(long guildId, long roleId) {
Guild guild = jda.getGuildById(guildId);
Role role = jda.getRoleById(roleId);
if (guild == null) {
//TODO error
return null;
}
if (role == null) {
//TODO error
return null;
}
return guild.getMembers().stream().filter(member -> member.getRoles().contains(role)).toList();
}
}

View File

@ -1,40 +0,0 @@
package com.alttd.proxydiscordlink.bot;
import com.alttd.proxydiscordlink.bot.commands.*;
import net.dv8tion.jda.api.entities.Message;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public abstract class DiscordCommand {
private static List<DiscordCommand> commands;
public abstract String getCommand();
public abstract String getPermission();// TODO discord and LP permissions
public abstract String getDescription();
public abstract String getSyntax();
public abstract long getChannel();
public abstract void handleCommand(Message message, String sender, String command, String[] args);
public static void loadCommands() {
commands = new ArrayList<>();
loadCommand(new DiscordLinkCommand(),
new DiscordUnlink(),
new DiscordNick(),
new DiscordStaffList(),
new DiscordServerList()
);
}
private static void loadCommand(DiscordCommand ... discordCommands) {
Collections.addAll(commands, discordCommands);
}
public static List<DiscordCommand> getCommands() {
return commands;
}
}

View File

@ -37,13 +37,6 @@ public class CommandManager extends ListenerAdapter {
.filter(discordCommand -> discordCommand.getName().equalsIgnoreCase(commandName))
.findFirst();
if (first.isEmpty()) {
event.replyEmbeds(new EmbedBuilder()
.setTitle("Invalid command")
.setDescription("We couldn't find a command called [" + commandName + "], please report this issue to a Teri")
.setColor(Color.RED)
.build())
.setEphemeral(true)
.queue();
return;
}
first.get().execute(event);

View File

@ -28,7 +28,7 @@ public class CommandLink extends DiscordCommand {
private final CommandData commandData;
public CommandLink(JDA jda) {
commandData = Commands.slash(getName(), "Link your Discord and Altitude Minecraft accounts")
.addOption(OptionType.NUMBER, "code", "The code you got from doing /discord link on Altitude in Minecraft", true)
.addOption(OptionType.STRING, "code", "The code you got from doing /discord link on Altitude in Minecraft", true)
.setDefaultPermissions(DefaultMemberPermissions.ENABLED);
Utilities.registerCommand(jda, commandData);
@ -47,7 +47,7 @@ public class CommandLink extends DiscordCommand {
return;
}
UUID uuid = getUUID(event.getOption("link", OptionMapping::getAsInt));
UUID uuid = getUUID(event.getOption("code", OptionMapping::getAsString));
if (uuid == null) {
Utilities.commandErrAutoRem("This is not a valid link code, please check Minecraft and try again", event);
return;
@ -127,10 +127,10 @@ public class CommandLink extends DiscordCommand {
return "No User";
}
private UUID getUUID(Integer code) {
if (code == null)
private UUID getUUID(String code) {
if (code == null || !code.matches("[0-9]{6}"))
return null;
return DiscordLink.getPlugin().getCache().getUUID(String.valueOf(code));
return DiscordLink.getPlugin().getCache().getUUID(code);
}
@Override

View File

@ -13,6 +13,7 @@ import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import java.util.HashMap;
@ -20,8 +21,10 @@ public class CommandNick extends DiscordCommand {
CommandData commandData;
private final HashMap<String, SubOption> subOptionsMap = new HashMap<>();
public CommandNick(JDA jda) {
commandData = Commands.slash(getName(), "Create an auction")
.addOption(OptionType.NUMBER, "code", "The code you got from doing /discord link on Altitude in Minecraft", true)
commandData = Commands.slash(getName(), "Change your nickname to be your mc name/nickname")
.addSubcommands(new SubcommandData("username", "Change your name to your Minecraft username"),
new SubcommandData("nickname", "Change your name to your Minecraft nickname")
)
.setDefaultPermissions(DefaultMemberPermissions.ENABLED);
Utilities.registerSubOptions(subOptionsMap,
new SubCommandUserName(null,this),

View File

@ -0,0 +1,77 @@
package com.alttd.proxydiscordlink.bot.tasks;
import com.alttd.proxydiscordlink.DiscordLink;
import com.alttd.proxydiscordlink.bot.Bot;
import com.alttd.proxydiscordlink.config.BotConfig;
import com.alttd.proxydiscordlink.util.ALogger;
import net.dv8tion.jda.api.entities.Member;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class CheckLinkSync implements Runnable {
private final DiscordLink plugin;
private final Bot bot;
public CheckLinkSync() {
plugin = DiscordLink.getPlugin();
bot = plugin.getBot();
}
@Override
public void run() {
List<Member> members = bot.getMembersWithRole(BotConfig.DISCORD.GUILD_ID, BotConfig.DISCORD.LINKED_ROLE_ID);
HashSet<Long> dbIdSet = plugin.getDatabase().getLinkedUsers();
HashSet<Long> membersIdSet = members.stream().map(Member::getIdLong).collect(Collectors.toCollection(HashSet::new));
//give these people the link role in discord, if they are not in the discord unlink them in game
HashSet<Long> noRoleIds = dbIdSet.stream().filter(id -> !membersIdSet.contains(id)).collect(Collectors.toCollection(HashSet::new));
//remove the linked role from these people
HashSet<Long> notInDbIds = membersIdSet.stream().filter(id -> !dbIdSet.contains(id)).collect(Collectors.toCollection(HashSet::new));
fixNotInDb(members, notInDbIds);
fixNoLinkRole(members, noRoleIds);
}
private void fixNotInDb(List<Member> members, Set<Long> notInDbIds) {
for (Long id : notInDbIds) {
members.stream()
.filter(member -> member.getIdLong() == id)
.findAny()
.ifPresent(member -> {
ALogger.info("Removing linked role from user with discord id: [" + id + "] due to them not being in the database.");
// bot.removeRole(id, BotConfig.LINKED_ROLE_ID, member.getGuild().getIdLong());
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
private void fixNoLinkRole(List<Member> members, Set<Long> noRoleIds) {
for (Long id : noRoleIds) {
members.stream()
.filter(member -> member.getIdLong() == id)
.findAny()
.ifPresentOrElse(
member -> {
ALogger.info("Adding role to user with discord id: [" + id + "] due to them being in the database without having a role in discord.");
// bot.addRole(id, BotConfig.LINKED_ROLE_ID, member.getGuild().getIdLong());
}, () -> {
ALogger.info("Removing user with discord id: [" + id + "] from the database due to them not being in the discord.");
// plugin.getDatabase().removeLinkedAccount(id);
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}

View File

@ -0,0 +1,7 @@
package com.alttd.proxydiscordlink.bot.tasks;
public class CheckNitroSync {
//TODO copy CheckLinkSync but for nitro
}

View File

@ -35,7 +35,7 @@ public class BotConfig {
public static File CONFIGPATH;
public static void init() { // todo setup share for the config
CONFIGPATH = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + "DiscordLink");
CONFIGPATH = new File(File.separator + "mnt" + File.separator + "configs" + File.separator + "DiscordLink");
CONFIG_FILE = new File(CONFIGPATH, "bot-config.yml");
configLoader = YAMLConfigurationLoader.builder()

View File

@ -31,7 +31,7 @@ public final class Config {
public static File CONFIGPATH;
public static void init() { // todo setup share for the config
CONFIGPATH = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + "DiscordLink");
CONFIGPATH = new File(File.separator + "mnt" + File.separator + "configs" + File.separator + "DiscordLink");
CONFIG_FILE = new File(CONFIGPATH, "config.yml");
configLoader = YAMLConfigurationLoader.builder()
@ -215,7 +215,7 @@ public final class Config {
public static String HELP_RELOAD = "<yellow><gold>/discord reload</gold>: Reload the config.</yellow>";
public static String HELP_SYNC = "<yellow><gold>/discord sync</gold>: Manually synchronize your roles across Discord and Minecraft.</yellow>";
public static List<String> DISCORD_MESSAGE = new ArrayList<>(List.of("Invite code here."));
public static String DISCORD_LINK = "<click:run:command:discord link:><yellow>Your Minecraft and Discord accounts aren't linked yet, to link them click this message!</yellow></click>";
public static String DISCORD_LINK = "<click:run_command:'/discord link'><yellow>Your Minecraft and Discord accounts aren't linked yet, to link them click this message!</yellow></click>";
public static String GIVE_CODE = "<yellow>Your code is <gold><code></gold>, To link your accounts do <gold>&link <code></gold> in the Discord #link channel.</yellow>";
private static void loadMessages() {

View File

@ -1,13 +1,14 @@
package com.alttd.proxydiscordlink.database;
import com.alttd.proxydiscordlink.objects.DiscordLinkPlayer;
import com.velocitypowered.api.proxy.Player;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
public class Database {
@ -103,6 +104,19 @@ public class Database {
}
}
public void removeRole(UUID uuid, String roleName) {
try {
String sql = "DELETE FROM account_roles WHERE uuid = ? AND role_name = ?";
PreparedStatement statement = DatabaseConnection.getConnection().prepareStatement(sql);
statement.setString(1, uuid.toString());
statement.setString(2, roleName);
statement.execute();
} catch (SQLException exception) {
exception.printStackTrace();
}
}
public boolean playerIsLinked(UUID uuid) { //TODO maybe this can be using the discord api instead? (or a cache idk)
try {
PreparedStatement statement = DatabaseConnection.getConnection()
@ -149,7 +163,17 @@ public class Database {
} catch (SQLException var2) {
var2.printStackTrace();
}
}
public void removeLinkedAccount(Long id) {
try {
PreparedStatement statement = DatabaseConnection.getConnection()
.prepareStatement("DELETE FROM linked_accounts WHERE discord_id = ?");
statement.setLong(1, id);
statement.executeUpdate();
} catch (SQLException var2) {
var2.printStackTrace();
}
}
public String uuidFromName(String playerName) {
@ -269,4 +293,20 @@ public class Database {
exception.printStackTrace();
}
}
public HashSet<Long> getLinkedUsers() {
String sql = "SELECT discord_id FROM linked_accounts";
try {
PreparedStatement statement = DatabaseConnection.getConnection().prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
HashSet<Long> results = new HashSet<>();
while (resultSet.next()) {
results.add(resultSet.getLong("discord_id"));
}
return results;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -3,9 +3,14 @@ package com.alttd.proxydiscordlink.minecraft.listeners;
import com.alttd.proxydiscordlink.DiscordLink;
import com.alttd.proxydiscordlink.config.BotConfig;
import com.alttd.proxydiscordlink.objects.DiscordLinkPlayer;
import com.alttd.proxydiscordlink.util.ALogger;
import com.alttd.proxydiscordlink.util.Utilities;
import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.player.ServerConnectedEvent;
import com.velocitypowered.api.proxy.Player;
import java.util.UUID;
public class PlayerJoin {
@ -13,14 +18,16 @@ public class PlayerJoin {
public void playerConnected(ServerConnectedEvent event) {
if (event.getPreviousServer().isPresent())
return;
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
DiscordLinkPlayer discordLinkPlayer = DiscordLinkPlayer.getDiscordLinkPlayer(event.getPlayer().getUniqueId());
DiscordLinkPlayer discordLinkPlayer = DiscordLinkPlayer.getDiscordLinkPlayer(uuid);
if (discordLinkPlayer == null)
return;
boolean sync = false;
String username = event.getPlayer().getUsername();
String username = player.getUsername();
if (!discordLinkPlayer.getUsername().equals(username)) { //Update username if needed
discordLinkPlayer.setUsername(username);
@ -39,6 +46,18 @@ public class PlayerJoin {
DiscordLink.getPlugin().getBot().changeNick(BotConfig.DISCORD.GUILD_ID, discordLinkPlayer.getUserId(), nick);
}
boolean hasMinecraftNitro = Utilities.hasMinecraftNitro(player);
boolean hasDatabaseNitro = Utilities.hasDatabaseNitro(discordLinkPlayer);
if (hasMinecraftNitro != hasDatabaseNitro) {
if (hasMinecraftNitro) {
//Utilities.removeRole(uuid, "nitro"); //TODO add nitro to config
ALogger.info("Removing nitro role from [" + player.getUsername() + "] since they shouldn't have it");
} else {
//Utilities.addRole(uuid, "nitro"); //TODO add nitro to config
ALogger.info("Added nitro role to [" + player.getUsername() + "] since they should have it");
}
}
if (sync) //Sync if needed
DiscordLink.getPlugin().getDatabase().syncPlayer(discordLinkPlayer);
}

View File

@ -5,6 +5,7 @@ import com.alttd.proxydiscordlink.bot.commandManager.SubOption;
import com.alttd.proxydiscordlink.bot.objects.DiscordRole;
import com.alttd.proxydiscordlink.config.BotConfig;
import com.alttd.proxydiscordlink.config.Config;
import com.alttd.proxydiscordlink.objects.DiscordLinkPlayer;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import net.dv8tion.jda.api.EmbedBuilder;
@ -84,6 +85,16 @@ public class Utilities {
return false;
}
public static boolean hasDatabaseNitro(DiscordLinkPlayer player) {
List<String> groups = player.getRoles();
for (String group : Config.DISCORD_GROUPS) {
if (groups.contains(group)) {
return true;
}
}
return false;
}
public static String getAuthKey() {
String randChars = "1234567890";
StringBuilder salt = new StringBuilder();
@ -206,4 +217,31 @@ public class Utilities {
.setEphemeral(true)
.queue(res -> res.deleteOriginal().queueAfter(5, TimeUnit.SECONDS));
}
public static boolean removeRole(UUID uuid, String group) {
User user = getLuckPerms().getUserManager().getUser(uuid);
if (user == null)
return false;
user.getNodes(NodeType.INHERITANCE)
.stream()
.filter(inheritanceNode -> inheritanceNode.getGroupName().equalsIgnoreCase(group))
.findAny()
.ifPresent(node ->
getLuckPerms().getUserManager().modifyUser(uuid, result -> result.data().remove(node)));
return true;
}
public static boolean addRole(UUID uuid, String group) {
User user = getLuckPerms().getUserManager().getUser(uuid);
if (user == null)
return false;
user.getNodes(NodeType.INHERITANCE)
.stream()
.filter(inheritanceNode -> inheritanceNode.getGroupName().equalsIgnoreCase(group))
.findAny()
.ifPresent(node ->
getLuckPerms().getUserManager().modifyUser(uuid, result -> result.data().add(node)));
return true;
}
}