added /sync

This commit is contained in:
XTerPL 2025-02-03 22:44:33 +01:00
parent 4d807ca5ce
commit 30c7e4151f
8 changed files with 64 additions and 25 deletions

View file

@ -42,12 +42,12 @@ public class UnlinkCommand implements CommandExecutor {
StringBuilder embedDescription = new StringBuilder();
DiscordUser user = whitelist.getDiscordUser(whitelistedPlayer.discordUser);
boolean noMorePrimary = user == null || user.favoriteAccount == null;
embedDescription.append("You have been removed from the whitelist and unlinked from your discord account.\n");
if(removingPrimary) {
DiscordUser user = whitelist.getDiscordUser(whitelistedPlayer.discordUser);
boolean noMorePrimary = user == null || user.favoriteAccount == null;
if(noMorePrimary) {
embedDescription.append("You no longer have any players linked to this discord account.\n");
embedDescription.append("This means that in order to continue playing, you need to link a new account");
@ -56,8 +56,11 @@ public class UnlinkCommand implements CommandExecutor {
embedDescription.append("This account was your primary account up to now. Sending messages in the chat link channel will now send as ").append(whitelist.getWhitelistedPlayer(user.favoriteAccount).lastKnownName);
}
}
else if(noMorePrimary) {
embedDescription.append("You never had a primary account in the first place. How.");
}
else {
embedDescription.append("Your primary account was unaffected. Sending messages in the chat link channel will still send as ").append(whitelist.getWhitelistedPlayer(whitelist.getDiscordUser(whitelistedPlayer.discordUser).favoriteAccount).lastKnownName);
embedDescription.append("Your primary account was unaffected. Sending messages in the chat link channel will still send as ").append(whitelist.getWhitelistedPlayer(user.favoriteAccount).lastKnownName);
}
embedDescription.append(".\n");
embedDescription.append("To change your primary account, run `/setprimary` (in discord).\n");

View file

@ -66,7 +66,7 @@ public class DiscordApp extends ListenerAdapter {
);
private final List<ICommand> whitelistCommands = List.of(
new WhitelistCommand(), new UnlinkCommand(), new SetPrimaryCommand(), new LinksCommand()
new WhitelistCommand(), new UnlinkCommand(), new SetPrimaryCommand(), new LinksCommand(), new SyncCommand()
);

View file

@ -45,6 +45,14 @@ public class WhitelistManagement {
return whitelist.hasData(uuid);
}
public DiscordUser updateUser(User user) {
DiscordUser discordUser = getDiscordUser(user.getIdLong());
if(discordUser == null) {
return updateUser(user, null);
}
return updateUser(user, discordUser.favoriteAccount);
}
public DiscordUser updateUser(User user, UUID primary) {
DiscordUser discordUser = new DiscordUser();
discordUser.snowflake = user.getIdLong();

View file

@ -28,11 +28,7 @@ public class LinksCommand implements ICommand {
if (!DiscordApp.isWhitelistManaged()) throw new IllegalStateException("Whitelist is not managed, but /links was called");
WhitelistManagement whitelist = DiscordApp.getWhitelistManagement();
DiscordUser user = whitelist.getDiscordUser(event.getUser().getIdLong());
if(user == null) {
user = whitelist.updateUser(event.getUser(), null);
}
DiscordUser user = whitelist.updateUser(event.getUser());
List<WhitelistedPlayer> whitelistedPlayers = whitelist.getWhitelistedPlayersOfDiscordUser(user);

View file

@ -44,11 +44,7 @@ public class SetPrimaryCommand implements ICommand {
username = whitelistedPlayer.lastKnownName;
DiscordUser user = whitelist.getDiscordUser(event.getUser().getIdLong());
if(user == null) {
user = whitelist.updateUser(event.getUser(), null);
}
DiscordUser user = whitelist.updateUser(event.getUser());
if(whitelistedPlayer.uuid.equals(user.favoriteAccount)) {
event.reply("The specified player is already your primary account.").setEphemeral(true).queue();

View file

@ -0,0 +1,34 @@
package de.blazemcworld.blazinggames.discord.commands;
import de.blazemcworld.blazinggames.discord.DiscordApp;
import de.blazemcworld.blazinggames.discord.WhitelistManagement;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
public class SyncCommand implements ICommand {
@Override
public String name() {
return "sync";
}
@Override
public String description() {
return "Updates the data that the server and the bot know about you.";
}
@SuppressWarnings("null")
@Override
public void handle(SlashCommandInteractionEvent event) {
if (!DiscordApp.isWhitelistManaged()) throw new IllegalStateException("Whitelist is not managed, but /sync was called");
WhitelistManagement whitelist = DiscordApp.getWhitelistManagement();
whitelist.updateUser(event.getUser());
event.reply("Successfully updated the data that the server and the bot know about you!").setEphemeral(true).queue();
}
@Override
public OptionData[] getOptions() {
return new OptionData[] {};
}
}

View file

@ -46,22 +46,24 @@ public class UnlinkCommand implements ICommand {
StringBuilder embedDescription = new StringBuilder();
DiscordUser user = whitelist.updateUser(event.getUser());
boolean noMorePrimary = user == null || user.favoriteAccount == null;
embedDescription.append("The player ").append(username).append(" has been removed from the whitelist and unlinked from your discord account.\n\n");
if(removingPrimary) {
DiscordUser user = whitelist.getDiscordUser(event.getUser().getIdLong());
boolean noMorePrimary = user == null || user.favoriteAccount == null;
if(noMorePrimary) {
embedDescription.append("You no longer have any players linked to this discord account. ");
embedDescription.append("This means that in order to continue playing, you need to link a new account");
}
else {
embedDescription.append("The player ").append(username).append(" was your primary account up to now. Sending messages in the chat link channel will now send as ").append(whitelist.getWhitelistedPlayer(whitelist.getDiscordUser(event.getMember().getIdLong()).favoriteAccount).lastKnownName);
embedDescription.append("The player ").append(username).append(" was your primary account up to now. Sending messages in the chat link channel will now send as ").append(whitelist.getWhitelistedPlayer(user.favoriteAccount).lastKnownName);
}
}
else if(noMorePrimary) {
embedDescription.append("You never had a primary account in the first place. How.");
}
else {
embedDescription.append("Your primary account was unaffected. Sending messages in the chat link channel will still send as ").append(whitelist.getWhitelistedPlayer(whitelist.getDiscordUser(event.getMember().getIdLong()).favoriteAccount).lastKnownName);
embedDescription.append("Your primary account was unaffected. Sending messages in the chat link channel will still send as ").append(whitelist.getWhitelistedPlayer(user.favoriteAccount).lastKnownName);
}
embedDescription.append(".\n\n");
embedDescription.append("* To change your primary account, run `/setprimary` (in discord).\n");

View file

@ -40,7 +40,7 @@ public class WhitelistCommand implements ICommand {
boolean isNewPrimary;
DiscordUser discordUser = whitelist.getDiscordUser(event.getMember().getIdLong());
DiscordUser discordUser = whitelist.updateUser(event.getUser());
if (discordUser == null || discordUser.favoriteAccount == null) {
isNewPrimary = true;
@ -54,7 +54,7 @@ public class WhitelistCommand implements ICommand {
whitelist.updatePlayerLastKnownName(uuid, username);
if(isNewPrimary) {
whitelist.updateUser(event.getUser(), uuid);
discordUser = whitelist.updateUser(event.getUser(), uuid);
}
whitelist.invalidateLinkCode(code);
@ -64,7 +64,7 @@ public class WhitelistCommand implements ICommand {
if (isNewPrimary) {
embedDescription.append("This is your new primary account. You may send messages in the chat link channel with this account and they will send as ").append(username);
} else {
embedDescription.append("Your primary account was unaffected. Sending messages in the chat link channel will still send as ").append(whitelist.getWhitelistedPlayer(whitelist.getDiscordUser(event.getMember().getIdLong()).favoriteAccount).lastKnownName);
embedDescription.append("Your primary account was unaffected. Sending messages in the chat link channel will still send as ").append(whitelist.getWhitelistedPlayer(discordUser.favoriteAccount).lastKnownName);
}
embedDescription.append(".\n\n");
embedDescription.append("* To change your primary account, run `/setprimary` (in discord).\n");