Search in sources :

Example 1 with AudioPlayback

use of net.robinfriedli.aiode.audio.AudioPlayback in project aiode by robinfriedli.

the class GuildManager method initializeGuild.

private GuildContext initializeGuild(Guild guild) {
    GuildContext createdContext = hibernateComponent.invokeWithSession(session -> {
        AudioPlayer player = audioManager.getPlayerManager().createPlayer();
        Optional<Long> existingSpecification = queryBuilderFactory.select(GuildSpecification.class, "pk", Long.class).where((cb, root) -> cb.equal(root.get("guildId"), guild.getId())).build(session).uniqueResultOptional();
        if (existingSpecification.isPresent()) {
            return new GuildContext(guild, new AudioPlayback(player, guild), existingSpecification.get());
        } else {
            GuildSpecification newSpecification = new GuildSpecification(guild);
            session.persist(newSpecification);
            commandManager.getCommandContributionContext().query(attribute("restrictedAccess").is(true), CommandContribution.class).getResultStream().forEach(restrictedCommand -> {
                AccessConfiguration permissionConfiguration = new AccessConfiguration(restrictedCommand, session);
                session.persist(permissionConfiguration);
                newSpecification.addAccessConfiguration(permissionConfiguration);
            });
            session.flush();
            GuildContext guildContext = new GuildContext(guild, new AudioPlayback(player, guild), newSpecification.getPk());
            handleNewGuild(guild, guildContext);
            return guildContext;
        }
    });
    guildContexts.put(guild, createdContext);
    return createdContext;
}
Also used : AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration)

Example 2 with AudioPlayback

use of net.robinfriedli.aiode.audio.AudioPlayback in project aiode by robinfriedli.

the class GuildManager method getActiveGuilds.

/**
 * Return guilds that are active now (playing music) or were active withing the specified amount of milliseconds
 * (by entering a command or listening a song).
 *
 * @param session the hibernate session
 * @param delayMs the maximum amount of time since the last action for a guild to be considered active in milliseconds
 * @return all active guilds
 */
public Set<Guild> getActiveGuilds(Session session, long delayMs) {
    Aiode aiode = Aiode.get();
    ShardManager shardManager = aiode.getShardManager();
    QueryBuilderFactory queryBuilderFactory = aiode.getQueryBuilderFactory();
    Set<Guild> activeGuilds = Sets.newHashSet();
    Set<String> activeGuildIds = Sets.newHashSet();
    if (ExecutionContext.Current.isSet()) {
        activeGuilds.add(ExecutionContext.Current.require().getGuild());
    }
    for (Guild guild : shardManager.getGuilds()) {
        AudioPlayback playback = audioManager.getPlaybackForGuild(guild);
        if (playback.isPlaying()) {
            activeGuilds.add(guild);
        }
    }
    long startMillis = System.currentTimeMillis() - delayMs;
    Set<String> recentCommandGuildIds = queryBuilderFactory.select(CommandHistory.class, "guildId", String.class).where((cb, root) -> cb.greaterThan(root.get("startMillis"), startMillis)).build(session).getResultStream().collect(Collectors.toSet());
    activeGuildIds.addAll(recentCommandGuildIds);
    LocalDateTime dateTime10MinutesAgo = LocalDateTime.ofInstant(Instant.ofEpochMilli(startMillis), ZoneId.systemDefault());
    Set<String> recentPlaybackGuildIds = queryBuilderFactory.select(PlaybackHistory.class, "guildId", String.class).where((cb, root) -> cb.greaterThan(root.get("timestamp"), dateTime10MinutesAgo)).build(session).getResultStream().collect(Collectors.toSet());
    activeGuildIds.addAll(recentPlaybackGuildIds);
    for (String guildId : activeGuildIds) {
        Guild guild = shardManager.getGuildById(guildId);
        if (guild != null) {
            activeGuilds.add(guild);
        }
    }
    return activeGuilds;
}
Also used : AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) PlaylistItemTimestampInterceptor(net.robinfriedli.aiode.persist.interceptors.PlaylistItemTimestampInterceptor) PlaybackHistory(net.robinfriedli.aiode.entities.PlaybackHistory) JxpBackend(net.robinfriedli.jxp.api.JxpBackend) LoggerFactory(org.slf4j.LoggerFactory) Member(net.dv8tion.jda.api.entities.Member) TextChannel(net.dv8tion.jda.api.entities.TextChannel) HibernatePlaylistMigrator(net.robinfriedli.aiode.persist.tasks.HibernatePlaylistMigrator) ExecutionContext(net.robinfriedli.aiode.concurrent.ExecutionContext) Guild(net.dv8tion.jda.api.entities.Guild) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) Playlist(net.robinfriedli.aiode.entities.Playlist) Map(java.util.Map) Resource(org.springframework.core.io.Resource) EmbedDocumentContribution(net.robinfriedli.aiode.entities.xml.EmbedDocumentContribution) CommandManager(net.robinfriedli.aiode.command.CommandManager) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) SessionFactory(org.hibernate.SessionFactory) Set(java.util.Set) AudioManager(net.robinfriedli.aiode.audio.AudioManager) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) Sets(com.google.common.collect.Sets) List(java.util.List) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration) SnowflakeMap(net.robinfriedli.aiode.util.SnowflakeMap) Optional(java.util.Optional) InterceptorChain(net.robinfriedli.aiode.persist.interceptors.InterceptorChain) HibernateComponent(net.robinfriedli.aiode.boot.configurations.HibernateComponent) PlaylistItem(net.robinfriedli.aiode.entities.PlaylistItem) MessageChannel(net.dv8tion.jda.api.entities.MessageChannel) AbstractGuildProperty(net.robinfriedli.aiode.discord.property.AbstractGuildProperty) LocalDateTime(java.time.LocalDateTime) Session(org.hibernate.Session) MutexSync(net.robinfriedli.exec.MutexSync) Value(org.springframework.beans.factory.annotation.Value) Strings(com.google.common.base.Strings) QueryBuilderFactory(net.robinfriedli.aiode.persist.qb.QueryBuilderFactory) GuildPropertyManager(net.robinfriedli.aiode.discord.property.GuildPropertyManager) SearchEngine(net.robinfriedli.aiode.util.SearchEngine) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) Nullable(javax.annotation.Nullable) VerifyPlaylistInterceptor(net.robinfriedli.aiode.persist.interceptors.VerifyPlaylistInterceptor) Context(net.robinfriedli.jxp.persist.Context) Logger(org.slf4j.Logger) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) CommandHistory(net.robinfriedli.aiode.entities.CommandHistory) IOException(java.io.IOException) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Conditions(net.robinfriedli.jxp.queries.Conditions) HibernateInvoker(net.robinfriedli.aiode.function.HibernateInvoker) Aiode(net.robinfriedli.aiode.Aiode) SpotifyApi(se.michaelthelin.spotify.SpotifyApi) Component(org.springframework.stereotype.Component) LocalDateTime(java.time.LocalDateTime) QueryBuilderFactory(net.robinfriedli.aiode.persist.qb.QueryBuilderFactory) AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) Guild(net.dv8tion.jda.api.entities.Guild) Aiode(net.robinfriedli.aiode.Aiode)

Example 3 with AudioPlayback

use of net.robinfriedli.aiode.audio.AudioPlayback in project aiode by robinfriedli.

the class VoiceChannelListener method onGuildVoiceLeave.

@Override
public void onGuildVoiceLeave(GuildVoiceLeaveEvent event) {
    if (!event.getMember().getUser().isBot()) {
        Guild guild = event.getGuild();
        EventHandlerPool.execute(() -> xSync.execute(guild.getIdLong(), () -> {
            VoiceChannel channel = event.getChannelLeft();
            AudioPlayback playback = audioManager.getPlaybackForGuild(guild);
            if (channel.equals(playback.getVoiceChannel()) && noOtherMembersLeft(channel, guild)) {
                if (isAutoPauseEnabled(guild)) {
                    playback.pause();
                    playback.leaveChannel();
                } else {
                    playback.setAloneSince(LocalDateTime.now());
                }
            }
        }));
    }
}
Also used : AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) VoiceChannel(net.dv8tion.jda.api.entities.VoiceChannel) Guild(net.dv8tion.jda.api.entities.Guild)

Example 4 with AudioPlayback

use of net.robinfriedli.aiode.audio.AudioPlayback in project aiode by robinfriedli.

the class VoiceChannelListener method onGuildVoiceJoin.

@Override
public void onGuildVoiceJoin(@Nonnull GuildVoiceJoinEvent event) {
    if (!event.getMember().getUser().isBot()) {
        Guild guild = event.getGuild();
        EventHandlerPool.execute(() -> xSync.execute(guild.getIdLong(), () -> {
            AudioPlayback playback = audioManager.getPlaybackForGuild(guild);
            if (event.getChannelJoined().equals(playback.getVoiceChannel())) {
                playback.setAloneSince(null);
            }
        }));
    }
}
Also used : AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) Guild(net.dv8tion.jda.api.entities.Guild)

Example 5 with AudioPlayback

use of net.robinfriedli.aiode.audio.AudioPlayback in project aiode by robinfriedli.

the class ClearCommand method doRun.

@Override
public void doRun() {
    AudioPlayback playback = Aiode.get().getAudioManager().getPlaybackForGuild(getContext().getGuild());
    AudioQueue audioQueue = playback.getAudioQueue();
    audioQueue.clear(playback.isPlaying());
}
Also used : AudioPlayback(net.robinfriedli.aiode.audio.AudioPlayback) AudioQueue(net.robinfriedli.aiode.audio.AudioQueue)

Aggregations

AudioPlayback (net.robinfriedli.aiode.audio.AudioPlayback)25 Guild (net.dv8tion.jda.api.entities.Guild)13 AudioManager (net.robinfriedli.aiode.audio.AudioManager)9 AudioQueue (net.robinfriedli.aiode.audio.AudioQueue)7 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)6 VoiceChannel (net.dv8tion.jda.api.entities.VoiceChannel)5 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)4 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)2 IOException (java.io.IOException)2 LocalDateTime (java.time.LocalDateTime)2 MessageChannel (net.dv8tion.jda.api.entities.MessageChannel)2 Aiode (net.robinfriedli.aiode.Aiode)2 Playable (net.robinfriedli.aiode.audio.Playable)2 AccessConfiguration (net.robinfriedli.aiode.entities.AccessConfiguration)2 GuildSpecification (net.robinfriedli.aiode.entities.GuildSpecification)2 NoResultsFoundException (net.robinfriedli.aiode.exceptions.NoResultsFoundException)2 Strings (com.google.common.base.Strings)1 Sets (com.google.common.collect.Sets)1 OutputStream (java.io.OutputStream)1 Duration (java.time.Duration)1