Search in sources :

Example 61 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel in project DiscordSRV by Scarsz.

the class WebhookUtil method getWebhookToUseForChannel.

public static Webhook getWebhookToUseForChannel(TextChannel channel, String targetName) {
    synchronized (lastUsedWebhooks) {
        List<Webhook> webhooks = new ArrayList<>();
        channel.getGuild().getWebhooks().complete().stream().filter(webhook -> webhook.getName().startsWith("DiscordSRV #" + channel.getName() + " #")).forEach(webhooks::add);
        if (webhooks.size() != 2) {
            webhooks.forEach(webhook -> webhook.delete().reason("Purging orphaned webhook").queue());
            webhooks.clear();
            if (!channel.getGuild().getMember(channel.getJDA().getSelfUser()).hasPermission(Permission.MANAGE_WEBHOOKS)) {
                DiscordSRV.error("Can't create a webhook to deliver chat message, bot is missing permission \"Manage Webhooks\"");
                return null;
            }
            // create webhooks to use
            Webhook webhook1 = createWebhook(channel.getGuild(), channel, "DiscordSRV " + channel.getId() + " #1");
            Webhook webhook2 = createWebhook(channel.getGuild(), channel, "DiscordSRV " + channel.getId() + " #2");
            if (webhook1 == null || webhook2 == null)
                return null;
            webhooks.add(webhook1);
            webhooks.add(webhook2);
        }
        LastWebhookInfo info = lastUsedWebhooks.getOrDefault(channel, null);
        Webhook target;
        if (info == null) {
            target = webhooks.get(0);
            lastUsedWebhooks.put(channel, new LastWebhookInfo(target.getId(), targetName));
            return target;
        }
        target = info.targetName.equals(targetName) ? webhooks.get(0).getId().equals(info.webhook) ? webhooks.get(0) : webhooks.get(1) : webhooks.get(0).getId().equals(info.webhook) ? webhooks.get(0) : webhooks.get(1);
        lastUsedWebhooks.put(channel, new LastWebhookInfo(target.getId(), targetName));
        return target;
    }
}
Also used : TextChannel(net.dv8tion.jda.core.entities.TextChannel) HashMap(java.util.HashMap) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) Webhook(net.dv8tion.jda.core.entities.Webhook) Guild(net.dv8tion.jda.core.entities.Guild) Unirest(com.mashape.unirest.http.Unirest) List(java.util.List) Permission(net.dv8tion.jda.core.Permission) DiscordSRV(github.scarsz.discordsrv.DiscordSRV) HttpResponse(com.mashape.unirest.http.HttpResponse) Map(java.util.Map) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) ArrayList(java.util.ArrayList) Webhook(net.dv8tion.jda.core.entities.Webhook)

Example 62 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel in project FlareBot by FlareBot.

the class PaginationUtil method sendEmbedPagedMessage.

/**
 * Sends an embed paged message the to specified channel.
 * You can build with Embed with {@link PagedEmbedBuilder}.
 *
 * @param pagedEmbed The {@link stream.flarebot.flarebot.util.pagination.PagedEmbedBuilder.PagedEmbed} to use.
 * @param page       The page to start on (0 Indexed).
 * @param channel    The channel to send the paged message to.
 * @param sender     The user who requested the embed
 */
public static void sendEmbedPagedMessage(PagedEmbedBuilder.PagedEmbed pagedEmbed, int page, TextChannel channel, User sender, String group) {
    if (page < 0 || page > pagedEmbed.getPageTotal() - 1) {
        MessageUtils.sendErrorMessage("Invalid page: " + (page + 1) + " Total Pages: " + pagedEmbed.getPageTotal(), channel);
        return;
    }
    if (!pagedEmbed.isSinglePage()) {
        ButtonGroup buttonGroup = new ButtonGroup(sender.getIdLong(), group);
        Integer[] pages = new Integer[] { page };
        buttonGroup.addButton(new ButtonGroup.Button("\u23EE", (ownerID, user, message) -> {
            // Start
            pages[0] = 0;
            message.editMessage(pagedEmbed.getEmbed(pages[0])).queue();
        }));
        buttonGroup.addButton(new ButtonGroup.Button("\u23EA", (ownerID, user, message) -> {
            // Prev
            if (pages[0] != 0) {
                pages[0] -= 1;
                message.editMessage(pagedEmbed.getEmbed(pages[0])).queue();
            }
        }));
        buttonGroup.addButton(new ButtonGroup.Button("\u23E9", (ownerID, user, message) -> {
            // Next
            if (pages[0] + 1 != pagedEmbed.getPageTotal()) {
                pages[0] += 1;
                message.editMessage(pagedEmbed.getEmbed(pages[0])).queue();
            }
        }));
        buttonGroup.addButton(new ButtonGroup.Button("\u23ED", (ownerID, user, message) -> {
            // Last
            pages[0] = pagedEmbed.getPageTotal() - 1;
            message.editMessage(pagedEmbed.getEmbed(pages[0])).queue();
        }));
        buttonGroup.addButton(new ButtonGroup.Button("\u274C", (ownerID, user, message) -> {
            // Delete
            if (user.getIdLong() == ownerID || message.getGuild().getMember(user).hasPermission(Permission.MANAGE_PERMISSIONS)) {
                message.delete().queue(null, e -> {
                });
            } else {
                MessageUtils.sendErrorMessage("You need to be the sender or have the `Manage Messages` discord permission to do this!", (TextChannel) message.getChannel());
            }
        }));
        ButtonUtil.sendButtonedMessage(channel, pagedEmbed.getEmbed(page), buttonGroup);
    } else {
        channel.sendMessage(pagedEmbed.getEmbed(page)).queue();
    }
}
Also used : ButtonGroup(stream.flarebot.flarebot.util.objects.ButtonGroup) List(java.util.List) Permission(net.dv8tion.jda.core.Permission) User(net.dv8tion.jda.core.entities.User) TextChannel(net.dv8tion.jda.core.entities.TextChannel) ButtonUtil(stream.flarebot.flarebot.util.buttons.ButtonUtil) ArrayUtils(org.apache.commons.lang3.ArrayUtils) MessageUtils(stream.flarebot.flarebot.util.MessageUtils) ArrayList(java.util.ArrayList) TextChannel(net.dv8tion.jda.core.entities.TextChannel) ButtonGroup(stream.flarebot.flarebot.util.objects.ButtonGroup)

Example 63 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel in project FlareBot by FlareBot.

the class PaginationUtil method sendPagedMessage.

/**
 * Sends a paged message
 *
 * @param textChannel The channel to send it to
 * @param list        The {@link PaginationList} to use
 * @param page        The starting page
 * @param sender      The member who requested the button
 */
public static void sendPagedMessage(TextChannel textChannel, PaginationList list, int page, User sender, String group) {
    if (page < 0 || page > list.getPages() - 1) {
        MessageUtils.sendErrorMessage("Invalid page: " + (page + 1) + " Total Pages: " + list.getPages(), textChannel);
        return;
    }
    Integer[] pages = new Integer[] { page };
    if (list.getPages() > 1) {
        ButtonGroup buttonGroup = new ButtonGroup(sender.getIdLong(), group);
        buttonGroup.addButton(new ButtonGroup.Button("\u23EE", (ownerID, user, message) -> {
            // Start
            pages[0] = 0;
            message.editMessage(list.getPage(pages[0])).queue();
        }));
        buttonGroup.addButton(new ButtonGroup.Button("\u23EA", (ownerID, user, message) -> {
            // Prev
            if (pages[0] != 0) {
                pages[0] -= 1;
                message.editMessage(list.getPage(pages[0])).queue();
            }
        }));
        buttonGroup.addButton(new ButtonGroup.Button("\u23E9", (ownerID, user, message) -> {
            // Next
            if (pages[0] + 1 != list.getPages()) {
                pages[0] += 1;
                message.editMessage(list.getPage(pages[0])).queue();
            }
        }));
        buttonGroup.addButton(new ButtonGroup.Button("\u23ED", (ownerID, user, message) -> {
            // Last
            pages[0] = list.getPages() - 1;
            message.editMessage(list.getPage(pages[0])).queue();
        }));
        buttonGroup.addButton(new ButtonGroup.Button("\u274C", (ownerID, user, message) -> {
            // Delete
            if (user.getIdLong() == ownerID || message.getGuild().getMember(user).hasPermission(Permission.MANAGE_PERMISSIONS)) {
                message.delete().queue(null, e -> {
                });
            } else {
                MessageUtils.sendErrorMessage("You need to be the sender or have the `Manage Messages` discord permission to do this!", (TextChannel) message.getChannel());
            }
        }));
        ButtonUtil.sendButtonedMessage(textChannel, list.getPage(page), buttonGroup);
    } else {
        textChannel.sendMessage(list.getPage(page)).queue();
    }
}
Also used : ButtonGroup(stream.flarebot.flarebot.util.objects.ButtonGroup) List(java.util.List) Permission(net.dv8tion.jda.core.Permission) User(net.dv8tion.jda.core.entities.User) TextChannel(net.dv8tion.jda.core.entities.TextChannel) ButtonUtil(stream.flarebot.flarebot.util.buttons.ButtonUtil) ArrayUtils(org.apache.commons.lang3.ArrayUtils) MessageUtils(stream.flarebot.flarebot.util.MessageUtils) ArrayList(java.util.ArrayList) TextChannel(net.dv8tion.jda.core.entities.TextChannel) ButtonGroup(stream.flarebot.flarebot.util.objects.ButtonGroup)

Example 64 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel in project FlareBot by FlareBot.

the class ModlogHandler method postToModlog.

public void postToModlog(GuildWrapper wrapper, ModlogEvent event, User target, User responsible, String reason, MessageEmbed.Field... extraFields) {
    if (!wrapper.getModeration().isEventEnabled(wrapper, event))
        return;
    TextChannel tc = getModlogChannel(wrapper, event);
    // They either don't have a channel or set it to another guild.
    if (tc != null) {
        if (!tc.getGuild().getSelfMember().hasPermission(tc, Permission.MESSAGE_READ, Permission.MESSAGE_WRITE, Permission.MESSAGE_EMBED_LINKS)) {
            tc.getGuild().getOwner().getUser().openPrivateChannel().queue(pc -> {
                pc.sendMessage("Please give me permission to read message, write messages and embed links in the modlog channel: " + tc.getAsMention() + " or set the modlog channel to one I have access to!").queue();
            });
            return;
        }
        if (!wrapper.getModeration().isEventCompacted(event)) {
            EmbedBuilder eb = event.getEventEmbed(target, responsible, reason);
            if (extraFields != null && extraFields.length > 0) {
                for (MessageEmbed.Field field : extraFields) eb.addField(field);
            }
            tc.sendMessage(eb.build()).queue();
        } else {
            StringBuilder sb = new StringBuilder(event.getEventText(target, responsible, reason));
            if (extraFields != null && extraFields.length > 0) {
                sb.append("\n");
                for (MessageEmbed.Field field : extraFields) {
                    if (field == null)
                        continue;
                    sb.append("**").append(field.getName()).append("**: ").append(field.getValue()).append("\t");
                }
            }
            sb.append("\n** **");
            tc.sendMessage(sb.toString().trim()).queue();
        }
    }
}
Also used : TextChannel(net.dv8tion.jda.core.entities.TextChannel) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed)

Example 65 with TextChannel

use of net.dv8tion.jda.core.entities.TextChannel in project FlareBot by FlareBot.

the class ModlogHandler method handleAction.

/**
 * Handle a ModAction, this will do a bunch of checks and if they pass it will handle said action. For example if
 * you want to ban someone it will do checks like if you can ban that user, ig they're the owner, if you're trying
 * to ban yourself etc. After those pass it will then do the actual banning, post to the modlog and handle any tmp
 * stuff if needed.<br />
 * See also {@link #handleAction(GuildWrapper, TextChannel, User, User, ModAction, String)}
 *
 * @param wrapper   The GuildWrapper of the guild this is being done in.
 * @param channel   The channel this was executed, this is used for failire messages in the checks.
 * @param sender    The person who sent that said action, the user responsible.
 * @param target    The target user to have the action taken against.
 * @param modAction The ModAction to be performed.
 * @param reason    The reason this was done, if this is null it will default to "No Reason Given".
 * @param duration  The duration of said action, this only applies to temp actions, -1 should be passed otherwise.
 */
public void handleAction(GuildWrapper wrapper, TextChannel channel, User sender, User target, ModAction modAction, String reason, long duration) {
    String rsn = (reason == null ? "No reason given!" : reason.replaceAll("`", "'"));
    Member member = null;
    if (target != null) {
        member = wrapper.getGuild().getMember(target);
    }
    if (channel == null)
        return;
    if (member == null && modAction != ModAction.FORCE_BAN && modAction != ModAction.UNBAN) {
        MessageUtils.sendErrorMessage("That user isn't in this server!" + (modAction == ModAction.KICK ? " You can forceban with `{%}forceban <id>` to keep them from coming back." : ""), channel);
        return;
    }
    // Make sure the target user isn't the guild owner
    if (member != null && member.isOwner()) {
        MessageUtils.sendErrorMessage(String.format("Cannot %s **%s** because they're the guild owner!", modAction.getLowercaseName(), MessageUtils.getTag(target)), channel);
        return;
    }
    // Make sure the target user isn't themselves
    if (target != null && sender != null && target.getIdLong() == sender.getIdLong()) {
        MessageUtils.sendErrorMessage(String.format("You cannot %s yourself you daft person!", modAction.getLowercaseName()), channel);
        return;
    }
    if (target != null && target.getIdLong() == FlareBot.instance().getClient().getSelfUser().getIdLong()) {
        if (modAction == ModAction.UNBAN || modAction == ModAction.UNMUTE)
            MessageUtils.sendWarningMessage("W-why would you want to do that in the first place. Meanie :(", channel);
        else
            MessageUtils.sendWarningMessage(String.format("T-that's meannnnnnn :( I can't %s myself and I hope you don't want to either :(", modAction.getLowercaseName()), channel);
        return;
    }
    // Check if the person is below the target in role hierarchy
    if (member != null && sender != null && !canInteract(wrapper.getGuild().getMember(sender), member, wrapper)) {
        MessageUtils.sendErrorMessage(String.format("You cannot %s a user who is higher than you in the role hierarchy!", modAction.getLowercaseName()), channel);
        return;
    }
    // not just kick, ban etc.
    if (member != null && !wrapper.getGuild().getSelfMember().canInteract(member)) {
        MessageUtils.sendErrorMessage(String.format("Cannot " + modAction.getLowercaseName() + " %s! " + "Their highest role is higher than my highest role or they're the guild owner.", MessageUtils.getTag(target)), channel);
        return;
    }
    try {
        // BAN
        switch(modAction) {
            case BAN:
                channel.getGuild().getController().ban(target, 7, reason).queue(aVoid -> channel.sendMessage(new EmbedBuilder().setColor(Color.GREEN).setDescription("The ban hammer has been struck on " + target.getName() + " <:banhammer:368861419602575364>\nReason: " + rsn).setImage(channel.getGuild().getIdLong() == Constants.OFFICIAL_GUILD ? "https://flarebot.stream/img/banhammer.png" : null).build()).queue());
                break;
            case SOFTBAN:
                channel.getGuild().getController().ban(target, 7, reason).queue(aVoid -> {
                    channel.sendMessage(new EmbedBuilder().setColor(Color.GREEN).setDescription(target.getName() + " was softly hit with the ban hammer... " + "this time\nReason: " + rsn).build()).queue();
                    channel.getGuild().getController().unban(target).queue();
                });
                break;
            case FORCE_BAN:
                channel.getGuild().getController().ban(target.getId(), 7, reason).queue(aVoid -> channel.sendMessage(new EmbedBuilder().setColor(Color.GREEN).setDescription("The ban hammer has been forcefully struck on " + target.getName() + " <:banhammer:368861419602575364>\nReason: " + rsn).setImage(channel.getGuild().getIdLong() == Constants.OFFICIAL_GUILD ? "https://flarebot.stream/img/banhammer.png" : null).build()).queue());
                break;
            case TEMP_BAN:
                {
                    Period period = new Period(duration);
                    channel.getGuild().getController().ban(channel.getGuild().getMember(target), 7, reason).queue(aVoid -> {
                        channel.sendMessage(new EmbedBuilder().setDescription("The ban hammer has been struck on " + target.getName() + " for " + FormatUtils.formatJodaTime(period) + "\nReason: " + rsn).setImage(channel.getGuild().getIdLong() == Constants.OFFICIAL_GUILD ? "https://flarebot.stream/img/banhammer.png" : null).setColor(Color.WHITE).build()).queue();
                        Scheduler.queueFutureAction(channel.getGuild().getIdLong(), channel.getIdLong(), sender.getIdLong(), target.getIdLong(), reason, period, FutureAction.Action.TEMP_BAN);
                    });
                    break;
                }
            case UNBAN:
                if (target == null)
                    return;
                wrapper.getGuild().getController().unban(target).queue();
                MessageUtils.sendSuccessMessage("Unbanned " + target.getAsMention() + "!", channel, sender);
                // MUTE
                break;
            case MUTE:
                try {
                    wrapper.getModeration().muteUser(wrapper, wrapper.getGuild().getMember(target));
                } catch (HierarchyException e) {
                    MessageUtils.sendErrorMessage("Cannot apply the mute role, make sure it is below FlareBot in the " + "role hierarchy.", channel);
                    return;
                }
                MessageUtils.sendSuccessMessage("Muted " + target.getAsMention() + "\nReason: " + rsn, channel, sender);
                break;
            case TEMP_MUTE:
                {
                    try {
                        wrapper.getModeration().muteUser(wrapper, wrapper.getGuild().getMember(target));
                    } catch (HierarchyException e) {
                        MessageUtils.sendErrorMessage("Cannot apply the mute role, make sure it is below FlareBot in the " + "role hierarchy.", channel);
                        return;
                    }
                    Period period = new Period(duration);
                    Scheduler.queueFutureAction(channel.getGuild().getIdLong(), channel.getIdLong(), sender.getIdLong(), target.getIdLong(), reason, period, FutureAction.Action.TEMP_MUTE);
                    MessageUtils.sendSuccessMessage("Temporarily Muted " + target.getAsMention() + " for " + FormatUtils.formatJodaTime(period) + "\nReason: " + rsn, channel, sender);
                    break;
                }
            case UNMUTE:
                if (wrapper.getMutedRole() != null && wrapper.getGuild().getMember(target).getRoles().contains(wrapper.getMutedRole())) {
                    wrapper.getModeration().unmuteUser(wrapper, member);
                    MessageUtils.sendSuccessMessage("Unmuted " + target.getAsMention(), channel, sender);
                } else {
                    MessageUtils.sendErrorMessage("That user isn't muted!!", channel);
                }
                // KICK and WARN
                break;
            case KICK:
                channel.getGuild().getController().kick(member, reason).queue(aVoid -> MessageUtils.sendSuccessMessage(target.getName() + " has been kicked from the server!\nReason: " + rsn, channel, sender));
                break;
            case WARN:
                wrapper.addWarning(target, (reason != null ? reason : "No reason provided - action done by " + sender.getName()));
                EmbedBuilder eb = new EmbedBuilder();
                eb.appendDescription("\u26A0 Warned " + MessageUtils.getTag(target) + "\nReason: " + rsn).setColor(Color.WHITE);
                channel.sendMessage(eb.build()).queue();
                break;
            default:
                throw new IllegalArgumentException("An illegal ModAction was attempted to be handled - " + modAction.toString());
        }
    } catch (PermissionException e) {
        MessageUtils.sendErrorMessage(String.format("Cannot " + modAction.getLowercaseName() + " %s! " + "I do not have the `" + e.getPermission().getName() + "` permission!", MessageUtils.getTag(target)), channel);
        return;
    }
    // TODO: Infraction
    postToModlog(wrapper, modAction.getEvent(), target, sender, rsn);
}
Also used : Role(net.dv8tion.jda.core.entities.Role) Color(java.awt.Color) Period(org.joda.time.Period) Member(net.dv8tion.jda.core.entities.Member) TextChannel(net.dv8tion.jda.core.entities.TextChannel) FlareBot(stream.flarebot.flarebot.FlareBot) HierarchyException(net.dv8tion.jda.core.exceptions.HierarchyException) GuildWrapper(stream.flarebot.flarebot.objects.GuildWrapper) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) FormatUtils(stream.flarebot.flarebot.util.general.FormatUtils) Permission(net.dv8tion.jda.core.Permission) User(net.dv8tion.jda.core.entities.User) Scheduler(stream.flarebot.flarebot.scheduler.Scheduler) FutureAction(stream.flarebot.flarebot.scheduler.FutureAction) Constants(stream.flarebot.flarebot.util.Constants) MessageUtils(stream.flarebot.flarebot.util.MessageUtils) MessageEmbed(net.dv8tion.jda.core.entities.MessageEmbed) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) HierarchyException(net.dv8tion.jda.core.exceptions.HierarchyException) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Period(org.joda.time.Period) Member(net.dv8tion.jda.core.entities.Member)

Aggregations

TextChannel (net.dv8tion.jda.core.entities.TextChannel)90 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)27 Guild (net.dv8tion.jda.core.entities.Guild)21 User (net.dv8tion.jda.core.entities.User)19 Member (net.dv8tion.jda.core.entities.Member)18 List (java.util.List)17 Message (net.dv8tion.jda.core.entities.Message)17 ArrayList (java.util.ArrayList)14 VoiceChannel (net.dv8tion.jda.core.entities.VoiceChannel)13 GuildWrapper (stream.flarebot.flarebot.objects.GuildWrapper)13 MessageUtils (stream.flarebot.flarebot.util.MessageUtils)13 Collectors (java.util.stream.Collectors)10 CommandType (stream.flarebot.flarebot.commands.CommandType)8 Role (net.dv8tion.jda.core.entities.Role)7 FlareBot (stream.flarebot.flarebot.FlareBot)7 Track (com.arsenarsen.lavaplayerbridge.player.Track)6 MessageEmbed (net.dv8tion.jda.core.entities.MessageEmbed)6 MantaroData (net.kodehawa.mantarobot.data.MantaroData)6 DBGuild (net.kodehawa.mantarobot.db.entities.DBGuild)6 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)6