diff --git a/gradle.properties b/gradle.properties index 4c174c9..e7ace85 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ version_major=0 version_minor=0 -version_patch=17 +version_patch=18 shade_group=com.hypherionmc.sdlink.shaded. diff --git a/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java b/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java index 0b37188..59080fc 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java +++ b/src/main/java/com/hypherionmc/sdlink/core/accounts/MinecraftAccount.java @@ -108,6 +108,10 @@ public class MinecraftAccount { * @param guild The server the command is run from */ public Result linkAccount(Member member, Guild guild) { + return this.linkAccount(member, guild, true); + } + + public Result linkAccount(Member member, Guild guild, boolean updateNick) { if (getStoredAccount() == null) { return Result.error("We couldn't link your Minecraft and Discord Accounts together. If this error persists, please ask a staff member for help"); } @@ -130,7 +134,7 @@ public class MinecraftAccount { String finalnickname = SDLinkConfig.INSTANCE.whitelistingAndLinking.accountLinking.nicknameFormat.replace("%nick%", nickname).replace("%mcname%", suffix); - if (SDLinkConfig.INSTANCE.whitelistingAndLinking.accountLinking.changeNickname) { + if (SDLinkConfig.INSTANCE.whitelistingAndLinking.accountLinking.changeNickname && updateNick) { try { member.modifyNickname(finalnickname).queue(); @@ -332,7 +336,9 @@ public class MinecraftAccount { */ public SDLinkAccount getStoredAccount() { sdlinkDatabase.reloadCollection("accounts"); - return sdlinkDatabase.findById(this.uuid.toString(), SDLinkAccount.class); + SDLinkAccount account = sdlinkDatabase.findById(this.uuid.toString(), SDLinkAccount.class); + + return account == null ? newDBEntry() : account; } /** diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/CommandManager.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/CommandManager.java index 5563391..944c3aa 100644 --- a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/CommandManager.java +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/CommandManager.java @@ -8,14 +8,8 @@ import com.hypherionmc.sdlink.core.config.SDLinkConfig; import com.hypherionmc.sdlink.core.discord.commands.slash.general.HelpSlashCommand; import com.hypherionmc.sdlink.core.discord.commands.slash.general.PlayerListSlashCommand; import com.hypherionmc.sdlink.core.discord.commands.slash.general.ServerStatusSlashCommand; -import com.hypherionmc.sdlink.core.discord.commands.slash.linking.ConfirmAccountLinkSlashCommand; -import com.hypherionmc.sdlink.core.discord.commands.slash.linking.LinkAccountCommand; -import com.hypherionmc.sdlink.core.discord.commands.slash.linking.UnlinkAccountSlashCommand; -import com.hypherionmc.sdlink.core.discord.commands.slash.linking.ViewLinkedAccountsCommand; -import com.hypherionmc.sdlink.core.discord.commands.slash.whitelist.ConfirmWhitelistSlashCommand; -import com.hypherionmc.sdlink.core.discord.commands.slash.whitelist.UnWhitelistAccountSlashCommand; -import com.hypherionmc.sdlink.core.discord.commands.slash.whitelist.ViewWhitelistedAccountsSlashCommand; -import com.hypherionmc.sdlink.core.discord.commands.slash.whitelist.WhitelistAccountCommand; +import com.hypherionmc.sdlink.core.discord.commands.slash.linking.*; +import com.hypherionmc.sdlink.core.discord.commands.slash.whitelist.*; import com.jagrosh.jdautilities.command.CommandClient; import com.jagrosh.jdautilities.command.SlashCommand; @@ -61,6 +55,12 @@ public class CommandManager { // Enable the Help command commands.add(new HelpSlashCommand()); + + // Staff commands + commands.add(new StaffLinkCommand()); + commands.add(new StaffUnlinkCommand()); + commands.add(new StaffWhitelist()); + commands.add(new StaffUnwhitelist()); } /** diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/linking/StaffLinkCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/linking/StaffLinkCommand.java new file mode 100644 index 0000000..b2653f8 --- /dev/null +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/linking/StaffLinkCommand.java @@ -0,0 +1,47 @@ +package com.hypherionmc.sdlink.core.discord.commands.slash.linking; + +import com.hypherionmc.sdlink.core.accounts.MinecraftAccount; +import com.hypherionmc.sdlink.core.discord.commands.slash.SDLinkSlashCommand; +import com.hypherionmc.sdlink.core.messaging.Result; +import com.jagrosh.jdautilities.command.SlashCommandEvent; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; + +import java.util.ArrayList; +import java.util.List; + +public class StaffLinkCommand extends SDLinkSlashCommand { + + public StaffLinkCommand() { + super(true); + this.name = "stafflink"; + this.help = "Allow staff members to link other minecraft players, without verification"; + + List options = new ArrayList<>() {{ + add(new OptionData(OptionType.USER, "discorduser", "The discord user the minecraft account belongs to").setRequired(true)); + add(new OptionData(OptionType.STRING, "mcname", "The minecraft account to link to the user").setRequired(true)); + }}; + + this.options = options; + } + + @Override + protected void execute(SlashCommandEvent event) { + String mcname = event.getOption("mcname").getAsString(); + User user = event.getOption("discorduser").getAsUser(); + + Member member = event.getGuild().getMemberById(user.getId()); + + if (member == null) { + event.reply(user.getEffectiveName() + " is not a member of this discord server").setEphemeral(true).queue(); + return; + } + + MinecraftAccount minecraftAccount = MinecraftAccount.standard(mcname); + Result result = minecraftAccount.linkAccount(member, event.getGuild(), false); + event.reply(result.getMessage()).setEphemeral(true).queue(); + } + +} diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/linking/StaffUnlinkCommand.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/linking/StaffUnlinkCommand.java new file mode 100644 index 0000000..75cd658 --- /dev/null +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/linking/StaffUnlinkCommand.java @@ -0,0 +1,59 @@ +package com.hypherionmc.sdlink.core.discord.commands.slash.linking; + +import com.hypherionmc.sdlink.core.accounts.MinecraftAccount; +import com.hypherionmc.sdlink.core.database.SDLinkAccount; +import com.hypherionmc.sdlink.core.discord.commands.slash.SDLinkSlashCommand; +import com.hypherionmc.sdlink.core.messaging.Result; +import com.jagrosh.jdautilities.command.SlashCommandEvent; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static com.hypherionmc.sdlink.core.managers.DatabaseManager.sdlinkDatabase; + +public class StaffUnlinkCommand extends SDLinkSlashCommand { + + public StaffUnlinkCommand() { + super(false); + this.name = "staffunlink"; + this.help = "Unlink another linked Discord and Minecraft account"; + + List options = new ArrayList<>() {{ + add(new OptionData(OptionType.USER, "discorduser", "The discord user the minecraft account belongs to").setRequired(true)); + add(new OptionData(OptionType.STRING, "mcname", "The minecraft account of the linked user").setRequired(true)); + }}; + + this.options = options; + } + + @Override + protected void execute(SlashCommandEvent event) { + sdlinkDatabase.reloadCollection("accounts"); + List accounts = sdlinkDatabase.findAll(SDLinkAccount.class); + + if (accounts.isEmpty()) { + event.reply("Sorry, but this server does not contain any stored players in its database").setEphemeral(true).queue(); + return; + } + + String mcname = event.getOption("mcname").getAsString(); + User user = event.getOption("discorduser").getAsUser(); + + Member member = event.getGuild().getMemberById(user.getId()); + + if (member == null) { + event.reply(user.getEffectiveName() + " is not a member of this discord server").setEphemeral(true).queue(); + return; + } + + MinecraftAccount minecraftAccount = MinecraftAccount.standard(mcname); + Result result = minecraftAccount.unlinkAccount(member, event.getGuild()); + event.reply(result.getMessage()).setEphemeral(true).queue(); + } + +} diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/whitelist/StaffUnwhitelist.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/whitelist/StaffUnwhitelist.java new file mode 100644 index 0000000..f52319f --- /dev/null +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/whitelist/StaffUnwhitelist.java @@ -0,0 +1,58 @@ +package com.hypherionmc.sdlink.core.discord.commands.slash.whitelist; + +import com.hypherionmc.sdlink.core.accounts.MinecraftAccount; +import com.hypherionmc.sdlink.core.database.SDLinkAccount; +import com.hypherionmc.sdlink.core.discord.commands.slash.SDLinkSlashCommand; +import com.hypherionmc.sdlink.core.messaging.Result; +import com.jagrosh.jdautilities.command.SlashCommandEvent; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; + +import java.util.ArrayList; +import java.util.List; + +import static com.hypherionmc.sdlink.core.managers.DatabaseManager.sdlinkDatabase; + +public class StaffUnwhitelist extends SDLinkSlashCommand { + + public StaffUnwhitelist() { + super(false); + this.name = "staffunwhitelist"; + this.help = "Unwhitelist another linked Discord and Minecraft account"; + + List options = new ArrayList<>() {{ + add(new OptionData(OptionType.USER, "discorduser", "The discord user the minecraft account belongs to").setRequired(true)); + add(new OptionData(OptionType.STRING, "mcname", "The minecraft account of the whitelisted user").setRequired(true)); + }}; + + this.options = options; + } + + @Override + protected void execute(SlashCommandEvent event) { + sdlinkDatabase.reloadCollection("accounts"); + List accounts = sdlinkDatabase.findAll(SDLinkAccount.class); + + if (accounts.isEmpty()) { + event.reply("Sorry, but this server does not contain any stored players in its database").setEphemeral(true).queue(); + return; + } + + String mcname = event.getOption("mcname").getAsString(); + User user = event.getOption("discorduser").getAsUser(); + + Member member = event.getGuild().getMemberById(user.getId()); + + if (member == null) { + event.reply(user.getEffectiveName() + " is not a member of this discord server").setEphemeral(true).queue(); + return; + } + + MinecraftAccount minecraftAccount = MinecraftAccount.standard(mcname); + Result result = minecraftAccount.unwhitelistAccount(member, event.getGuild()); + event.reply(result.getMessage()).setEphemeral(true).queue(); + } + +} diff --git a/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/whitelist/StaffWhitelist.java b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/whitelist/StaffWhitelist.java new file mode 100644 index 0000000..64c5912 --- /dev/null +++ b/src/main/java/com/hypherionmc/sdlink/core/discord/commands/slash/whitelist/StaffWhitelist.java @@ -0,0 +1,47 @@ +package com.hypherionmc.sdlink.core.discord.commands.slash.whitelist; + +import com.hypherionmc.sdlink.core.accounts.MinecraftAccount; +import com.hypherionmc.sdlink.core.discord.commands.slash.SDLinkSlashCommand; +import com.hypherionmc.sdlink.core.messaging.Result; +import com.jagrosh.jdautilities.command.SlashCommandEvent; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; + +import java.util.ArrayList; +import java.util.List; + +public class StaffWhitelist extends SDLinkSlashCommand { + + public StaffWhitelist() { + super(true); + this.name = "staffwhitelist"; + this.help = "Allow staff members to whitelist other minecraft players, without verification"; + + List options = new ArrayList<>() {{ + add(new OptionData(OptionType.USER, "discorduser", "The discord user the minecraft account belongs to").setRequired(true)); + add(new OptionData(OptionType.STRING, "mcname", "The minecraft account to link to the user").setRequired(true)); + }}; + + this.options = options; + } + + @Override + protected void execute(SlashCommandEvent event) { + String mcname = event.getOption("mcname").getAsString(); + User user = event.getOption("discorduser").getAsUser(); + + Member member = event.getGuild().getMemberById(user.getId()); + + if (member == null) { + event.reply(user.getEffectiveName() + " is not a member of this discord server").setEphemeral(true).queue(); + return; + } + + MinecraftAccount minecraftAccount = MinecraftAccount.standard(mcname); + Result result = minecraftAccount.whitelistAccount(member, event.getGuild()); + event.reply(result.getMessage()).setEphemeral(true).queue(); + } + +}