use of net.dv8tion.jda.internal.JDAImpl in project JDA by DV8FromTheWorld.
the class AudioConnection method prepareReady.
/* Used by AudioWebSocket */
protected void prepareReady() {
Thread readyThread = new Thread(() -> {
getJDA().setContext();
final long timeout = getGuild().getAudioManager().getConnectTimeout();
final long started = System.currentTimeMillis();
while (!webSocket.isReady()) {
if (timeout > 0 && System.currentTimeMillis() - started > timeout)
break;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
LOG.error("AudioConnection ready thread got interrupted while sleeping", e);
Thread.currentThread().interrupt();
}
}
if (webSocket.isReady()) {
setupSendSystem();
setupReceiveSystem();
} else {
webSocket.close(ConnectionStatus.ERROR_CONNECTION_TIMEOUT);
}
});
readyThread.setUncaughtExceptionHandler((thread, throwable) -> {
LOG.error("Uncaught exception in Audio ready-thread", throwable);
JDAImpl api = getJDA();
api.handleEvent(new ExceptionEvent(api, throwable, true));
});
readyThread.setDaemon(true);
readyThread.setName(threadIdentifier + " Ready Thread");
readyThread.start();
}
use of net.dv8tion.jda.internal.JDAImpl in project JDA by DV8FromTheWorld.
the class AudioConnection method setupCombinedExecutor.
private synchronized void setupCombinedExecutor() {
if (combinedAudioExecutor == null) {
combinedAudioExecutor = Executors.newSingleThreadScheduledExecutor((task) -> {
final Thread t = new Thread(task, threadIdentifier + " Combined Thread");
t.setDaemon(true);
t.setUncaughtExceptionHandler((thread, throwable) -> {
LOG.error("I have no idea how, but there was an uncaught exception in the combinedAudioExecutor", throwable);
JDAImpl api = getJDA();
api.handleEvent(new ExceptionEvent(api, throwable, true));
});
return t;
});
combinedAudioExecutor.scheduleAtFixedRate(() -> {
getJDA().setContext();
try {
List<User> users = new LinkedList<>();
List<short[]> audioParts = new LinkedList<>();
if (receiveHandler != null && receiveHandler.canReceiveCombined()) {
long currentTime = System.currentTimeMillis();
for (Map.Entry<User, Queue<AudioData>> entry : combinedQueue.entrySet()) {
User user = entry.getKey();
Queue<AudioData> queue = entry.getValue();
if (queue.isEmpty())
continue;
AudioData audioData = queue.poll();
// Make sure the audio packet is younger than 100ms
while (audioData != null && currentTime - audioData.time > queueTimeout) {
audioData = queue.poll();
}
// If none of the audio packets were younger than 100ms, then there is nothing to add.
if (audioData == null) {
continue;
}
users.add(user);
audioParts.add(audioData.data);
}
if (!audioParts.isEmpty()) {
int audioLength = audioParts.stream().mapToInt(it -> it.length).max().getAsInt();
// 960 PCM samples for each channel
short[] mix = new short[1920];
int sample;
for (int i = 0; i < audioLength; i++) {
sample = 0;
for (Iterator<short[]> iterator = audioParts.iterator(); iterator.hasNext(); ) {
short[] audio = iterator.next();
if (i < audio.length)
sample += audio[i];
else
iterator.remove();
}
if (sample > Short.MAX_VALUE)
mix[i] = Short.MAX_VALUE;
else if (sample < Short.MIN_VALUE)
mix[i] = Short.MIN_VALUE;
else
mix[i] = (short) sample;
}
receiveHandler.handleCombinedAudio(new CombinedAudio(users, mix));
} else {
// No audio to mix, provide 20 MS of silence. (960 PCM samples for each channel)
receiveHandler.handleCombinedAudio(new CombinedAudio(Collections.emptyList(), new short[1920]));
}
}
} catch (Exception e) {
LOG.error("There was some unexpected exception in the combinedAudioExecutor!", e);
}
}, 0, 20, TimeUnit.MILLISECONDS);
}
}
use of net.dv8tion.jda.internal.JDAImpl in project SkyBot by DuncteBot.
the class CommandManager method runCustomCommand.
private void runCustomCommand(ICommand cmd, String invoke, List<String> args, GuildMessageReceivedEvent event) {
final CustomCommand cusomCommand = (CustomCommand) cmd;
if (cusomCommand.getGuildId() != event.getGuild().getIdLong()) {
return;
}
try {
MDC.put("command.custom.message", cusomCommand.getMessage());
final Parser parser = CommandUtils.getParser(new CommandContext(invoke, args, event, variables));
final String message = parser.parse(cusomCommand.getMessage());
final MessageConfig.Builder messageBuilder = MessageConfig.Builder.fromEvent(event);
final DataObject object = parser.get("embed");
boolean hasContent = false;
if (!message.isEmpty()) {
messageBuilder.setMessage("\u200B" + message);
hasContent = true;
}
if (object != null) {
final JDAImpl jda = (JDAImpl) event.getJDA();
final EmbedBuilder embed = new EmbedBuilder(jda.getEntityBuilder().createMessageEmbed(object));
messageBuilder.addEmbed(true, embed);
hasContent = true;
}
if (hasContent) {
sendMsg(messageBuilder.build());
}
parser.clear();
} catch (Exception e) {
sendMsg(MessageConfig.Builder.fromEvent(event).setMessage("Error with parsing custom command: " + e.getMessage()).build());
Sentry.captureException(e);
}
}
use of net.dv8tion.jda.internal.JDAImpl in project SkyBot by duncte123.
the class CommandManager method runCustomCommand.
private void runCustomCommand(ICommand cmd, String invoke, List<String> args, GuildMessageReceivedEvent event) {
final CustomCommand cusomCommand = (CustomCommand) cmd;
if (cusomCommand.getGuildId() != event.getGuild().getIdLong()) {
return;
}
try {
MDC.put("command.custom.message", cusomCommand.getMessage());
final Parser parser = CommandUtils.getParser(new CommandContext(invoke, args, event, variables));
final String message = parser.parse(cusomCommand.getMessage());
final MessageConfig.Builder messageBuilder = MessageConfig.Builder.fromEvent(event);
final DataObject object = parser.get("embed");
boolean hasContent = false;
if (!message.isEmpty()) {
messageBuilder.setMessage("\u200B" + message);
hasContent = true;
}
if (object != null) {
final JDAImpl jda = (JDAImpl) event.getJDA();
final EmbedBuilder embed = new EmbedBuilder(jda.getEntityBuilder().createMessageEmbed(object));
messageBuilder.addEmbed(true, embed);
hasContent = true;
}
if (hasContent) {
sendMsg(messageBuilder.build());
}
parser.clear();
} catch (Exception e) {
sendMsg(MessageConfig.Builder.fromEvent(event).setMessage("Error with parsing custom command: " + e.getMessage()).build());
Sentry.captureException(e);
}
}
use of net.dv8tion.jda.internal.JDAImpl in project SkyBot by duncte123.
the class ChangeLogCommand method execute.
@Override
public void execute(@Nonnull CommandContext ctx) {
if (embedJson == null || embedJson.isEmpty()) {
fetchLatetstGitHubCommits(ctx);
return;
}
final JDAImpl jda = (JDAImpl) ctx.getJDA();
final MessageEmbed embed = jda.getEntityBuilder().createMessageEmbed(DataObject.fromJson(embedJson));
sendEmbed(ctx, new EmbedBuilder(embed));
}
Aggregations