Search in sources :

Example 1 with PermissionOverride

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

the class LockChatCommand method onCommand.

@Override
public void onCommand(User sender, GuildWrapper guild, TextChannel channel, Message message, String[] args, Member member) {
    if (!guild.getGuild().getSelfMember().hasPermission(Permission.MANAGE_ROLES)) {
        MessageUtils.sendErrorMessage("I can't lock the chat due to lack of permissions! " + "I need the `Manage Roles` permission", channel);
        return;
    }
    String reason = null;
    long time = -1;
    @Nonnull AtomicReference<TextChannel> tc = new AtomicReference<>(channel);
    if (args.length >= 1) {
        TextChannel tmp = ParseUtils.parseChannel(guild.getGuild(), args[0], false);
        if (tmp != null)
            tc.set(tmp);
        if (tmp == null || args.length >= 2) {
            Long l = GeneralUtils.parseTime(tmp == null ? args[0] : args[1]);
            if (l == null) {
                MessageUtils.sendErrorMessage("Invalid time format! Please use something like `1h10m`", channel);
                return;
            }
            time = l;
        }
        if (tmp != null && time > 0)
            reason = MessageUtils.getMessage(args, 2);
        else if ((tmp == null && time > 0) || (tmp != null && time == -1))
            reason = MessageUtils.getMessage(args, 1);
        else
            reason = MessageUtils.getMessage(args, 0);
        if (reason.isEmpty())
            reason = null;
    }
    PermissionOverride everyoneOvr = tc.get().getPermissionOverride(guild.getGuild().getPublicRole());
    boolean locking = !everyoneOvr.getDenied().contains(Permission.MESSAGE_WRITE);
    EnumSet<Permission> perm = EnumSet.of(Permission.MESSAGE_WRITE);
    EnumSet<Permission> empty = EnumSet.noneOf(Permission.class);
    tc.get().getPermissionOverride(guild.getGuild().getPublicRole()).getManager().deny(locking ? perm : empty).clear(locking ? empty : perm).reason(reason + "\nDone by: " + sender.getIdLong()).queue();
    tc.get().putPermissionOverride(guild.getGuild().getSelfMember()).setPermissions(locking ? perm : empty, empty).reason(reason + "\nDone by: " + sender.getIdLong()).queue();
    if (tc.get().getIdLong() != channel.getIdLong())
        channel.sendMessage(new EmbedBuilder().setColor(locking ? ColorUtils.RED : ColorUtils.GREEN).setDescription(tc.get().getAsMention() + " has been " + (locking ? "locked" : "unlocked") + "!").build()).queue();
    if (guild.getGuild().getSelfMember().hasPermission(tc.get(), Permission.MESSAGE_WRITE))
        channel.sendMessage(new EmbedBuilder().setColor(locking ? ColorUtils.RED : ColorUtils.GREEN).setDescription("The chat has been " + (locking ? "locked" : "unlocked") + " by a staff member" + (locking && time > 0 ? " for " + FormatUtils.formatTime(time, TimeUnit.MILLISECONDS, true, false) : "") + "!" + (reason != null ? "\nReason: " + reason : "")).build()).queue();
    if (locking && time > 0) {
        new FlareBotTask("ChannelUnlock-" + tc.get().getIdLong()) {

            @Override
            public void run() {
                tc.get().getPermissionOverride(guild.getGuild().getPublicRole()).getManager().clear(Permission.MESSAGE_WRITE).queue();
                if (guild.getGuild().getSelfMember().hasPermission(tc.get(), Permission.MESSAGE_WRITE))
                    channel.sendMessage(new EmbedBuilder().setColor(ColorUtils.GREEN).setDescription("The chat has been unlocked").build()).queue();
            }
        }.delay(time);
    }
}
Also used : PermissionOverride(net.dv8tion.jda.core.entities.PermissionOverride) Nonnull(javax.annotation.Nonnull) AtomicReference(java.util.concurrent.atomic.AtomicReference) TextChannel(net.dv8tion.jda.core.entities.TextChannel) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Permission(net.dv8tion.jda.core.Permission) FlareBotTask(stream.flarebot.flarebot.scheduler.FlareBotTask) PermissionOverride(net.dv8tion.jda.core.entities.PermissionOverride) PermissionOverride(net.dv8tion.jda.core.entities.PermissionOverride)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Nonnull (javax.annotation.Nonnull)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1 Permission (net.dv8tion.jda.core.Permission)1 PermissionOverride (net.dv8tion.jda.core.entities.PermissionOverride)1 TextChannel (net.dv8tion.jda.core.entities.TextChannel)1 FlareBotTask (stream.flarebot.flarebot.scheduler.FlareBotTask)1