@Override public boolean onMessage(final Message message) { if (message instanceof ClockMessage) { final ClockMessage clockMessage = (ClockMessage) message; MessageScheduler.getInstance().removeClock(clockMessage.getClockId()); if (this.m_cancelTimedPop) { return false; } this.popup(); return false; } else { switch (message.getId()) { case 19301: { final UIShowPopupInfosMessage msg = (UIShowPopupInfosMessage) message; boolean ok = false; if (this.m_popupDialog == null || this.m_fieldProvider == null) { MessageScheduler.getInstance().removeAllClocks(this); return false; } if (this.m_fieldProvider instanceof SpellLevel && msg.getContent() instanceof SpellLevel) { final SpellLevel spellLevel1 = (SpellLevel) this.m_fieldProvider; final SpellLevel spellLevel2 = (SpellLevel) msg.getContent(); ok = (spellLevel1.getSpell().getId() == spellLevel2.getSpell().getId()); } else { ok = this.m_fieldProvider.equals(msg.getContent()); } if (ok) { this.m_cancelTimedPop = !XulorActions.closePopup(null, this.m_popupDialog); PropertiesProvider.getInstance().removeProperty(this.m_property); } return false; } case 19300: { final UIShowPopupInfosMessage msg = (UIShowPopupInfosMessage) message; this.m_fieldProvider = msg.getContent(); if (this.m_fieldProvider == null) { return false; } if (msg.getIntValue() > 0) { this.m_cancelTimedPop = false; MessageScheduler.getInstance().addClock(this, msg.getIntValue(), -1, 1); } else { this.popup(); } return false; } default: { return true; } } } }
public void runHibernate() { // ############################################################################ Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); Message message = new Message("Hello World"); session.save(message); tx.commit(); session.close(); // ############################################################################ Session secondSession = HibernateUtil.getSessionFactory().openSession(); Transaction secondTransaction = secondSession.beginTransaction(); List messages = secondSession.createQuery("from Message m order by m.text asc").list(); System.out.println(messages.size() + " message(s) found:"); for (Iterator iter = messages.iterator(); iter.hasNext(); ) { Message loadedMsg = (Message) iter.next(); System.out.println(loadedMsg.getText()); } secondTransaction.commit(); secondSession.close(); // ############################################################################ Session thirdSession = HibernateUtil.getSessionFactory().openSession(); Transaction thirdTransaction = thirdSession.beginTransaction(); // message.getId() holds the identifier value of the first message Message loadedMessage = (Message) thirdSession.get(Message.class, message.getId()); loadedMessage.setText("Greetings Earthling"); loadedMessage.setNextMessage(new Message("Take me to your leader (please)")); thirdTransaction.commit(); thirdSession.close(); // ############################################################################ // Final unit of work (just repeat the query) // TODO: You can move this query into the thirdSession before the // commit, makes more sense! Session fourthSession = HibernateUtil.getSessionFactory().openSession(); Transaction fourthTransaction = fourthSession.beginTransaction(); messages = fourthSession.createQuery("from Message m order by m.text asc").list(); System.out.println(messages.size() + " message(s) found:"); for (Iterator iter = messages.iterator(); iter.hasNext(); ) { Message loadedMsg = (Message) iter.next(); System.out.println(loadedMsg.getText()); } fourthTransaction.commit(); fourthSession.close(); // Shutting down the application HibernateUtil.shutdown(); }
private void vanish(MessageContext context, String args) { Message message = context.getMessage(); if (args.isEmpty()) { return; } try { final int cap = 100; final int capPages = 10; String[] vals = args.split(" "); String userId; int numMsgs; if (vals.length == 2) { User user = findUser(context, vals[0]); userId = user.getId(); if (user == NO_USER) { userId = vals[0]; } numMsgs = Math.max(1, Math.min(cap, Integer.parseInt(vals[1]))); } else if (vals.length == 1) { userId = ""; numMsgs = Math.max(1, Math.min(cap, Integer.parseInt(vals[0]))); } else { userId = ""; numMsgs = 10; } int limit = numMsgs; String before = message.getId(); // Limit search to 10 pages (500 msgs) // Instead of deleting them when we find them, we build a list instead List<String> messagesToDelete = new ArrayList<>(limit); for (int k = 0; k < capPages && limit > 0; k++) { Map<String, String> headers = new HashMap<>(); headers.put(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType()); headers.put(HttpHeaders.AUTHORIZATION, apiClient.getToken()); Map<String, Object> queryParams = new HashMap<>(); queryParams.put("limit", 50); queryParams.put("before", before); HttpResponse<JsonNode> response = Unirest.get(ApiConst.CHANNELS_ENDPOINT + message.getChannelId() + "/messages") .headers(headers) .queryString(queryParams) .asJson(); JSONArray ret = response.getBody().getArray(); if (userId.isEmpty()) { for (int i = 0; i < ret.length() && limit > 0; i++) { JSONObject msg = ret.getJSONObject(i); String mid = msg.getString("id"); before = mid; messagesToDelete.add(mid); limit--; } } else { for (int i = 0; i < ret.length() && limit > 0; i++) { JSONObject msg = ret.getJSONObject(i); String mid = msg.getString("id"); JSONObject msgUsr = msg.getJSONObject("author"); String uid = msgUsr.getString("id"); before = mid; if (userId.equals(uid)) { messagesToDelete.add(mid); limit--; } } } } LOGGER.info("Deleting {} messages", messagesToDelete.size()); // Using bulk delete endpoint apiClient.bulkDeleteMessages( context.getChannel().getId(), messagesToDelete.toArray(new String[messagesToDelete.size()])); } catch (Exception e) { LOGGER.warn("Unable to get messages", e); } }
@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(); } }