Search in sources :

Example 1 with MusicDto

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

the class IntroSongCommand method persistMusicUrl.

private void persistMusicUrl(UserDto targetDto, Integer deleteDelay, TextChannel channel, String filename, String url, String memberName, int introVolume) {
    MusicDto musicFileDto = targetDto.getMusicDto();
    byte[] urlBytes = url.getBytes();
    if (musicFileDto == null) {
        MusicDto musicDto = new MusicDto(targetDto.getDiscordId(), targetDto.getGuildId(), filename, introVolume, urlBytes);
        musicFileService.createNewMusicFile(musicDto);
        targetDto.setMusicDto(musicDto);
        userService.updateUser(targetDto);
        channel.sendMessageFormat("Successfully set %s's intro song to '%s' with volume '%d'", memberName, musicDto.getFileName(), musicDto.getIntroVolume()).queue(message -> ICommand.deleteAfter(message, deleteDelay));
        return;
    }
    musicFileDto.setFileName(filename);
    musicFileDto.setIntroVolume(introVolume);
    musicFileDto.setMusicBlob(urlBytes);
    musicFileService.updateMusicFile(musicFileDto);
    channel.sendMessageFormat("Successfully updated %s's intro song to '%s' with volume '%d'", memberName, filename, introVolume).queue(message -> ICommand.deleteAfter(message, deleteDelay));
}
Also used : MusicDto(toby.jpa.dto.MusicDto)

Example 2 with MusicDto

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

the class IntroSongCommand method persistMusicFile.

private void persistMusicFile(UserDto targetDto, Integer deleteDelay, TextChannel channel, String filename, int introVolume, InputStream inputStream, String memberName) {
    byte[] fileContents;
    try {
        fileContents = readInputStreamToByteArray(inputStream);
    } catch (ExecutionException | InterruptedException | IOException e) {
        channel.sendMessageFormat("Unable to read file '%s'", filename).queue(message -> ICommand.deleteAfter(message, deleteDelay));
        return;
    }
    MusicDto musicFileDto = targetDto.getMusicDto();
    if (musicFileDto == null) {
        MusicDto musicDto = new MusicDto(targetDto.getDiscordId(), targetDto.getGuildId(), filename, introVolume, fileContents);
        musicFileService.createNewMusicFile(musicDto);
        targetDto.setMusicDto(musicDto);
        userService.updateUser(targetDto);
        channel.sendMessageFormat("Successfully set %s's intro song to '%s' with volume '%d'", memberName, musicDto.getFileName(), musicDto.getIntroVolume()).queue(message -> ICommand.deleteAfter(message, deleteDelay));
        return;
    }
    musicFileDto.setFileName(filename);
    musicFileDto.setIntroVolume(introVolume);
    musicFileDto.setMusicBlob(fileContents);
    musicFileService.updateMusicFile(musicFileDto);
    channel.sendMessageFormat("Successfully updated %s's intro song to '%s' with volume '%d'", memberName, filename, introVolume).queue(message -> ICommand.deleteAfter(message, deleteDelay));
}
Also used : Message(net.dv8tion.jda.api.entities.Message) CommandContext(toby.command.CommandContext) IConfigService(toby.jpa.service.IConfigService) Arrays(java.util.Arrays) IMusicFileService(toby.jpa.service.IMusicFileService) UserDto(toby.jpa.dto.UserDto) IOException(java.io.IOException) UserDtoHelper.calculateUserDto(toby.helpers.UserDtoHelper.calculateUserDto) Member(net.dv8tion.jda.api.entities.Member) TextChannel(net.dv8tion.jda.api.entities.TextChannel) URLHelper(toby.helpers.URLHelper) IUserService(toby.jpa.service.IUserService) Collectors(java.util.stream.Collectors) MusicDto(toby.jpa.dto.MusicDto) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) ConfigDto(toby.jpa.dto.ConfigDto) ICommand(toby.command.ICommand) FileUtils.readInputStreamToByteArray(toby.helpers.FileUtils.readInputStreamToByteArray) URI(java.net.URI) InputStream(java.io.InputStream) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) MusicDto(toby.jpa.dto.MusicDto)

Example 3 with MusicDto

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

the class MusicPlayerHelper method playUserIntro.

public static void playUserIntro(UserDto dbUser, Guild guild, TextChannel channel, int deleteDelay) {
    MusicDto musicDto = dbUser.getMusicDto();
    PlayerManager instance = PlayerManager.getInstance();
    int currentVolume = PlayerManager.getInstance().getMusicManager(guild).getAudioPlayer().getVolume();
    if (musicDto != null && musicDto.getFileName() != null) {
        Integer introVolume = musicDto.getIntroVolume();
        changeVolumeForIntro(channel, deleteDelay, currentVolume, introVolume);
        PlayerManager.getInstance().getMusicManager(guild).getAudioPlayer().setVolume(introVolume != null ? introVolume : currentVolume);
        instance.loadAndPlay(guild.getSystemChannel(), String.format(ConsumeWebService.getWebUrl() + "/music?id=%s", musicDto.getId()), true, 0);
    } else if (musicDto != null) {
        Integer introVolume = musicDto.getIntroVolume();
        PlayerManager.getInstance().getMusicManager(guild).getAudioPlayer().setVolume(introVolume != null ? introVolume : currentVolume);
        changeVolumeForIntro(channel, deleteDelay, currentVolume, introVolume);
        instance.setPreviousVolume(currentVolume);
        instance.loadAndPlay(guild.getSystemChannel(), Arrays.toString(dbUser.getMusicDto().getMusicBlob()), true, 0);
    }
}
Also used : PlayerManager(toby.lavaplayer.PlayerManager) MusicDto(toby.jpa.dto.MusicDto)

Example 4 with MusicDto

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

the class UserDtoHelper method calculateUserDto.

public static UserDto calculateUserDto(long guildId, long discordId, boolean isSuperUser, IUserService userService, int introVolume) {
    Optional<UserDto> dbUserDto = userService.listGuildUsers(guildId).stream().filter(userDto -> userDto.getGuildId().equals(guildId) && userDto.getDiscordId().equals(discordId)).findFirst();
    if (dbUserDto.isEmpty()) {
        UserDto userDto = new UserDto();
        userDto.setDiscordId(discordId);
        userDto.setGuildId(guildId);
        userDto.setSuperUser(isSuperUser);
        MusicDto musicDto = new MusicDto(userDto.getDiscordId(), userDto.getGuildId(), null, introVolume, null);
        userDto.setMusicDto(musicDto);
        return userService.createNewUser(userDto);
    }
    return userService.getUserById(discordId, guildId);
}
Also used : UserDto(toby.jpa.dto.UserDto) Optional(java.util.Optional) IUserService(toby.jpa.service.IUserService) MusicDto(toby.jpa.dto.MusicDto) UserDto(toby.jpa.dto.UserDto) MusicDto(toby.jpa.dto.MusicDto)

Example 5 with MusicDto

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

the class MusicFilePersistenceImpl method getMusicFileById.

@Override
public MusicDto getMusicFileById(String id) {
    Query q = em.createNamedQuery("MusicDto.getById", MusicDto.class);
    q.setParameter("id", id);
    return (MusicDto) q.getSingleResult();
}
Also used : Query(javax.persistence.Query) MusicDto(toby.jpa.dto.MusicDto)

Aggregations

MusicDto (toby.jpa.dto.MusicDto)14 UserDto (toby.jpa.dto.UserDto)8 Test (org.junit.jupiter.api.Test)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 IUserService (toby.jpa.service.IUserService)3 Arrays (java.util.Arrays)2 List (java.util.List)2 Message (net.dv8tion.jda.api.entities.Message)2 TextChannel (net.dv8tion.jda.api.entities.TextChannel)2 CommandContext (toby.command.CommandContext)2 ICommand (toby.command.ICommand)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URL (java.net.URL)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 ExecutionException (java.util.concurrent.ExecutionException)1 Collectors (java.util.stream.Collectors)1 Query (javax.persistence.Query)1