public static synchronized void stop(JDA api, String ratelimitIdentifier) { Map<String, AsyncMessageSender> senders = instances.get(api); if (senders != null && !senders.isEmpty()) { AsyncMessageSender sender = senders.get(ratelimitIdentifier); if (sender != null) { sender.kill(); senders.remove(ratelimitIdentifier); } } }
@Override public void sendMessageAsync(Message msg, Consumer<Message> callback) { checkVerification(); SelfInfo self = getJDA().getSelfInfo(); if (!checkPermission(self, Permission.MESSAGE_WRITE)) throw new PermissionException(Permission.MESSAGE_WRITE); ((MessageImpl) msg).setChannelId(id); AsyncMessageSender.getInstance(getJDA(), guild.getId()).enqueue(msg, false, callback); }
@Override public void run() { sending: // Label so that, if needed, we can completely kill the while loop from inside the // nested loop. while (sender.alive) { Queue<Task> queue = sender.getQueue(); while (sender.alive && !queue.isEmpty()) { Long messageLimit = sender.api.getMessageLimit(sender.ratelimitIdentifier); if (messageLimit != null) { try { Thread.sleep(messageLimit - System.currentTimeMillis()); } catch (InterruptedException e) { JDAImpl.LOG.log(e); } } Task task = queue.peek(); Message msg = task.message; Requester.Response response; if (sender.api.getTextChannelById(msg.getChannelId()) == null && sender.api.getPrivateChannelById(msg.getChannelId()) == null) { // We no longer have access to the MessageChannel that this message is queued to // send to. This is most likely because it was deleted. AsyncMessageSender.stop(sender.api, sender.ratelimitIdentifier); break sending; } if (task.isEdit) { response = sender .api .getRequester() .patch( Requester.DISCORD_API_PREFIX + "channels/" + msg.getChannelId() + "/messages/" + msg.getId(), new JSONObject().put("content", msg.getRawContent())); } else { response = sender .api .getRequester() .post( Requester.DISCORD_API_PREFIX + "channels/" + msg.getChannelId() + "/messages", new JSONObject() .put("content", msg.getRawContent()) .put("tts", msg.isTTS())); } if (response.responseText == null) { JDAImpl.LOG.debug( "Error sending async-message (returned null-text)... Retrying after 1s"); sender.api.setMessageTimeout(sender.ratelimitIdentifier, 1000); } else if (!response.isRateLimit()) // success/unrecoverable error { queue.poll(); // remove from queue try { if (response.isOk()) { if (task.callback != null) task.callback.accept( new EntityBuilder(sender.api).createMessage(response.getObject())); } else { // if response didn't have id, sending failed (due to permission/blocked pm,... JDAImpl.LOG.fatal( "Could not send/update async message to channel: " + msg.getChannelId() + ". Discord-response: " + response.toString()); if (task.callback != null) task.callback.accept(null); } } catch (JSONException ex) { // could not generate message from json JDAImpl.LOG.log(ex); } catch (IllegalArgumentException ex) { JDAImpl.LOG.log(ex); } } else { sender.api.setMessageTimeout( sender.ratelimitIdentifier, response.getObject().getLong("retry_after")); } if (queue.isEmpty()) { queue = sender.getQueue(); } } sender.waitNew(); } }