Search in sources :

Example 41 with GuildVoiceState

use of net.dv8tion.jda.api.entities.GuildVoiceState in project Tux by Bakterio.

the class JoinCommand method invoke.

@Override
public void invoke(GuildMessageReceivedEvent e, String[] args) {
    final GuildVoiceState voiceState = e.getGuild().getSelfMember().getVoiceState();
    final GuildVoiceState memberVoiceState = e.getMember().getVoiceState();
    if (voiceState.inVoiceChannel()) {
        e.getChannel().sendMessage("I am already in " + voiceState.getChannel().getName() + " channel.").queue();
        return;
    }
    if (!memberVoiceState.inVoiceChannel()) {
        e.getChannel().sendMessage("You have to be in voice channel to invoke this command...").queue();
        return;
    }
    final AudioManager audioManager = e.getGuild().getAudioManager();
    audioManager.openAudioConnection(memberVoiceState.getChannel());
    e.getChannel().sendMessage("I am in!!!").queue();
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager) GuildVoiceState(net.dv8tion.jda.api.entities.GuildVoiceState)

Example 42 with GuildVoiceState

use of net.dv8tion.jda.api.entities.GuildVoiceState in project Tux by Bakterio.

the class LeaveCommand method invoke.

@Override
public void invoke(GuildMessageReceivedEvent e, String[] args) {
    final GuildVoiceState voiceState = e.getGuild().getSelfMember().getVoiceState();
    if (!voiceState.inVoiceChannel()) {
        e.getChannel().sendMessage("I can't leave, I am not in any voice channel!!!").queue();
        return;
    }
    final AudioManager audioManager = e.getGuild().getAudioManager();
    // TODO why tf is this not working?
    audioManager.closeAudioConnection();
    e.getChannel().sendMessage("See you around... :wink:").queue();
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager) GuildVoiceState(net.dv8tion.jda.api.entities.GuildVoiceState)

Example 43 with GuildVoiceState

use of net.dv8tion.jda.api.entities.GuildVoiceState in project flowermoon by wolf-yuan-6115.

the class MusicCommand method execute.

@Override
protected void execute(CommandEvent event) {
    Settings settings = event.getClient().getSettingsFor(event.getGuild());
    TextChannel tchannel = settings.getTextChannel(event.getGuild());
    if (tchannel != null && !event.getTextChannel().equals(tchannel)) {
        try {
            event.getMessage().delete().queue();
        } catch (PermissionException ignore) {
        }
        event.replyInDm(event.getClient().getError() + " 你只能在 " + tchannel.getAsMention() + " 中使用這個指令!");
        return;
    }
    // no point constantly checking for this later
    bot.getPlayerManager().setUpHandler(event.getGuild());
    if (bePlaying && !((AudioHandler) event.getGuild().getAudioManager().getSendingHandler()).isMusicPlaying(event.getJDA())) {
        event.reply(event.getClient().getError() + " 必須要有一個音樂正在播放!");
        return;
    }
    if (beListening) {
        VoiceChannel current = event.getGuild().getSelfMember().getVoiceState().getChannel();
        if (current == null)
            current = settings.getVoiceChannel(event.getGuild());
        GuildVoiceState userState = event.getMember().getVoiceState();
        if (!userState.inVoiceChannel() || userState.isDeafened() || (current != null && !userState.getChannel().equals(current))) {
            event.replyError("你必須在 " + (current == null ? "一個語音頻道" : current.getAsMention()) + " 才能使用這個指令!");
            return;
        }
        VoiceChannel afkChannel = userState.getGuild().getAfkChannel();
        if (afkChannel != null && afkChannel.equals(userState.getChannel())) {
            event.replyError("你不能在閒置頻道中使用這個指令!");
            return;
        }
        if (!event.getGuild().getSelfMember().getVoiceState().inVoiceChannel()) {
            try {
                event.getGuild().getAudioManager().openAudioConnection(userState.getChannel());
            } catch (PermissionException ex) {
                event.reply(event.getClient().getError() + " 我無法連結至 " + userState.getChannel().getAsMention() + " 語音頻道!");
                return;
            }
        }
    }
    doCommand(event);
}
Also used : PermissionException(net.dv8tion.jda.api.exceptions.PermissionException) TextChannel(net.dv8tion.jda.api.entities.TextChannel) GuildVoiceState(net.dv8tion.jda.api.entities.GuildVoiceState) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel) AudioHandler(com.jagrosh.jmusicbot.audio.AudioHandler) Settings(com.jagrosh.jmusicbot.settings.Settings)

Example 44 with GuildVoiceState

use of net.dv8tion.jda.api.entities.GuildVoiceState in project flowermoon by wolf-yuan-6115.

the class Command method run.

/**
 * Runs checks for the {@link com.jagrosh.jdautilities.command.Command Command} with the
 * given {@link com.jagrosh.jdautilities.command.CommandEvent CommandEvent} that called it.
 * <br>Will terminate, and possibly respond with a failure message, if any checks fail.
 *
 * @param  event
 *         The CommandEvent that triggered this Command
 */
public final void run(CommandEvent event) {
    // child check
    if (!event.getArgs().isEmpty()) {
        String[] parts = Arrays.copyOf(event.getArgs().split("\\s+", 2), 2);
        if (helpBiConsumer != null && parts[0].equalsIgnoreCase(event.getClient().getHelpWord())) {
            helpBiConsumer.accept(event, this);
            return;
        }
        for (Command cmd : children) {
            if (cmd.isCommandFor(parts[0])) {
                event.setArgs(parts[1] == null ? "" : parts[1]);
                cmd.run(event);
                return;
            }
        }
    }
    // owner check
    if (ownerCommand && !(event.isOwner())) {
        terminate(event, null);
        return;
    }
    // category check
    if (category != null && !category.test(event)) {
        terminate(event, category.getFailureResponse());
        return;
    }
    // is allowed check
    if (event.isFromType(ChannelType.TEXT) && !isAllowed(event.getTextChannel())) {
        terminate(event, "這個指令不能在這個頻道中使用!");
        return;
    }
    // required role check
    if (requiredRole != null)
        if (!event.isFromType(ChannelType.TEXT) || event.getMember().getRoles().stream().noneMatch(r -> r.getName().equalsIgnoreCase(requiredRole))) {
            terminate(event, event.getClient().getError() + " 你必須要有名為 `" + requiredRole + "` 的身分組才可以使用這個指令!");
            return;
        }
    // availability check
    if (event.getChannelType() == ChannelType.TEXT) {
        // bot perms
        for (Permission p : botPermissions) {
            if (p.isChannel()) {
                if (p.name().startsWith("VOICE")) {
                    GuildVoiceState gvc = event.getMember().getVoiceState();
                    VoiceChannel vc = gvc == null ? null : gvc.getChannel();
                    if (vc == null) {
                        terminate(event, event.getClient().getError() + " 你必須要在一個語音頻道裡才能使用這個指令!");
                        return;
                    } else if (!event.getSelfMember().hasPermission(vc, p)) {
                        terminate(event, String.format(BOT_PERM, event.getClient().getError(), "語音頻道", p.getName()));
                        return;
                    }
                } else {
                    if (!event.getSelfMember().hasPermission(event.getTextChannel(), p)) {
                        terminate(event, String.format(BOT_PERM, event.getClient().getError(), "頻道", p.getName()));
                        return;
                    }
                }
            } else {
                if (!event.getSelfMember().hasPermission(p)) {
                    terminate(event, String.format(BOT_PERM, event.getClient().getError(), "伺服器", p.getName()));
                    return;
                }
            }
        }
        // user perms
        for (Permission p : userPermissions) {
            if (p.isChannel()) {
                if (!event.getMember().hasPermission(event.getTextChannel(), p)) {
                    terminate(event, String.format(USER_PERM, event.getClient().getError(), "頻道", p.getName()));
                    return;
                }
            } else {
                if (!event.getMember().hasPermission(p)) {
                    terminate(event, String.format(USER_PERM, event.getClient().getError(), "伺服器", p.getName()));
                    return;
                }
            }
        }
    } else if (guildOnly) {
        terminate(event, event.getClient().getError() + " 這個指令不能再私訊中使用!");
        return;
    }
    // cooldown check
    if (cooldown > 0) {
        String key = getCooldownKey(event);
        int remaining = event.getClient().getRemainingCooldown(key);
        if (remaining > 0) {
            terminate(event, getCooldownError(event, remaining));
            return;
        } else
            event.getClient().applyCooldown(key, cooldown);
    }
    // run
    try {
        execute(event);
    } catch (Throwable t) {
        if (event.getClient().getListener() != null) {
            event.getClient().getListener().onCommandException(event, this, t);
            return;
        }
        // otherwise we rethrow
        throw t;
    }
    if (event.getClient().getListener() != null)
        event.getClient().getListener().onCompletedCommand(event, this);
}
Also used : Permission(net.dv8tion.jda.api.Permission) GuildVoiceState(net.dv8tion.jda.api.entities.GuildVoiceState) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel)

Example 45 with GuildVoiceState

use of net.dv8tion.jda.api.entities.GuildVoiceState in project ascent_bot by FeroniK.

the class Join method handle.

@Override
public void handle(CommandContext ctx) {
    final TextChannel channel = ctx.getChannel();
    final Member member = ctx.getMember();
    final GuildVoiceState memberVoiceState = member.getVoiceState();
    AudioManager audioManager = ctx.getGuild().getAudioManager();
    audioManager.setSelfDeafened(true);
    final VoiceChannel memberChannel = memberVoiceState.getChannel();
    // Embeds
    String title = "Connected";
    String description = "Connected successfully to " + memberChannel.getAsMention();
    String footer = "Requested by " + member.getEffectiveName();
    String footerUrl = member.getEffectiveAvatarUrl();
    EmbedBuilder builder = Embeds.createBuilder(title, description, footer, footerUrl, null);
    audioManager.openAudioConnection(memberChannel);
    channel.sendMessageEmbeds(builder.build()).queue();
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager) TextChannel(net.dv8tion.jda.api.entities.TextChannel) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) GuildVoiceState(net.dv8tion.jda.api.entities.GuildVoiceState) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel) Member(net.dv8tion.jda.api.entities.Member)

Aggregations

GuildVoiceState (net.dv8tion.jda.api.entities.GuildVoiceState)51 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)16 Member (net.dv8tion.jda.api.entities.Member)16 LogUtils (main.utils.json.logs.LogUtils)13 AudioManager (net.dv8tion.jda.api.managers.AudioManager)12 Guild (net.dv8tion.jda.api.entities.Guild)11 TextChannel (net.dv8tion.jda.api.entities.TextChannel)9 VoiceChannel (net.dv8tion.jda.api.entities.VoiceChannel)9 ArrayList (java.util.ArrayList)7 Permission (net.dv8tion.jda.api.Permission)5 CommandFlag (at.xirado.bean.command.CommandFlag)4 SlashCommand (at.xirado.bean.command.SlashCommand)4 SlashCommandContext (at.xirado.bean.command.SlashCommandContext)4 EmbedUtil (at.xirado.bean.misc.EmbedUtil)4 Message (net.dv8tion.jda.api.entities.Message)4 Bean (at.xirado.bean.Bean)3 GenericCommand (at.xirado.bean.command.GenericCommand)3 MessageContextCommand (at.xirado.bean.command.context.MessageContextCommand)3 UserContextCommand (at.xirado.bean.command.context.UserContextCommand)3 MockContextMenuCommand (at.xirado.bean.command.context.message.MockContextMenuCommand)3