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;
}
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;
}
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());
}
}
}));
}
}
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);
}
}));
}
}
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());
}
Aggregations