Example #1
0
  @Override
  public Message sendFile(File file, Message message) {
    checkVerification();
    if (!checkPermission(getJDA().getSelfInfo(), Permission.MESSAGE_WRITE))
      throw new PermissionException(Permission.MESSAGE_WRITE);
    if (!checkPermission(getJDA().getSelfInfo(), Permission.MESSAGE_ATTACH_FILES))
      throw new PermissionException(Permission.MESSAGE_ATTACH_FILES);
    if (file == null || !file.exists() || !file.canRead())
      throw new IllegalArgumentException(
          "Provided file is either null, doesn't exist or is not readable!");
    if (file.length() > 8 << 20) // 8MB
    throw new IllegalArgumentException("File is to big! Max file-size is 8MB");

    JDAImpl api = (JDAImpl) getJDA();
    try {
      MultipartBody body =
          Unirest.post(Requester.DISCORD_API_PREFIX + "channels/" + getId() + "/messages")
              .header("authorization", getJDA().getAuthToken())
              .header("user-agent", Requester.USER_AGENT)
              .field("file", file);
      if (message != null)
        body.field("content", message.getRawContent()).field("tts", message.isTTS());

      String dbg =
          String.format(
              "Requesting %s -> %s\n\tPayload: file: %s, message: %s, tts: %s\n\tResponse: ",
              body.getHttpRequest().getHttpMethod().name(),
              body.getHttpRequest().getUrl(),
              file.getAbsolutePath(),
              message == null ? "null" : message.getRawContent(),
              message == null ? "N/A" : message.isTTS());
      HttpResponse<JsonNode> response = body.asJson();
      Requester.LOG.trace(dbg + body);

      try {
        int status = response.getStatus();

        if (status >= 200 && status < 300) {
          return new EntityBuilder(api).createMessage(response.getBody().getObject());
        } else if (response.getStatus() == 429) {
          long retryAfter = response.getBody().getObject().getLong("retry_after");
          api.setMessageTimeout(guild.getId(), retryAfter);
          throw new RateLimitedException(retryAfter);
        } else {
          throw new RuntimeException(
              "An unknown status code was returned when attempting to upload file. Status: "
                  + status
                  + " JSON: "
                  + response.getBody().toString());
        }

      } catch (JSONException e) {
        Requester.LOG.fatal("Following json caused an exception: " + response.getBody().toString());
        Requester.LOG.log(e);
      }
    } catch (UnirestException e) {
      Requester.LOG.log(e);
    }
    return null;
  }
Example #2
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;
    }
  }