/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package moe.nekojimi.chords.commands; import java.util.List; import moe.nekojimi.chords.Chords; import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; public class JoinCommand extends Command { public JoinCommand(Chords bot) { super(bot, "join"); } @Override public void call(Invocation invocation) { MessageReceivedEvent event = invocation.getEvent(); List args = invocation.getArgs(); MessageChannel textChannel = event.getChannel(); VoiceChannel channel = null; if (args.isEmpty() || args.get(0).isBlank()) { final Member member = event.getMessage().getMember(); if (member != null) { GuildVoiceState voiceState = member.getVoiceState(); if (voiceState != null && voiceState.inAudioChannel()) channel = voiceState.getChannel().asVoiceChannel(); } if (channel == null) { Guild guild = event.getMessage().getGuild(); List voiceChannels = guild.getVoiceChannels(); System.out.println("Finding channel to join..."); for (VoiceChannel voiceChannel : voiceChannels) { List members = voiceChannel.getMembers(); if (!members.isEmpty()) channel = voiceChannel; System.out.println("\t" + voiceChannel.getName() + " " + members.size()); } } if (channel == null) throw new RuntimeException("Cannot find any voice channels with people in!"); } else { String arg0 = args.get(0); Guild guild = event.getGuild(); boolean isNumber = arg0.matches("\\d+"); // This is a regular expression that ensures the input consists of digits if (isNumber) // The input is an id? channel = guild.getVoiceChannelById(arg0); if (channel == null) // Then the input must be a name? { List channels = guild.getVoiceChannelsByName(arg0, true); if (!channels.isEmpty()) // Make sure we found at least one exact match channel = channels.get(0); // We found a channel! This cannot be null. } if (channel == null) // I have no idea what you want mr user { onUnknownChannel(textChannel, arg0); // Let the user know about our failure return; } } bot.connectTo(channel); // We found a channel to connect to! onConnecting(channel, textChannel); // Let the user know, we were successful! } /** * Inform user about successful connection. * * @param channel * The voice channel we connected to * @param textChannel * The text channel to send the message in */ public void onConnecting(VoiceChannel channel, MessageChannel textChannel) { textChannel.sendMessage("Connecting to " + channel.getName()).queue(); // never forget to queue()! } /** * The channel to connect to is not known to us. * * @param channel * The message channel (text channel abstraction) to send failure * information to * @param comment * The information of this channel */ public void onUnknownChannel(MessageChannel channel, String comment) { channel.sendMessage("Unable to connect to ``" + comment + "``, no such channel!").queue(); // never forget to queue()! } }