Search in sources :

Example 1 with InsufficientPermissionException

use of net.dv8tion.jda.core.exceptions.InsufficientPermissionException in project FlareBot by FlareBot.

the class Events method onMessageReactionAdd.

@Override
public void onMessageReactionAdd(MessageReactionAddEvent event) {
    if (!event.getGuild().getSelfMember().hasPermission(event.getTextChannel(), Permission.MESSAGE_READ))
        return;
    if (event.getUser().isBot())
        return;
    if (ButtonUtil.isButtonMessage(event.getMessageId())) {
        for (ButtonGroup.Button button : ButtonUtil.getButtonGroup(event.getMessageId()).getButtons()) {
            if ((event.getReactionEmote() != null && event.getReactionEmote().isEmote() && event.getReactionEmote().getIdLong() == button.getEmoteId()) || (button.getUnicode() != null && event.getReactionEmote().getName().equals(button.getUnicode()))) {
                try {
                    event.getChannel().getMessageById(event.getMessageId()).queue(message -> {
                        for (MessageReaction reaction : message.getReactions()) {
                            if (reaction.getReactionEmote().equals(event.getReactionEmote())) {
                                reaction.removeReaction(event.getUser()).queue();
                            }
                        }
                    });
                } catch (InsufficientPermissionException e) {
                    event.getGuild().getOwner().getUser().openPrivateChannel().queue(privateChannel -> {
                        privateChannel.sendMessage("I cannot remove reactions from messages in the channel: " + event.getChannel().getName() + "! Please give me the `MESSAGE_HISTORY` " + "permission to allow me to do this!").queue();
                    }, ignored -> {
                    });
                }
                button.onClick(ButtonUtil.getButtonGroup(event.getMessageId()).getOwner(), event.getUser());
                String emote = event.getReactionEmote() != null ? event.getReactionEmote().getName() + "(" + event.getReactionEmote().getId() + ")" : button.getUnicode();
                Metrics.buttonsPressed.labels(emote, ButtonUtil.getButtonGroup(event.getMessageId()).getName()).inc();
                if (!event.getGuild().getSelfMember().hasPermission(Permission.MESSAGE_MANAGE)) {
                    return;
                }
                return;
            }
        }
    }
    if (!FlareBotManager.instance().getGuild(event.getGuild().getId()).getBetaAccess())
        return;
    // Check if it's a :pushpin:
    if (!event.getReactionEmote().getName().equals("\uD83D\uDCCC"))
        return;
    event.getChannel().getMessageById(event.getMessageId()).queue(message -> {
        MessageReaction reaction = message.getReactions().stream().filter(r -> r.getReactionEmote().getName().equals(event.getReactionEmote().getName())).findFirst().orElse(null);
        if (reaction != null) {
            if (reaction.getCount() == 5) {
                message.pin().queue((aVoid) -> event.getChannel().getHistory().retrievePast(1).complete().get(0).delete().queue());
            }
        }
    });
}
Also used : Color(java.awt.Color) Arrays(java.util.Arrays) Metrics(stream.flarebot.flarebot.metrics.Metrics) GuildMemberJoinEvent(net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent) RedisController(stream.flarebot.flarebot.database.RedisController) Random(java.util.Random) ListenerAdapter(net.dv8tion.jda.core.hooks.ListenerAdapter) ModlogEvent(stream.flarebot.flarebot.mod.modlog.ModlogEvent) Message(net.dv8tion.jda.core.entities.Message) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) GuildLeaveEvent(net.dv8tion.jda.core.events.guild.GuildLeaveEvent) JSONObject(org.json.JSONObject) Permission(net.dv8tion.jda.core.Permission) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DisconnectEvent(net.dv8tion.jda.core.events.DisconnectEvent) Map(java.util.Map) Histogram(io.prometheus.client.Histogram) JDA(net.dv8tion.jda.core.JDA) ReadyEvent(net.dv8tion.jda.core.events.ReadyEvent) Role(net.dv8tion.jda.core.entities.Role) Request(okhttp3.Request) PlayerCache(stream.flarebot.flarebot.objects.PlayerCache) VoiceChannel(net.dv8tion.jda.core.entities.VoiceChannel) GuildVoiceLeaveEvent(net.dv8tion.jda.core.events.guild.voice.GuildVoiceLeaveEvent) WebUtils(stream.flarebot.flarebot.util.WebUtils) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GenericGuildMessageEvent(net.dv8tion.jda.core.events.message.guild.GenericGuildMessageEvent) Welcome(stream.flarebot.flarebot.objects.Welcome) Set(java.util.Set) VariableUtils(stream.flarebot.flarebot.util.general.VariableUtils) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) OnlineStatus(net.dv8tion.jda.core.OnlineStatus) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Event(net.dv8tion.jda.core.events.Event) SkipCommand(stream.flarebot.flarebot.commands.music.SkipCommand) Pattern(java.util.regex.Pattern) GuildVoiceMoveEvent(net.dv8tion.jda.core.events.guild.voice.GuildVoiceMoveEvent) MessageReactionAddEvent(net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent) stream.flarebot.flarebot.commands(stream.flarebot.flarebot.commands) StatusChangeEvent(net.dv8tion.jda.core.events.StatusChangeEvent) RandomUtils(stream.flarebot.flarebot.util.RandomUtils) TextChannel(net.dv8tion.jda.core.entities.TextChannel) UserUpdateOnlineStatusEvent(net.dv8tion.jda.core.events.user.update.UserUpdateOnlineStatusEvent) ButtonUtil(stream.flarebot.flarebot.util.buttons.ButtonUtil) LocalDateTime(java.time.LocalDateTime) ModlogHandler(stream.flarebot.flarebot.mod.modlog.ModlogHandler) HashMap(java.util.HashMap) GuildWrapper(stream.flarebot.flarebot.objects.GuildWrapper) GeneralUtils(stream.flarebot.flarebot.util.general.GeneralUtils) RequestBody(okhttp3.RequestBody) VoteUtil(stream.flarebot.flarebot.util.votes.VoteUtil) ArrayList(java.util.ArrayList) UpdateCommand(stream.flarebot.flarebot.commands.secret.update.UpdateCommand) GuildVoiceJoinEvent(net.dv8tion.jda.core.events.guild.voice.GuildVoiceJoinEvent) RoleDeleteEvent(net.dv8tion.jda.core.events.role.RoleDeleteEvent) Constants(stream.flarebot.flarebot.util.Constants) MessageUtils(stream.flarebot.flarebot.util.MessageUtils) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) GuildUtils(stream.flarebot.flarebot.util.general.GuildUtils) ExecutorService(java.util.concurrent.ExecutorService) ButtonGroup(stream.flarebot.flarebot.util.objects.ButtonGroup) Logger(org.slf4j.Logger) InsufficientPermissionException(net.dv8tion.jda.core.exceptions.InsufficientPermissionException) MessageReaction(net.dv8tion.jda.core.entities.MessageReaction) GuildJoinEvent(net.dv8tion.jda.core.events.guild.GuildJoinEvent) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) TimeUnit(java.util.concurrent.TimeUnit) Guild(net.dv8tion.jda.core.entities.Guild) Markers(stream.flarebot.flarebot.util.errorhandling.Markers) MessageReaction(net.dv8tion.jda.core.entities.MessageReaction) ButtonGroup(stream.flarebot.flarebot.util.objects.ButtonGroup) InsufficientPermissionException(net.dv8tion.jda.core.exceptions.InsufficientPermissionException)

Aggregations

Histogram (io.prometheus.client.Histogram)1 Color (java.awt.Color)1 LocalDateTime (java.time.LocalDateTime)1 OffsetDateTime (java.time.OffsetDateTime)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Random (java.util.Random)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1 JDA (net.dv8tion.jda.core.JDA)1