Added Nick and Unlink command
This commit is contained in:
parent
9f389a8a12
commit
cfeb04fe6e
|
|
@ -1,6 +1,8 @@
|
|||
package com.alttd.proxydiscordlink.bot.commandManager;
|
||||
|
||||
import com.alttd.proxydiscordlink.bot.commandManager.commands.CommandLink;
|
||||
import com.alttd.proxydiscordlink.bot.commandManager.commands.CommandUnlink;
|
||||
import com.alttd.proxydiscordlink.bot.commandManager.commands.NickCommand.CommandNick;
|
||||
import com.alttd.proxydiscordlink.util.ALogger;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
|
|
@ -20,7 +22,9 @@ public class CommandManager extends ListenerAdapter {
|
|||
public CommandManager(JDA jda/*, ChatListener chatListener*/) {
|
||||
ALogger.info("Loading commands...");
|
||||
commands = List.of(
|
||||
new CommandLink(jda)
|
||||
new CommandLink(jda),
|
||||
new CommandUnlink(jda),
|
||||
new CommandNick(jda)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
package com.alttd.proxydiscordlink.bot.commandManager;
|
||||
|
||||
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class SubCommand extends SubOption{
|
||||
|
||||
private final SubCommandGroup parentGroup;
|
||||
private final boolean inSubGroup;
|
||||
|
||||
protected SubCommand(SubCommandGroup parentGroup, DiscordCommand parent) {
|
||||
super(parent);
|
||||
this.parentGroup = parentGroup;
|
||||
this.inSubGroup = parentGroup != null;
|
||||
}
|
||||
|
||||
public SubCommandGroup getParentGroup() {
|
||||
return parentGroup;
|
||||
}
|
||||
|
||||
public boolean isInSubGroup() {
|
||||
return inSubGroup;
|
||||
}
|
||||
|
||||
public void suggest(CommandAutoCompleteInteractionEvent event) {
|
||||
event.replyChoices(new ArrayList<>()).queue();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
package com.alttd.proxydiscordlink.bot.commandManager;
|
||||
|
||||
public abstract class SubCommandGroup extends SubOption{
|
||||
protected SubCommandGroup(DiscordCommand parent) {
|
||||
super(parent);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
package com.alttd.proxydiscordlink.bot.commandManager;
|
||||
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
|
||||
public abstract class SubOption {
|
||||
private final DiscordCommand parent;
|
||||
|
||||
protected SubOption(DiscordCommand parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public DiscordCommand getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public abstract String getName();
|
||||
|
||||
public abstract void execute(SlashCommandInteractionEvent event);
|
||||
|
||||
public abstract String getHelpMessage();
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
package com.alttd.proxydiscordlink.bot.commandManager.commands;
|
||||
|
||||
import com.alttd.proxydiscordlink.bot.commandManager.DiscordCommand;
|
||||
import com.alttd.proxydiscordlink.config.BotConfig;
|
||||
import com.alttd.proxydiscordlink.objects.DiscordLinkPlayer;
|
||||
import com.alttd.proxydiscordlink.util.Utilities;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.interactions.commands.DefaultMemberPermissions;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.Commands;
|
||||
|
||||
public class CommandUnlink extends DiscordCommand {
|
||||
private final CommandData commandData;
|
||||
public CommandUnlink(JDA jda) {
|
||||
commandData = Commands.slash(getName(), "Unlink your Discord and Altitude Minecraft accounts")
|
||||
.setDefaultPermissions(DefaultMemberPermissions.ENABLED);
|
||||
|
||||
Utilities.registerCommand(jda, commandData);
|
||||
}
|
||||
@Override
|
||||
public String getName() {
|
||||
return "unlink";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(SlashCommandInteractionEvent event) {
|
||||
Member member = event.getMember();
|
||||
if (member == null) {
|
||||
Utilities.commandErrAutoRem("Unable to find you", event);
|
||||
return;
|
||||
}
|
||||
|
||||
DiscordLinkPlayer discordLinkPlayer = DiscordLinkPlayer.getDiscordLinkPlayer(member.getIdLong());
|
||||
if (discordLinkPlayer == null) {
|
||||
Utilities.commandErrAutoRem("Your accounts aren't linked", event);
|
||||
return;
|
||||
}
|
||||
discordLinkPlayer.unlinkDiscordLinkPlayer();
|
||||
event.replyEmbeds(Utilities.genericSuccessEmbed("Success", "Your Discord and Minecraft accounts have been unlinked."))
|
||||
.setEphemeral(true)
|
||||
.queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandData getCommandData() {
|
||||
return commandData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getChannelId() {
|
||||
return BotConfig.DISCORD.LINK_CHANNEL;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
package com.alttd.proxydiscordlink.bot.commandManager.commands.NickCommand;
|
||||
|
||||
import com.alttd.proxydiscordlink.DiscordLink;
|
||||
import com.alttd.proxydiscordlink.bot.commandManager.DiscordCommand;
|
||||
import com.alttd.proxydiscordlink.bot.commandManager.SubOption;
|
||||
import com.alttd.proxydiscordlink.objects.DiscordLinkPlayer;
|
||||
import com.alttd.proxydiscordlink.util.ALogger;
|
||||
import com.alttd.proxydiscordlink.util.Utilities;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
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 java.util.HashMap;
|
||||
|
||||
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)
|
||||
.setDefaultPermissions(DefaultMemberPermissions.ENABLED);
|
||||
Utilities.registerSubOptions(subOptionsMap,
|
||||
new SubCommandUserName(null,this),
|
||||
new SubCommandNick(null,this)
|
||||
);
|
||||
Utilities.registerCommand(jda, commandData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "nick";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(SlashCommandInteractionEvent event) {
|
||||
if (event.getGuild() == null || event.getMember() == null) {
|
||||
Utilities.commandErrAutoRem("This command can only be used within a guild: " + getName(), event);
|
||||
return;
|
||||
}
|
||||
|
||||
String subcommandName = event.getInteraction().getSubcommandGroup();
|
||||
subcommandName = subcommandName == null ? event.getInteraction().getSubcommandName() : subcommandName;
|
||||
if (subcommandName == null) {
|
||||
ALogger.error("No subcommand found for " + getName());
|
||||
return;
|
||||
}
|
||||
|
||||
SubOption subOption = subOptionsMap.get(subcommandName);
|
||||
if (subOption == null) {
|
||||
event.replyEmbeds(Utilities.invalidSubcommand(subcommandName))
|
||||
.setEphemeral(true)
|
||||
.queue();
|
||||
return;
|
||||
}
|
||||
|
||||
subOption.execute(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandData getCommandData() {
|
||||
return commandData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getChannelId() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String setNickname(SlashCommandInteractionEvent event, boolean hasNick) {
|
||||
Member member = event.getMember();
|
||||
if (member == null) {
|
||||
Utilities.commandErrAutoRem("This command can only be run in a guild.", event);
|
||||
return null;
|
||||
}
|
||||
DiscordLinkPlayer discordLinkPlayer = DiscordLinkPlayer.getDiscordLinkPlayer(member.getIdLong());
|
||||
if (discordLinkPlayer == null) {
|
||||
Utilities.commandErrAutoRem("You aren't linked, please link before using this command.", event);
|
||||
return null;
|
||||
}
|
||||
|
||||
String nick;
|
||||
if (hasNick) {
|
||||
nick = DiscordLink.getPlugin().getDatabase().getNick(discordLinkPlayer.getUuid());
|
||||
if (nick == null || nick.isBlank())
|
||||
nick = discordLinkPlayer.getUsername();
|
||||
} else {
|
||||
nick = discordLinkPlayer.getUsername();
|
||||
}
|
||||
member.modifyNickname(nick).queue();
|
||||
discordLinkPlayer.setNick(hasNick);
|
||||
DiscordLink.getPlugin().getDatabase().syncPlayer(discordLinkPlayer);
|
||||
return nick;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package com.alttd.proxydiscordlink.bot.commandManager.commands.NickCommand;
|
||||
|
||||
import com.alttd.proxydiscordlink.bot.commandManager.DiscordCommand;
|
||||
import com.alttd.proxydiscordlink.bot.commandManager.SubCommand;
|
||||
import com.alttd.proxydiscordlink.bot.commandManager.SubCommandGroup;
|
||||
import com.alttd.proxydiscordlink.util.Utilities;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
|
||||
public class SubCommandNick extends SubCommand {
|
||||
protected SubCommandNick(SubCommandGroup parentGroup, DiscordCommand parent) {
|
||||
super(parentGroup, parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "nickname";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(SlashCommandInteractionEvent event) {
|
||||
if (!(getParent() instanceof CommandNick commandNick)) {
|
||||
Utilities.commandErrAutoRem("Couldn't find parent command", event);
|
||||
return;
|
||||
}
|
||||
|
||||
String resultingName = commandNick.setNickname(event, true);
|
||||
event.replyEmbeds(Utilities.genericSuccessEmbed("Success",
|
||||
"Your nickname has been set to `" + resultingName + "`."))
|
||||
.setEphemeral(true).queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
package com.alttd.proxydiscordlink.bot.commandManager.commands.NickCommand;
|
||||
|
||||
import com.alttd.proxydiscordlink.bot.commandManager.DiscordCommand;
|
||||
import com.alttd.proxydiscordlink.bot.commandManager.SubCommand;
|
||||
import com.alttd.proxydiscordlink.bot.commandManager.SubCommandGroup;
|
||||
import com.alttd.proxydiscordlink.util.Utilities;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
|
||||
public class SubCommandUserName extends SubCommand {
|
||||
protected SubCommandUserName(SubCommandGroup parentGroup, DiscordCommand parent) {
|
||||
super(parentGroup, parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "username";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(SlashCommandInteractionEvent event) {
|
||||
if (!(getParent() instanceof CommandNick commandNick)) {
|
||||
Utilities.commandErrAutoRem("Couldn't find parent command", event);
|
||||
return;
|
||||
}
|
||||
|
||||
String resultingName = commandNick.setNickname(event, false);
|
||||
event.replyEmbeds(Utilities.genericSuccessEmbed("Success",
|
||||
"Your nickname has been set to `" + resultingName + "`."))
|
||||
.setEphemeral(true).queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpMessage() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package com.alttd.proxydiscordlink.util;
|
||||
|
||||
import com.alttd.proxydiscordlink.DiscordLink;
|
||||
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;
|
||||
|
|
@ -12,6 +13,7 @@ import net.dv8tion.jda.api.entities.Guild;
|
|||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.api.entities.Role;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import net.dv8tion.jda.api.requests.RestAction;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
|
|
@ -24,6 +26,7 @@ import net.luckperms.api.node.types.InheritanceNode;
|
|||
import java.awt.*;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Utilities {
|
||||
|
|
@ -153,6 +156,19 @@ public class Utilities {
|
|||
guild.upsertCommand(commandData).queue(RestAction.getDefaultSuccess(), Utilities::handleFailure);
|
||||
}
|
||||
|
||||
public static void registerSubOptions(HashMap<String, SubOption> subCommandMap, SubOption... subOptions) {
|
||||
for (SubOption subOption : subOptions)
|
||||
subCommandMap.put(subOption.getName(), subOption);
|
||||
}
|
||||
|
||||
public static MessageEmbed invalidSubcommand(String subcommandName) {
|
||||
return new EmbedBuilder()
|
||||
.setTitle("Invalid sub command")
|
||||
.setDescription("This is not a valid sub command: " + subcommandName)
|
||||
.setColor(Color.RED)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static MessageEmbed genericErrorEmbed(String title, String desc) {
|
||||
return new EmbedBuilder()
|
||||
.setTitle(title)
|
||||
|
|
@ -184,4 +200,10 @@ public class Utilities {
|
|||
public static void handleFailure(Throwable failure) {
|
||||
ALogger.error(failure.getMessage());
|
||||
}
|
||||
|
||||
public static void commandErrAutoRem(String text, SlashCommandInteractionEvent event) {
|
||||
event.replyEmbeds(Utilities.genericErrorEmbed("Error", text))
|
||||
.setEphemeral(true)
|
||||
.queue(res -> res.deleteOriginal().queueAfter(5, TimeUnit.SECONDS));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user