Skip to content

Commit

Permalink
Switch API and Gateway version to v8
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshix-1 committed Dec 29, 2020
1 parent a27421c commit fe24091
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 16 deletions.
4 changes: 2 additions & 2 deletions javacord-api/src/main/java/org/javacord/api/Javacord.java
Expand Up @@ -110,7 +110,7 @@ public class Javacord {
* A list with all gateway versions can be found
* <a href="https://discord.com/developers/docs/topics/gateway#gateways-gateway-versions">here</a>.
*/
public static final String DISCORD_GATEWAY_VERSION = "6";
public static final String DISCORD_GATEWAY_VERSION = "8";

/**
* The voice gateway version from Discord which we are using.
Expand All @@ -124,7 +124,7 @@ public class Javacord {
* A list with all API versions can be found
* <a href="https://discord.com/developers/docs/reference#api-versioning-api-versions">here</a>.
*/
public static final String DISCORD_API_VERSION = "6";
public static final String DISCORD_API_VERSION = "8";

private Javacord() {
throw new UnsupportedOperationException();
Expand Down
Expand Up @@ -745,6 +745,37 @@ default URL getLink() throws AssertionError {
*/
MessageAuthor getAuthor();

/**
* Gets the id of the message referenced with a reply.
* Only present if this message is type {@code MessageType.REPLY}.
*
* @return The id of the referenced message.
*/
Optional<Long> getReferencedMessageId();

/**
* Gets the message referenced with a reply.
* Only present if this message is type {@code MessageType.REPLY},
* discord decided to send it and the message hasn't been deleted.
*
* @return The referenced message.
*/
Optional<Message> getReferencedMessage();

/**
* Requests the message referenced with a reply.
*
* <p>If the message is in the cache, the message is served from the cache.
*
* @return The referenced message.
*/
default Optional<CompletableFuture<Message>> requestReferencedMessage() {
return getReferencedMessageId().map(id ->
getReferencedMessage().map(CompletableFuture::completedFuture)
.orElseGet(() -> getApi().getMessageById(id, getChannel())));
}


/**
* Checks if the message is kept in cache forever.
*
Expand Down Expand Up @@ -1378,6 +1409,26 @@ default boolean canDelete(User user) {
return getServerTextChannel().map(channel -> channel.canManageMessages(user)).orElse(false);
}

/**
* Replies to this message with the given text.
*
* @param messageContent The text to reply with.
* @return The sent message.
*/
default CompletableFuture<Message> reply(String messageContent) {
return new MessageBuilder().replyTo(getId()).setContent(messageContent).send(getChannel());
}

/**
* Replies to this message with the given embed.
*
* @param embed The EmbedBuilder to reply with.
* @return The sent message.
*/
default CompletableFuture<Message> reply(EmbedBuilder embed) {
return new MessageBuilder().replyTo(getId()).setEmbed(embed).send(getChannel());
}

/**
* Checks if the user of the connected account can delete this message.
*
Expand Down
Expand Up @@ -440,6 +440,28 @@ public MessageBuilder setAllowedMentions(AllowedMentions allowedMentions) {
return this;
}

/**
* Sets the message to reply to.
*
* @param message The the message to reply to.
* @return The current instance in order to chain call methods.
*/
public MessageBuilder replyTo(Message message) {
delegate.replyTo(message.getId());
return this;
}

/**
* Sets the message to reply to.
*
* @param messageId The id of the message to reply to.
* @return The current instance in order to chain call methods.
*/
public MessageBuilder replyTo(long messageId) {
delegate.replyTo(messageId);
return this;
}

/**
* Sets the nonce of the message.
*
Expand Down
Expand Up @@ -21,6 +21,15 @@ public enum MessageType {
CHANNEL_ICON_CHANGE(5),
CHANNEL_PINNED_MESSAGE(6),
SERVER_MEMBER_JOIN(7),
USER_PREMIUM_SERVER_SUBSCRIPTION(8),
USER_PREMIUM_SERVER_SUBSCRIPTION_TIER_1(9),
USER_PREMIUM_SERVER_SUBSCRIPTION_TIER_2(10),
USER_PREMIUM_SERVER_SUBSCRIPTION_TIER_3(11),
CHANNEL_FOLLOW_ADD(12),
SERVER_DISCOVERY_DISQUALIFIED(14),
SERVER_DISCOVERY_REQUALIFIED(15),
REPLY(19),
APPLICATION_COMMAND(20),

/**
* An unknown message type.
Expand Down Expand Up @@ -66,6 +75,24 @@ public static MessageType byType(int type, boolean webhook) {
return CHANNEL_PINNED_MESSAGE;
case 7:
return SERVER_MEMBER_JOIN;
case 8:
return USER_PREMIUM_SERVER_SUBSCRIPTION;
case 9:
return USER_PREMIUM_SERVER_SUBSCRIPTION_TIER_1;
case 10:
return USER_PREMIUM_SERVER_SUBSCRIPTION_TIER_2;
case 11:
return USER_PREMIUM_SERVER_SUBSCRIPTION_TIER_3;
case 12:
return CHANNEL_FOLLOW_ADD;
case 14:
return SERVER_DISCOVERY_DISQUALIFIED;
case 15:
return SERVER_DISCOVERY_REQUALIFIED;
case 19:
return REPLY;
case 20:
return APPLICATION_COMMAND;
default:
return UNKNOWN;
}
Expand Down
Expand Up @@ -234,6 +234,13 @@ public interface MessageBuilderDelegate {
*/
void setAllowedMentions(AllowedMentions allowedMentions);

/**
* Sets the message to reply to.
*
* @param messageId The id of the message to reply to.
*/
void replyTo(long messageId);

/**
* Sets the nonce of the message.
*
Expand Down
Expand Up @@ -88,14 +88,14 @@ protected void prepareBody(ObjectNode body) {
for (Map.Entry<Long, Permissions> entry : overwrittenUserPermissions.entrySet()) {
permissionOverwrites.addObject()
.put("id", Long.toUnsignedString(entry.getKey()))
.put("type", "member")
.put("type", "1")
.put("allow", entry.getValue().getAllowedBitmask())
.put("deny", entry.getValue().getDeniedBitmask());
}
for (Map.Entry<Long, Permissions> entry : overwrittenRolePermissions.entrySet()) {
permissionOverwrites.addObject()
.put("id", Long.toUnsignedString(entry.getKey()))
.put("type", "role")
.put("type", "0")
.put("allow", entry.getValue().getAllowedBitmask())
.put("deny", entry.getValue().getDeniedBitmask());
}
Expand Down
Expand Up @@ -104,11 +104,11 @@ public ServerChannelImpl(DiscordApiImpl api, ServerImpl server, JsonNode data) {
int allow = permissionOverwrite.has("allow") ? permissionOverwrite.get("allow").asInt() : 0;
int deny = permissionOverwrite.has("deny") ? permissionOverwrite.get("deny").asInt() : 0;
Permissions permissions = new PermissionsImpl(allow, deny);
switch (permissionOverwrite.get("type").asText()) {
case "role":
switch (permissionOverwrite.get("type").asInt()) {
case 0:
overwrittenRolePermissions.put(id, permissions);
break;
case "member":
case 1:
overwrittenUserPermissions.put(id, permissions);
break;
default:
Expand Down
Expand Up @@ -131,7 +131,7 @@ protected boolean prepareUpdateBody(ObjectNode body) {
for (Map.Entry<Long, Permissions> entry : overwrittenUserPermissions.entrySet()) {
permissionOverwrites.addObject()
.put("id", Long.toUnsignedString(entry.getKey()))
.put("type", "member")
.put("type", 1)
.put("allow", entry.getValue().getAllowedBitmask())
.put("deny", entry.getValue().getDeniedBitmask());
}
Expand All @@ -140,7 +140,7 @@ protected boolean prepareUpdateBody(ObjectNode body) {
for (Map.Entry<Long, Permissions> entry : overwrittenRolePermissions.entrySet()) {
permissionOverwrites.addObject()
.put("id", Long.toUnsignedString(entry.getKey()))
.put("type", "role")
.put("type", 0)
.put("allow", entry.getValue().getAllowedBitmask())
.put("deny", entry.getValue().getDeniedBitmask());
}
Expand Down
Expand Up @@ -85,6 +85,11 @@ public class MessageBuilderDelegateImpl implements MessageBuilderDelegate {
*/
private AllowedMentions allowedMentions = null;

/**
* The message to reply to.
*/
private Long replyingTo = null;

@Override
public void appendCode(String language, String code) {
strBuilder
Expand Down Expand Up @@ -270,6 +275,11 @@ public void setAllowedMentions(AllowedMentions allowedMentions) {
this.allowedMentions = allowedMentions;
}

@Override
public void replyTo(long messageId) {
replyingTo = messageId;
}

@Override
public void setNonce(String nonce) {
this.nonce = nonce;
Expand Down Expand Up @@ -321,6 +331,10 @@ public CompletableFuture<Message> send(TextChannel channel) {
body.put("nonce", nonce);
}

if (replyingTo != null) {
body.putObject("message_reference").put("message_id", replyingTo);
}

RestRequest<Message> request = new RestRequest<Message>(channel.getApi(), RestMethod.POST, RestEndpoint.MESSAGE)
.setUrlParameters(channel.getIdAsString());
if (!attachments.isEmpty() || (embeds.size() > 0 && embeds.get(0).requiresAttachments())) {
Expand Down
Expand Up @@ -101,6 +101,15 @@ public class MessageImpl implements Message, InternalMessageAttachableListenerMa
*/
private final String nonce;

/**
* The id of the message referenced via message reply.
*/
private final Long referencedMessageId;
/**
* The message referenced via message reply.
*/
private final Message referencedMessage;

/**
* If the message should be cached forever or not.
*/
Expand Down Expand Up @@ -209,6 +218,18 @@ public MessageImpl(DiscordApiImpl api, TextChannel channel, JsonNode data) {
} else {
nonce = null;
}

if (data.has("message_reference")) {
referencedMessageId = data.get("message_reference").get("message_id").asLong();
} else {
referencedMessageId = null;
}

if (data.hasNonNull("referenced_message")) {
referencedMessage = api.getOrCreateMessage(channel, data.get("referenced_message"));
} else {
referencedMessage = null;
}
}

/**
Expand Down Expand Up @@ -385,6 +406,16 @@ public Optional<User> getUserAuthor() {
return author.asUser();
}

@Override
public Optional<Long> getReferencedMessageId() {
return Optional.ofNullable(referencedMessageId);
}

@Override
public Optional<Message> getReferencedMessage() {
return Optional.ofNullable(referencedMessage);
}

@Override
public boolean isCachedForever() {
return cacheForever;
Expand Down Expand Up @@ -482,5 +513,4 @@ public int hashCode() {
public String toString() {
return String.format("Message (id: %s, content: %s)", getIdAsString(), getContent());
}

}
Expand Up @@ -1367,7 +1367,7 @@ public CompletableFuture<Void> kickUser(User user, String reason) {
public CompletableFuture<Void> banUser(User user, int deleteMessageDays, String reason) {
RestRequest<Void> request = new RestRequest<Void>(getApi(), RestMethod.PUT, RestEndpoint.BAN)
.setUrlParameters(getIdAsString(), user.getIdAsString())
.addQueryParameter("delete-message-days", String.valueOf(deleteMessageDays));
.addQueryParameter("delete_message_days", String.valueOf(deleteMessageDays));
if (reason != null) {
request.addQueryParameter("reason", reason);
}
Expand Down
Expand Up @@ -164,17 +164,17 @@ private void handleServerChannel(JsonNode jsonChannel) {
Permissions oldOverwrittenPermissions;
ConcurrentHashMap<Long, Permissions> overwrittenPermissions;
long entityId = permissionOverwriteJson.get("id").asLong();
Optional<DiscordEntity> entity = Optional.empty();
switch (permissionOverwriteJson.get("type").asText()) {
case "role":
Optional<DiscordEntity> entity;
switch (permissionOverwriteJson.get("type").asInt()) {
case 0:
Role role = server.getRoleById(entityId).orElseThrow(() ->
new IllegalStateException("Received channel update event with unknown role!"));
entity = Optional.of(role);
oldOverwrittenPermissions = channel.getOverwrittenPermissions(role);
overwrittenPermissions = channel.getInternalOverwrittenRolePermissions();
rolesWithOverwrittenPermissions.add(entityId);
break;
case "member":
case 1:
oldOverwrittenPermissions = channel.getOverwrittenUserPermissions()
.getOrDefault(entityId, PermissionsImpl.EMPTY_PERMISSIONS);
entity = api.getCachedUserById(entityId).map(DiscordEntity.class::cast);
Expand Down
Expand Up @@ -178,7 +178,7 @@ private void handleResponse(
.getEndpoint()
.getHardcodedRatelimit()
.map(ratelimit -> responseTimestamp + api.getTimeOffset() + ratelimit)
.orElseGet(() -> Long.parseLong(response.header("X-RateLimit-Reset", "0")) * 1000);
.orElseGet(() -> (long) (Double.parseDouble(response.header("X-RateLimit-Reset", "0")) * 1000));

// Check if we received a 429 response
if (result.getResponse().code() == 429) {
Expand Down

0 comments on commit fe24091

Please sign in to comment.