Search in sources :

Example 1 with ReactionRole

use of at.xirado.bean.data.ReactionRole in project Bean by Xirado.

the class ReactionRoleCommand method executeCommand.

@Override
public void executeCommand(@NotNull SlashCommandInteractionEvent event, @NotNull SlashCommandContext ctx) {
    Guild guild = event.getGuild();
    Member bot = guild.getSelfMember();
    String subcommand = event.getSubcommandName();
    if (subcommand == null) {
        ctx.reply(ctx.getLocalized("commands.invalid_subcommand")).setEphemeral(true).queue();
        return;
    }
    if (subcommand.equalsIgnoreCase("remove")) {
        TextChannel channel = (TextChannel) event.getOption("channel").getAsGuildChannel();
        if (channel == null) {
            ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.channel_not_exists")).setEphemeral(true).queue();
            return;
        }
        long messageID = 0;
        try {
            messageID = Long.parseLong(event.getOption("message_id").getAsString());
        } catch (Exception e) {
            ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.message_invalid")).setEphemeral(true).queue();
            return;
        }
        channel.retrieveMessageById(messageID).queue((message) -> {
            ctx.getGuildData().removeReactionRoles(message.getIdLong()).update();
            message.clearReactions().queue(s -> {
            }, e -> {
            });
            ctx.reply(SlashCommandContext.SUCCESS + " " + ctx.getLocalized("commands.reactionroles.removed_success")).setEphemeral(true).queue();
        }, new ErrorHandler().handle(ErrorResponse.UNKNOWN_MESSAGE, (err) -> ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.message_not_exists")).setEphemeral(true).queue()).handle(EnumSet.allOf(ErrorResponse.class), (err) -> {
            ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("general.unknown_error_occured")).setEphemeral(true).queue();
            LOGGER.error("An error occurred while trying to remove reaction roles!", err);
        }));
    } else if (subcommand.equalsIgnoreCase("create")) {
        TextChannel channel = (TextChannel) event.getOption("channel").getAsGuildChannel();
        if (channel == null) {
            ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.channel_not_exists")).setEphemeral(true).queue();
            return;
        }
        long messageID = 0;
        try {
            messageID = Long.parseLong(event.getOption("message_id").getAsString());
        } catch (Exception e) {
            ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.message_invalid")).setEphemeral(true).queue();
            return;
        }
        channel.retrieveMessageById(messageID).queue((message) -> {
            Role role = event.getOption("role").getAsRole();
            if (!bot.canInteract(role)) {
                ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.cannot_interact_role", role.getAsMention())).setEphemeral(true).queue();
                return;
            }
            String emoticon = event.getOption("emote").getAsString();
            String emote = emoticon;
            Pattern pattern = Message.MentionType.EMOTE.getPattern();
            Matcher matcher = pattern.matcher(emoticon);
            if (matcher.matches()) {
                emoticon = "emote:" + matcher.group(2);
                emote = matcher.group(2);
            }
            String finalEmote = emote;
            message.addReaction(emoticon).queue((success) -> {
                ReactionRole reactionRole = new at.xirado.bean.data.ReactionRole(finalEmote, message.getIdLong(), role.getIdLong());
                ctx.getGuildData().addReactionRoles(reactionRole).update();
                ctx.reply(SlashCommandContext.SUCCESS + " " + ctx.getLocalized("commands.reactionroles.added_success")).setEphemeral(true).queue();
            }, new ErrorHandler().handle(ErrorResponse.UNKNOWN_EMOJI, (e) -> ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.emote_invalid")).setEphemeral(true).queue()).handle(EnumSet.allOf(ErrorResponse.class), (e) -> {
                ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("general.unknown_error_occured")).setEphemeral(true).queue();
                LOGGER.error("An error occurred while adding reaction-role!", e);
            }));
        }, (error) -> ctx.reply(SlashCommandContext.ERROR + " " + ctx.getLocalized("commands.message_not_exists")).setEphemeral(true).queue());
    }
}
Also used : OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) Logger(org.slf4j.Logger) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) ReactionRole(at.xirado.bean.data.ReactionRole) Permission(net.dv8tion.jda.api.Permission) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) LoggerFactory(org.slf4j.LoggerFactory) SlashCommandInteractionEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) Commands(net.dv8tion.jda.api.interactions.commands.build.Commands) Matcher(java.util.regex.Matcher) SlashCommandContext(at.xirado.bean.command.SlashCommandContext) SlashCommand(at.xirado.bean.command.SlashCommand) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) EnumSet(java.util.EnumSet) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) ErrorHandler(net.dv8tion.jda.api.exceptions.ErrorHandler) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ReactionRole(at.xirado.bean.data.ReactionRole) ErrorResponse(net.dv8tion.jda.api.requests.ErrorResponse) ReactionRole(at.xirado.bean.data.ReactionRole) ReactionRole(at.xirado.bean.data.ReactionRole)

Example 2 with ReactionRole

use of at.xirado.bean.data.ReactionRole in project Bean by Xirado.

the class MessageReactionAddListener method onMessageReactionAdd.

@Override
public void onMessageReactionAdd(@NotNull MessageReactionAddEvent e) {
    if (!e.isFromGuild())
        return;
    if (GuildJoinListener.isGuildBanned(e.getGuild().getIdLong()))
        return;
    try {
        if (e.getMember().getUser().isBot())
            return;
        Guild g = e.getGuild();
        long id = e.getMessageIdLong();
        ReactionEmote reactionemote = e.getReactionEmote();
        String reacted = reactionemote.isEmoji() ? reactionemote.getAsReactionCode() : reactionemote.getEmote().getId();
        GuildData data = GuildManager.getGuildData(e.getGuild());
        ReactionRole reactionRole = data.getReactionRoles().stream().filter(x -> x.getMessageId() == id && x.getEmote().equals(reacted)).findFirst().orElse(null);
        if (reactionRole != null) {
            Role role = e.getGuild().getRoleById(reactionRole.getRoleId());
            if (role != null)
                g.addRoleToMember(e.getMember(), role).queue(s -> {
                }, ex -> {
                });
        }
    } catch (Exception e2) {
        LOGGER.error("An error occured whilst executing reaction role event!", e2);
    }
}
Also used : Role(net.dv8tion.jda.api.entities.Role) ReactionRole(at.xirado.bean.data.ReactionRole) GuildManager(at.xirado.bean.data.GuildManager) ReactionEmote(net.dv8tion.jda.api.entities.MessageReaction.ReactionEmote) GuildData(at.xirado.bean.data.GuildData) Guild(net.dv8tion.jda.api.entities.Guild) Logger(org.slf4j.Logger) Role(net.dv8tion.jda.api.entities.Role) ReactionRole(at.xirado.bean.data.ReactionRole) LoggerFactory(org.slf4j.LoggerFactory) ListenerAdapter(net.dv8tion.jda.api.hooks.ListenerAdapter) MessageReactionAddEvent(net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent) NotNull(org.jetbrains.annotations.NotNull) GuildData(at.xirado.bean.data.GuildData) Guild(net.dv8tion.jda.api.entities.Guild) ReactionEmote(net.dv8tion.jda.api.entities.MessageReaction.ReactionEmote) ReactionRole(at.xirado.bean.data.ReactionRole)

Aggregations

ReactionRole (at.xirado.bean.data.ReactionRole)2 NotNull (org.jetbrains.annotations.NotNull)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 SlashCommand (at.xirado.bean.command.SlashCommand)1 SlashCommandContext (at.xirado.bean.command.SlashCommandContext)1 GuildData (at.xirado.bean.data.GuildData)1 GuildManager (at.xirado.bean.data.GuildManager)1 EnumSet (java.util.EnumSet)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Permission (net.dv8tion.jda.api.Permission)1 net.dv8tion.jda.api.entities (net.dv8tion.jda.api.entities)1 Guild (net.dv8tion.jda.api.entities.Guild)1 ReactionEmote (net.dv8tion.jda.api.entities.MessageReaction.ReactionEmote)1 Role (net.dv8tion.jda.api.entities.Role)1 SlashCommandInteractionEvent (net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent)1 MessageReactionAddEvent (net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent)1 ErrorHandler (net.dv8tion.jda.api.exceptions.ErrorHandler)1 ListenerAdapter (net.dv8tion.jda.api.hooks.ListenerAdapter)1