Search in sources :

Example 1 with UserContextCommand

use of at.xirado.bean.command.context.UserContextCommand in project Bean by Xirado.

the class InteractionCommandHandler method handleUserContextCommand.

public void handleUserContextCommand(@NotNull UserContextInteractionEvent event) {
    if (!event.isFromGuild())
        return;
    Guild guild = event.getGuild();
    Member member = event.getMember();
    UserContextCommand command = null;
    if (registeredGuildCommands.containsKey(guild.getIdLong())) {
        List<UserContextCommand> guildCommands = registeredGuildCommands.get(guild.getIdLong()).stream().filter(cmd -> cmd instanceof UserContextCommand).map(cmd -> (UserContextCommand) cmd).toList();
        UserContextCommand guildCommand = guildCommands.stream().filter(cmd -> cmd.getData().getName().equalsIgnoreCase(event.getName())).findFirst().orElse(null);
        if (guildCommand != null)
            command = guildCommand;
    }
    if (command == null) {
        UserContextCommand globalCommand = getRegisteredUserContextCommands().stream().filter(cmd -> cmd.getData().getName().equalsIgnoreCase(event.getName())).findFirst().orElse(null);
        if (globalCommand != null)
            command = globalCommand;
    }
    if (command == null)
        return;
    List<Permission> neededPermissions = command.getRequiredUserPermissions();
    List<Permission> neededBotPermissions = command.getRequiredBotPermissions();
    if (neededPermissions != null && !member.hasPermission((GuildChannel) event.getChannel(), neededPermissions)) {
        event.reply(LocaleLoader.ofGuild(guild).get("general.no_perms", String.class)).queue();
        return;
    }
    if (neededBotPermissions != null && !event.getGuild().getSelfMember().hasPermission((GuildChannel) event.getChannel(), neededBotPermissions)) {
        event.reply(LocaleLoader.ofGuild(guild).get("general.no_bot_perms1", String.class)).queue();
        return;
    }
    if (command.getCommandFlags().contains(CommandFlag.MUST_BE_IN_VC)) {
        GuildVoiceState guildVoiceState = member.getVoiceState();
        if (guildVoiceState == null || !guildVoiceState.inAudioChannel()) {
            event.replyEmbeds(EmbedUtil.errorEmbed("You are not connected to a voice-channel!")).queue();
            return;
        }
    }
    if (command.getCommandFlags().contains(CommandFlag.MUST_BE_IN_SAME_VC)) {
        GuildVoiceState voiceState = member.getVoiceState();
        AudioManager manager = event.getGuild().getAudioManager();
        if (manager.isConnected()) {
            if (!manager.getConnectedChannel().equals(voiceState.getChannel())) {
                event.replyEmbeds(EmbedUtil.errorEmbed("You must be listening in " + manager.getConnectedChannel().getAsMention() + "to do this!")).setEphemeral(true).queue();
                return;
            }
        }
    }
    UserContextCommand finalCommand = command;
    Runnable r = () -> {
        try {
            finalCommand.executeCommand(event);
            Metrics.COMMANDS.labels("success").inc();
        } catch (Exception e) {
            Metrics.COMMANDS.labels("failed").inc();
            LinkedDataObject translation = event.getGuild() == null ? LocaleLoader.getForLanguage("en_US") : LocaleLoader.ofGuild(event.getGuild());
            if (event.isAcknowledged())
                event.getHook().sendMessageEmbeds(EmbedUtil.errorEmbed(translation.getString("general.unknown_error_occured"))).setEphemeral(true).queue(s -> {
                }, ex -> {
                });
            else
                event.replyEmbeds(EmbedUtil.errorEmbed(translation.getString("general.unknown_error_occured"))).setEphemeral(true).queue(s -> {
                }, ex -> {
                });
            LOGGER.error("Could not execute user-context-menu-command", e);
            EmbedBuilder builder = new EmbedBuilder().setTitle("An error occurred while executing a user-context-command!").addField("Guild", event.getGuild() == null ? "None (Direct message)" : event.getGuild().getIdLong() + " (" + event.getGuild().getName() + ")", true).addField("Channel", event.getGuild() == null ? "None (Direct message)" : event.getChannel().getName(), true).addField("User", event.getUser().getAsMention() + " (" + event.getUser().getAsTag() + ")", true).addField("Command", event.getName(), false).setColor(EmbedUtil.ERROR_COLOR);
            event.getJDA().openPrivateChannelById(Bean.OWNER_ID).flatMap(c -> c.sendMessageEmbeds(builder.build()).content("```fix\n" + ExceptionUtils.getStackTrace(e) + "\n```")).queue();
        }
    };
    Bean.getInstance().getCommandExecutor().submit(r);
}
Also used : LocaleLoader(at.xirado.bean.translation.LocaleLoader) MessageContextInteractionEvent(net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent) Permission(net.dv8tion.jda.api.Permission) AudioManager(net.dv8tion.jda.api.managers.AudioManager) LoggerFactory(org.slf4j.LoggerFactory) Util(at.xirado.bean.misc.Util) Member(net.dv8tion.jda.api.entities.Member) Command(net.dv8tion.jda.api.interactions.commands.Command) ArrayList(java.util.ArrayList) at.xirado.bean.command.slashcommands.moderation(at.xirado.bean.command.slashcommands.moderation) UserContextInteractionEvent(net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent) Guild(net.dv8tion.jda.api.entities.Guild) at.xirado.bean.command.slashcommands.music(at.xirado.bean.command.slashcommands.music) GuildVoiceState(net.dv8tion.jda.api.entities.GuildVoiceState) OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) Map(java.util.Map) GenericCommand(at.xirado.bean.command.GenericCommand) SlashCommand(at.xirado.bean.command.SlashCommand) EmbedUtil(at.xirado.bean.misc.EmbedUtil) UserContextCommand(at.xirado.bean.command.context.UserContextCommand) MockContextMenuCommand(at.xirado.bean.command.context.message.MockContextMenuCommand) Logger(org.slf4j.Logger) at.xirado.bean.command.slashcommands(at.xirado.bean.command.slashcommands) GuildChannel(net.dv8tion.jda.api.entities.GuildChannel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Metrics(at.xirado.bean.misc.Metrics) SlashCommandInteractionEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent) MessageContextCommand(at.xirado.bean.command.context.MessageContextCommand) CommandAutoCompleteInteractionEvent(net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) CommandFlag(at.xirado.bean.command.CommandFlag) at.xirado.bean.command.slashcommands.leveling(at.xirado.bean.command.slashcommands.leveling) SlashCommandContext(at.xirado.bean.command.SlashCommandContext) CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction) Bean(at.xirado.bean.Bean) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) SlapContextMenuCommand(at.xirado.bean.command.context.user.SlapContextMenuCommand) LinkedDataObject(at.xirado.bean.data.LinkedDataObject) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) UserContextCommand(at.xirado.bean.command.context.UserContextCommand) LinkedDataObject(at.xirado.bean.data.LinkedDataObject) GuildVoiceState(net.dv8tion.jda.api.entities.GuildVoiceState) Guild(net.dv8tion.jda.api.entities.Guild) AudioManager(net.dv8tion.jda.api.managers.AudioManager) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Permission(net.dv8tion.jda.api.Permission) Member(net.dv8tion.jda.api.entities.Member)

Aggregations

Bean (at.xirado.bean.Bean)1 CommandFlag (at.xirado.bean.command.CommandFlag)1 GenericCommand (at.xirado.bean.command.GenericCommand)1 SlashCommand (at.xirado.bean.command.SlashCommand)1 SlashCommandContext (at.xirado.bean.command.SlashCommandContext)1 MessageContextCommand (at.xirado.bean.command.context.MessageContextCommand)1 UserContextCommand (at.xirado.bean.command.context.UserContextCommand)1 MockContextMenuCommand (at.xirado.bean.command.context.message.MockContextMenuCommand)1 SlapContextMenuCommand (at.xirado.bean.command.context.user.SlapContextMenuCommand)1 at.xirado.bean.command.slashcommands (at.xirado.bean.command.slashcommands)1 at.xirado.bean.command.slashcommands.leveling (at.xirado.bean.command.slashcommands.leveling)1 at.xirado.bean.command.slashcommands.moderation (at.xirado.bean.command.slashcommands.moderation)1 at.xirado.bean.command.slashcommands.music (at.xirado.bean.command.slashcommands.music)1 LinkedDataObject (at.xirado.bean.data.LinkedDataObject)1 EmbedUtil (at.xirado.bean.misc.EmbedUtil)1 Metrics (at.xirado.bean.misc.Metrics)1 Util (at.xirado.bean.misc.Util)1 LocaleLoader (at.xirado.bean.translation.LocaleLoader)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1