Search in sources :

Example 1 with ConfigDto

use of toby.jpa.dto.ConfigDto in project toby-bot by ml404.

the class Handler method onGuildVoiceMove.

@Override
public void onGuildVoiceMove(GuildVoiceMoveEvent event) {
    Guild guild = event.getGuild();
    AudioManager audioManager = guild.getAudioManager();
    String volumePropertyName = ConfigDto.Configurations.VOLUME.getConfigValue();
    ConfigDto databaseConfig = configService.getConfigByName(volumePropertyName, event.getGuild().getId());
    int defaultVolume = databaseConfig != null ? Integer.parseInt(databaseConfig.getValue()) : 100;
    List<Member> nonBotConnectedMembersInOldChannel = event.getChannelLeft().getMembers().stream().filter(member -> !member.getUser().isBot()).collect(Collectors.toList());
    if (Objects.equals(audioManager.getConnectedChannel(), event.getChannelLeft()) && nonBotConnectedMembersInOldChannel.isEmpty()) {
        closeAudioPlayer(guild, audioManager, defaultVolume);
    }
    List<Member> nonBotConnectedMembers = event.getChannelJoined().getMembers().stream().filter(member -> !member.getUser().isBot()).collect(Collectors.toList());
    if (!nonBotConnectedMembers.isEmpty() && !audioManager.isConnected()) {
        PlayerManager.getInstance().getMusicManager(guild).getAudioPlayer().setVolume(defaultVolume);
        audioManager.openAudioConnection(event.getChannelJoined());
    }
}
Also used : AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) MusicPlayerHelper.playUserIntro(toby.helpers.MusicPlayerHelper.playUserIntro) JDA(net.dv8tion.jda.api.JDA) BotMain(toby.BotMain) UserDto(toby.jpa.dto.UserDto) AudioManager(net.dv8tion.jda.api.managers.AudioManager) LoggerFactory(org.slf4j.LoggerFactory) Configurable(org.springframework.beans.factory.annotation.Configurable) Autowired(org.springframework.beans.factory.annotation.Autowired) GuildVoiceLeaveEvent(net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent) EventWaiter(com.jagrosh.jdautilities.commons.waiter.EventWaiter) ReadyEvent(net.dv8tion.jda.api.events.ReadyEvent) ConfigDto(toby.jpa.dto.ConfigDto) Service(org.springframework.stereotype.Service) Nonnull(javax.annotation.Nonnull) GuildMessageReceivedEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent) Logger(org.slf4j.Logger) Emotes(toby.emote.Emotes) GuildVoiceMoveEvent(net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent) ListenerAdapter(net.dv8tion.jda.api.hooks.ListenerAdapter) GuildMusicManager(toby.lavaplayer.GuildMusicManager) CommandManager(toby.managers.CommandManager) Collectors(java.util.stream.Collectors) PlayerManager(toby.lavaplayer.PlayerManager) Objects(java.util.Objects) toby.jpa.service(toby.jpa.service) GuildVoiceJoinEvent(net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent) List(java.util.List) MessageReceivedEvent(net.dv8tion.jda.api.events.message.MessageReceivedEvent) AudioManager(net.dv8tion.jda.api.managers.AudioManager) ConfigDto(toby.jpa.dto.ConfigDto)

Example 2 with ConfigDto

use of toby.jpa.dto.ConfigDto in project toby-bot by ml404.

the class Handler method onGuildVoiceJoin.

// Auto joining voice channel when it becomes occupied and an audio connection doesn't already exist on the server, then play the associated user's intro song
@Override
public void onGuildVoiceJoin(GuildVoiceJoinEvent event) {
    Guild guild = event.getGuild();
    AudioManager audioManager = guild.getAudioManager();
    String volumePropertyName = ConfigDto.Configurations.VOLUME.getConfigValue();
    ConfigDto databaseVolumeConfig = configService.getConfigByName(volumePropertyName, event.getGuild().getId());
    ConfigDto deleteDelayConfig = configService.getConfigByName(ConfigDto.Configurations.DELETE_DELAY.getConfigValue(), event.getGuild().getId());
    int defaultVolume = databaseVolumeConfig != null ? Integer.parseInt(databaseVolumeConfig.getValue()) : 100;
    List<Member> nonBotConnectedMembers = event.getChannelJoined().getMembers().stream().filter(member -> !member.getUser().isBot()).collect(Collectors.toList());
    AudioPlayer audioPlayer = PlayerManager.getInstance().getMusicManager(guild).getAudioPlayer();
    if (!nonBotConnectedMembers.isEmpty() && !audioManager.isConnected()) {
        audioPlayer.setVolume(defaultVolume);
        audioManager.openAudioConnection(event.getChannelJoined());
    }
    Member member = event.getMember();
    long discordId = member.getUser().getIdLong();
    long guildId = member.getGuild().getIdLong();
    UserDto userDto = userService.getUserById(discordId, guildId);
    if (Objects.equals(audioManager.getConnectedChannel(), event.getChannelJoined())) {
        playUserIntro(userDto, guild, guild.getDefaultChannel(), Integer.parseInt(deleteDelayConfig.getValue()));
    }
}
Also used : AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) net.dv8tion.jda.api.entities(net.dv8tion.jda.api.entities) MusicPlayerHelper.playUserIntro(toby.helpers.MusicPlayerHelper.playUserIntro) JDA(net.dv8tion.jda.api.JDA) BotMain(toby.BotMain) UserDto(toby.jpa.dto.UserDto) AudioManager(net.dv8tion.jda.api.managers.AudioManager) LoggerFactory(org.slf4j.LoggerFactory) Configurable(org.springframework.beans.factory.annotation.Configurable) Autowired(org.springframework.beans.factory.annotation.Autowired) GuildVoiceLeaveEvent(net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent) EventWaiter(com.jagrosh.jdautilities.commons.waiter.EventWaiter) ReadyEvent(net.dv8tion.jda.api.events.ReadyEvent) ConfigDto(toby.jpa.dto.ConfigDto) Service(org.springframework.stereotype.Service) Nonnull(javax.annotation.Nonnull) GuildMessageReceivedEvent(net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent) Logger(org.slf4j.Logger) Emotes(toby.emote.Emotes) GuildVoiceMoveEvent(net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent) ListenerAdapter(net.dv8tion.jda.api.hooks.ListenerAdapter) GuildMusicManager(toby.lavaplayer.GuildMusicManager) CommandManager(toby.managers.CommandManager) Collectors(java.util.stream.Collectors) PlayerManager(toby.lavaplayer.PlayerManager) Objects(java.util.Objects) toby.jpa.service(toby.jpa.service) GuildVoiceJoinEvent(net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent) List(java.util.List) MessageReceivedEvent(net.dv8tion.jda.api.events.message.MessageReceivedEvent) AudioManager(net.dv8tion.jda.api.managers.AudioManager) ConfigDto(toby.jpa.dto.ConfigDto) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) UserDto(toby.jpa.dto.UserDto)

Example 3 with ConfigDto

use of toby.jpa.dto.ConfigDto in project toby-bot by ml404.

the class LeaveCommand method handle.

@Override
public void handle(CommandContext ctx, String prefix, UserDto requestingUserDto, Integer deleteDelay) {
    ICommand.deleteAfter(ctx.getMessage(), deleteDelay);
    final TextChannel channel = ctx.getChannel();
    final Member self = ctx.getSelfMember();
    final GuildVoiceState selfVoiceState = self.getVoiceState();
    if (!requestingUserDto.hasMusicPermission()) {
        sendErrorMessage(ctx, channel, deleteDelay);
        return;
    }
    if (!selfVoiceState.inVoiceChannel()) {
        channel.sendMessage("I'm not in a voice channel, somebody shoot this guy").queue(message -> ICommand.deleteAfter(message, deleteDelay));
        return;
    }
    final Member member = ctx.getMember();
    final GuildVoiceState memberVoiceState = member.getVoiceState();
    Guild guild = ctx.getGuild();
    final AudioManager audioManager = guild.getAudioManager();
    final VoiceChannel memberChannel = memberVoiceState.getChannel();
    GuildMusicManager musicManager = PlayerManager.getInstance().getMusicManager(guild);
    if (PlayerManager.getInstance().isCurrentlyStoppable() || member.hasPermission(Permission.KICK_MEMBERS)) {
        String volumePropertyName = ConfigDto.Configurations.VOLUME.getConfigValue();
        ConfigDto databaseConfig = configService.getConfigByName(volumePropertyName, ctx.getGuild().getId());
        int defaultVolume = databaseConfig != null ? Integer.parseInt(databaseConfig.getValue()) : 100;
        musicManager.getScheduler().setLooping(false);
        musicManager.getScheduler().getQueue().clear();
        musicManager.getAudioPlayer().stopTrack();
        musicManager.getAudioPlayer().setVolume(defaultVolume);
        audioManager.closeAudioConnection();
        channel.sendMessageFormat("Disconnecting from `\uD83D\uDD0A %s`", memberChannel.getName()).queue(message -> ICommand.deleteAfter(message, deleteDelay));
    } else {
        sendDeniedStoppableMessage(channel, musicManager, deleteDelay);
    }
}
Also used : AudioManager(net.dv8tion.jda.api.managers.AudioManager) ConfigDto(toby.jpa.dto.ConfigDto) GuildMusicManager(toby.lavaplayer.GuildMusicManager)

Example 4 with ConfigDto

use of toby.jpa.dto.ConfigDto in project toby-bot by ml404.

the class ConfigServiceImplIntegrationTest method testUpdate_thenNewConfigShouldBeReturned.

@Test
public void testUpdate_thenNewConfigShouldBeReturned() {
    ConfigDto configDto = new ConfigDto("TOKEN", "1234", "test");
    configService.createNewConfig(configDto);
    ConfigDto dbConfig1 = configService.getConfigByName(configDto.getName(), configDto.getGuildId());
    ConfigDto configDtoUpdated = new ConfigDto("TOKEN", "12345", "test");
    configService.updateConfig(configDtoUpdated);
    ConfigDto dbConfig2 = configService.getConfigByName(configDto.getName(), configDto.getGuildId());
    int configSize = configService.listGuildConfig("test").size();
    assertEquals(dbConfig1.getName(), configDto.getName());
    assertEquals(dbConfig1.getGuildId(), configDto.getGuildId());
    assertEquals(dbConfig2.getName(), configDtoUpdated.getName());
    assertEquals(dbConfig2.getGuildId(), configDtoUpdated.getGuildId());
    assertEquals(1, configSize);
}
Also used : ConfigDto(toby.jpa.dto.ConfigDto) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with ConfigDto

use of toby.jpa.dto.ConfigDto in project toby-bot by ml404.

the class ConfigServiceImplIntegrationTest method whenValidNameAndGuild_thenConfigShouldBeFound.

@Test
public void whenValidNameAndGuild_thenConfigShouldBeFound() {
    ConfigDto configDto = new ConfigDto("TOKEN", "1234", "test");
    configService.createNewConfig(configDto);
    ConfigDto dbConfig = configService.getConfigByName(configDto.getName(), configDto.getGuildId());
    assertEquals(dbConfig.getName(), configDto.getName());
    assertEquals(dbConfig.getGuildId(), configDto.getGuildId());
}
Also used : ConfigDto(toby.jpa.dto.ConfigDto) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ConfigDto (toby.jpa.dto.ConfigDto)10 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 AudioManager (net.dv8tion.jda.api.managers.AudioManager)5 UserDto (toby.jpa.dto.UserDto)5 Objects (java.util.Objects)4 net.dv8tion.jda.api.entities (net.dv8tion.jda.api.entities)4 EventWaiter (com.jagrosh.jdautilities.commons.waiter.EventWaiter)3 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)3 Nonnull (javax.annotation.Nonnull)3 JDA (net.dv8tion.jda.api.JDA)3 ReadyEvent (net.dv8tion.jda.api.events.ReadyEvent)3 GuildVoiceJoinEvent (net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent)3 GuildVoiceLeaveEvent (net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent)3 GuildVoiceMoveEvent (net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent)3 MessageReceivedEvent (net.dv8tion.jda.api.events.message.MessageReceivedEvent)3 GuildMessageReceivedEvent (net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent)3 ListenerAdapter (net.dv8tion.jda.api.hooks.ListenerAdapter)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3