sedmelluq / lavaplayer

Audio player library for Discord
Apache License 2.0
973 stars 247 forks source link

Discord-Bot with Lavaplayer - Volume #560

Closed officialLucas closed 3 years ago

officialLucas commented 4 years ago

Hey I am creating a Discord music bot with lavaplayer. I'm trying to do a volume command, but whenever I do it, the bot stops playing music. Can someone help?

String botChannelID;
    String userChannelID;
    private AudioPlayerManager playerManager;
    private Map<Long, GuildMusicManager> musicManagers;

    @Override
    public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
        Member member = event.getMember();

        if(event.getMessage().getContentRaw().startsWith(DiscordBot.PREFIX + "volume") || event.getMessage().getContentRaw().startsWith(DiscordBot.PREFIX + "vol")) {
            if(event.getGuild().getAudioManager().isConnected()) {
                if(member.getVoiceState().inVoiceChannel()) {

                    botChannelID = Objects.requireNonNull(event.getGuild().getAudioManager().getConnectedChannel()).getId();
                    userChannelID = Objects.requireNonNull(member.getVoiceState().getChannel()).getId();

                    if(botChannelID.equals(userChannelID)) {

                        String[] args = event.getMessage().getContentRaw().split(" ");
                        if(args.length == 2) {

                            if(isNumber(args[1])) {
                                int size = Integer.parseInt(args[1]);

                                if(size > 0 && size < 101) {

                                    getGuildAudioPlayer(event.getGuild()).player.setVolume(size);
                                    EmbedBuilder embedBuilder = new EmbedBuilder();
                                    embedBuilder.setColor(Color.GREEN);
                                    embedBuilder.setDescription("Die Lautstärke wurde auf " + size + " geändert!");
                                    event.getChannel().sendMessage(embedBuilder.build()).queue();

                                } else {

                                    EmbedBuilder embedBuilder = new EmbedBuilder();
                                    embedBuilder.setColor(Color.RED);
                                    embedBuilder.setDescription("`-volume <1-100>`");
                                    event.getChannel().sendMessage(embedBuilder.build()).queue();

                                }

                            } else {

                                EmbedBuilder embedBuilder = new EmbedBuilder();
                                embedBuilder.setColor(Color.RED);
                                embedBuilder.setDescription("`-volume <1-100>`");
                                event.getChannel().sendMessage(embedBuilder.build()).queue();

                            }

                        } else {

                            EmbedBuilder embedBuilder = new EmbedBuilder();
                            embedBuilder.setColor(Color.RED);
                            embedBuilder.setDescription("`-volume <1-100>`");
                            event.getChannel().sendMessage(embedBuilder.build()).queue();

                        }

                    } else {

                        EmbedBuilder embedBuilder = new EmbedBuilder();
                        embedBuilder.setColor(Color.RED);
                        embedBuilder.setDescription("Du befindest dich nicht im selben Voice-Channel wie der Bot!");
                        event.getChannel().sendMessage(embedBuilder.build()).queue();

                    }

                } else {

                    EmbedBuilder embedBuilder = new EmbedBuilder();
                    embedBuilder.setColor(Color.RED);
                    embedBuilder.setDescription("Du befindest dich nicht in einem Voice-Channel!");
                    event.getChannel().sendMessage(embedBuilder.build()).queue();

                }

            } else {

                EmbedBuilder embedBuilder = new EmbedBuilder();
                embedBuilder.setColor(Color.RED);
                embedBuilder.setDescription("Der Bot befindet sich nicht in einem Voice-Channel!");
                event.getChannel().sendMessage(embedBuilder.build()).queue();

            }

        }

    }

    public VolumeCommand() {
        this.musicManagers = new HashMap<>();

        this.playerManager = new DefaultAudioPlayerManager();
        AudioSourceManagers.registerRemoteSources(playerManager);
        AudioSourceManagers.registerLocalSource(playerManager);
    }

    public synchronized GuildMusicManager getGuildAudioPlayer(Guild guild) {
        long guildId = Long.parseLong(guild.getId());
        GuildMusicManager musicManager = musicManagers.get(guildId);

        if (musicManager == null) {
            musicManager = new GuildMusicManager(playerManager);
            musicManagers.put(guildId, musicManager);
        }

        guild.getAudioManager().setSendingHandler(musicManager.getSendHandler());

        return musicManager;
    }

    private static boolean isNumber(String string) {
        try{
            int size = Integer.parseInt(string);
            return true;
        } catch(IllegalArgumentException exception) {
            return false;
        }
    }
MCOfficer commented 4 years ago

sounds suspiciously like a case of #16. Do you have nothing in the error log?

Also provide your LP version.

PS: Codeblocks are surrounded by triple backticks ```

officialLucas commented 4 years ago

Hey I am using the Lavaplayer 1.3.55 version. There is no error in the console.

becauseimdavid commented 3 years ago

I've got the same issue

cFerg commented 3 years ago

I would recommend combining VolumeCommand() and getGuildAudioPlayer() if you can

My examples are from Kotlin, but it should work the same - I didn't see a player or listeners, which is prob what's missing.

the DefaultAudioPlayerManager is key here you can set it to a variable, say:

val playerManager = DefaultAudioPlayerManager()

Then create your player using the variable

val guildAudioPlayer = playerManager.createPlayer()

Then add your listeners

guildAudioPlayer.addListener(AudioEventHandler(guild))
audioManager.sendingHandler = AudioPlayerSendHandler(guildAudioPlayer)

(I also add the sources here using the manager, and then save the player and manager to a map based on the guild)

To set the volume, you should then use the player from the AudioPlayer

sedmelluq commented 3 years ago

There is no error in the console.

I have conveniently made an FAQ section about what to do if there "are no errors": https://github.com/sedmelluq/lavaplayer/blob/master/FAQ.md#a7c7989f