Esempio n. 1
0
  @Override
  public Message sendMessage(Message msg) {
    checkVerification();
    SelfInfo self = getJDA().getSelfInfo();
    if (!checkPermission(self, Permission.MESSAGE_WRITE))
      throw new PermissionException(Permission.MESSAGE_WRITE);

    JDAImpl api = (JDAImpl) getJDA();
    if (api.getMessageLimit(guild.getId()) != null) {
      throw new RateLimitedException(
          api.getMessageLimit(guild.getId()) - System.currentTimeMillis());
    }
    try {
      Requester.Response response =
          api.getRequester()
              .post(
                  Requester.DISCORD_API_PREFIX + "channels/" + getId() + "/messages",
                  new JSONObject().put("content", msg.getRawContent()).put("tts", msg.isTTS()));
      if (response.isRateLimit()) {
        long retry_after = response.getObject().getLong("retry_after");
        api.setMessageTimeout(guild.getId(), retry_after);
        throw new RateLimitedException(retry_after);
      }
      if (!response.isOk()) // sending failed (Verification-level?)
      return null;
      return new EntityBuilder(api).createMessage(response.getObject());
    } catch (JSONException ex) {
      JDAImpl.LOG.log(ex);
      // sending failed
      return null;
    }
  }