Search in sources :

Example 31 with VoiceChannel

use of net.dv8tion.jda.core.entities.VoiceChannel in project FredBoat by Frederikam.

the class VoiceChannelCleanupAgent method cleanup.

private void cleanup() {
    log.info("Checking guilds for stale voice connections.");
    final AtomicInteger totalGuilds = new AtomicInteger(0);
    final AtomicInteger totalVcs = new AtomicInteger(0);
    final AtomicInteger closedVcs = new AtomicInteger(0);
    guildProvider.streamGuilds().forEach(guild -> {
        try {
            totalGuilds.incrementAndGet();
            if (guild != null && guild.getSelfMember() != null && guild.getSelfMember().getVoiceState() != null && guild.getSelfMember().getVoiceState().getChannel() != null) {
                totalVcs.incrementAndGet();
                VoiceChannel vc = guild.getSelfMember().getVoiceState().getChannel();
                if (getHumanMembersInVC(vc).size() == 0) {
                    closedVcs.incrementAndGet();
                    VoteSkipCommand.guildSkipVotes.remove(guild.getIdLong());
                    audioConnectionFacade.closeConnection(guild);
                    VC_LAST_USED.remove(vc.getId());
                } else if (isBeingUsed(vc)) {
                    VC_LAST_USED.put(vc.getId(), System.currentTimeMillis());
                } else {
                    if (!VC_LAST_USED.containsKey(vc.getId())) {
                        VC_LAST_USED.put(vc.getId(), System.currentTimeMillis());
                    }
                    long lastUsed = VC_LAST_USED.get(vc.getId());
                    if (System.currentTimeMillis() - lastUsed > UNUSED_CLEANUP_THRESHOLD) {
                        closedVcs.incrementAndGet();
                        audioConnectionFacade.closeConnection(guild);
                        VC_LAST_USED.remove(vc.getId());
                    }
                }
            }
        } catch (Exception e) {
            log.error("Failed to check guild {} for stale voice connections", guild.getIdLong(), e);
        }
    });
    log.info("Checked {} guilds for stale voice connections.", totalGuilds.get());
    log.info("Closed {} of {} voice connections.", closedVcs.get(), totalVcs.get());
    Metrics.voiceChannelsCleanedUp.inc(closedVcs.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VoiceChannel(net.dv8tion.jda.core.entities.VoiceChannel)

Example 32 with VoiceChannel

use of net.dv8tion.jda.core.entities.VoiceChannel in project Rubicon by Rubicon-Bot.

the class CommandAutochannel method invokeDelete.

private void invokeDelete(CommandManager.ParsedCommandInvocation parsedCommandInvocation) {
    String[] args = parsedCommandInvocation.getArgs();
    Guild guild = parsedCommandInvocation.getMessage().getGuild();
    StringBuilder names = new StringBuilder();
    for (int i = 1; i < args.length; i++) {
        names.append(args[i]).append(" ");
    }
    String name = names.toString();
    name = names.replace(name.lastIndexOf(" "), name.lastIndexOf(" ") + 1, "").toString();
    List<VoiceChannel> channels = guild.getVoiceChannelsByName(name, false);
    String oldEntry = RubiconBot.getMySQL().getGuildValue(guild, "autochannels");
    List<VoiceChannel> autochannels = new ArrayList<>();
    channels.forEach(c -> {
        if (oldEntry.contains(c.getId()))
            autochannels.add(c);
    });
    if (autochannels.isEmpty()) {
        Message mymsg = parsedCommandInvocation.getTextChannel().sendMessage(EmbedUtil.error("Unknown Channel", "There is now channel with this name").build()).complete();
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                mymsg.delete().queue();
            }
        }, 5000);
        return;
    }
    if (autochannels.size() > 1) {
        ChannelSearch search = genChannelSearch(autochannels, true, parsedCommandInvocation);
        searches.put(guild, search);
    } else {
        VoiceChannel channel = channels.get(0);
        if (!oldEntry.contains(channel.getId())) {
            parsedCommandInvocation.getMessage().getTextChannel().sendMessage(EmbedUtil.error("Unknown channel", "This channel isn't an autochannel").build()).queue();
            return;
        }
        deleteChannel(channel, parsedCommandInvocation);
    }
}
Also used : Message(net.dv8tion.jda.core.entities.Message) Guild(net.dv8tion.jda.core.entities.Guild) VoiceChannel(net.dv8tion.jda.core.entities.VoiceChannel)

Example 33 with VoiceChannel

use of net.dv8tion.jda.core.entities.VoiceChannel in project Rubicon by Rubicon-Bot.

the class CommandAutochannel method genChannelSearch.

private ChannelSearch genChannelSearch(List<VoiceChannel> channels, boolean deleteChannel, CommandManager.ParsedCommandInvocation parsedCommandInvocation) {
    HashMap<String, VoiceChannel> channellist = new HashMap<>();
    StringBuilder channelnames = new StringBuilder();
    ArrayList<String> EMOJIS = new ArrayList<>(Arrays.asList(EMOTI));
    channels.forEach(c -> {
        String category;
        if (c.getParent() != null)
            category = c.getParent().getName();
        else
            category = "NONE";
        channelnames.append(EMOJIS.get(0)).append(" - ").append(c.getName()).append("(`").append(c.getId()).append("`) (Category: ").append(category).append(")\n");
        channellist.put(EMOJIS.get(0), c);
        EMOJIS.remove(0);
    });
    if (channellist.isEmpty()) {
        parsedCommandInvocation.getMessage().getTextChannel().sendMessage(EmbedUtil.error("Unknown Channel", "There is now channel with this name").build()).queue();
        return null;
    } else {
        Message msg = parsedCommandInvocation.getMessage().getTextChannel().sendMessage(new EmbedBuilder().setColor(Colors.COLOR_SECONDARY).setDescription(channelnames.toString()).build()).complete();
        channellist.keySet().forEach(e -> msg.addReaction(e).queue());
        return new ChannelSearch(msg, channellist, deleteChannel);
    }
}
Also used : EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) Message(net.dv8tion.jda.core.entities.Message) VoiceChannel(net.dv8tion.jda.core.entities.VoiceChannel)

Example 34 with VoiceChannel

use of net.dv8tion.jda.core.entities.VoiceChannel in project Rubicon by Rubicon-Bot.

the class AutochannelListener method onGuildVoiceMove.

@Override
public void onGuildVoiceMove(GuildVoiceMoveEvent e) {
    VoiceChannel ch = e.getChannelJoined();
    if (isAutoChannel(e.getGuild(), ch)) {
        VoiceChannel newChannel = (VoiceChannel) e.getGuild().getController().createCopyOfChannel(ch).setName(ch.getName() + " [AC]").complete();
        e.getGuild().getController().moveVoiceMember(e.getMember(), newChannel).queue();
    }
    if (e.getChannelLeft().getMembers().size() == 0) {
        if (e.getChannelLeft().getName().contains("[AC]")) {
            e.getChannelLeft().delete().queue();
        }
    }
}
Also used : VoiceChannel(net.dv8tion.jda.core.entities.VoiceChannel)

Example 35 with VoiceChannel

use of net.dv8tion.jda.core.entities.VoiceChannel in project TheLighterBot by PhotonBursted.

the class PermanentChannelCommand method execute.

@Override
protected void execute() {
    if (!ev.getMember().getVoiceState().inVoiceChannel()) {
        handleError(MessageContent.NOT_IN_VOICE_CHANNEL);
        return;
    }
    VoiceChannel vc = ev.getMember().getVoiceState().getChannel();
    if (l.getChannelController().isPermanent(vc)) {
        handleError(MessageContent.CHANNEL_ALREADY_PERMANENT);
        return;
    }
    l.getChannelController().getPermChannels().putStoring(ev.getChannel(), vc);
    LoggerUtils.logAndDelete(log, String.format("%s has been made permanent.", vc.getName()));
    l.getDiscordController().sendMessage(ev.getChannel(), String.format("Successfully made **%s** permanent!", vc.getName()), DiscordController.AUTOMATIC_REMOVAL_INTERVAL);
    l.getFileController().applyPermAddition(ev.getChannel(), vc);
}
Also used : VoiceChannel(net.dv8tion.jda.core.entities.VoiceChannel)

Aggregations

VoiceChannel (net.dv8tion.jda.core.entities.VoiceChannel)38 TextChannel (net.dv8tion.jda.core.entities.TextChannel)13 Guild (net.dv8tion.jda.core.entities.Guild)8 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)7 Message (net.dv8tion.jda.core.entities.Message)7 AudioManager (net.dv8tion.jda.core.managers.AudioManager)6 GuildPlayer (fredboat.audio.player.GuildPlayer)5 ArrayList (java.util.ArrayList)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Collectors (java.util.stream.Collectors)3 Member (net.dv8tion.jda.core.entities.Member)3 Player (com.arsenarsen.lavaplayerbridge.player.Player)2 Track (com.arsenarsen.lavaplayerbridge.player.Track)2 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)2 AudioTrackContext (fredboat.audio.queue.AudioTrackContext)2 SplitAudioTrackContext (fredboat.audio.queue.SplitAudioTrackContext)2 Consumer (java.util.function.Consumer)2 Nullable (javax.annotation.Nullable)2 User (net.dv8tion.jda.core.entities.User)2 JDAImpl (net.dv8tion.jda.core.entities.impl.JDAImpl)2