Skip to content

Commit

Permalink
Merge pull request #136 from discord-jar/feature/rolesub
Browse files Browse the repository at this point in the history
[FEATURE] Role Subscriptions
  • Loading branch information
seailz authored May 15, 2023
2 parents 6f38730 + c09089a commit f332443
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public enum GuildFeature {
BANNER,
// guild can enable welcome screen, Membership Screening, stage channels and discovery, and receives community updates
COMMUNITY,
// guild has enabled monetization
CREATOR_MONETIZABLE_PROVISIONAL,
// guild has enabled the role subscription promo page
CREATOR_STORE_PAGE,
// guild has been set as a support server on the App Directory
DEVELOPER_SUPPORT_SERVER,
// guild is able to be discovered in the directory
Expand All @@ -40,6 +44,10 @@ public enum GuildFeature {
PRIVATE_THREADS,
// guild is able to set role icons
ROLE_ICONS,
// guild has role subscriptions that can be purchased
ROLE_SUBSCRIPTIONS_AVAILABLE_FOR_PURCHASE,
// guild has enabled role subscriptions
ROLE_SUBSCRIPTIONS_ENABLED,
// guild has enabled ticketed events
TICKETED_EVENTS_ENABLED,
// guild has access to set a vanity URL
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/seailz/discordjar/model/invite/Invite.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ enum VoiceInviteTargetType {
STREAM(1),
EMBEDDED_APPLICATION(2), // This is for voice channel activities.

ROLE_SUBSCRIPTIONS_PURCHASE(3), // This is for role subscriptions. Bots can not create these invites.

UNKNOWN(-1);

private final int code;
Expand Down
68 changes: 67 additions & 1 deletion src/main/java/com/seailz/discordjar/model/message/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.seailz.discordjar.model.component.DisplayComponent;
import com.seailz.discordjar.model.embed.Embed;
import com.seailz.discordjar.model.emoji.Reaction;
import com.seailz.discordjar.model.emoji.sticker.StickerFormat;
import com.seailz.discordjar.model.interaction.Interaction;
import com.seailz.discordjar.model.message.activity.MessageActivity;
import com.seailz.discordjar.model.resolve.Resolvable;
Expand Down Expand Up @@ -88,6 +89,10 @@ public record Message(
Thread thread,
// sent if the message contains components like buttons, action rows, or other interactive components
List<DisplayComponent> components,
List<StickerItem> stickerItems,
int position,
// data of the role subscription purchase or renewal that prompted this ROLE_SUBSCRIPTION_PURCHASE message
RoleSubscriptionData roleSubscriptionData,
DiscordJar discordJar
) implements Compilerable, Resolvable, Snowflake {

Expand Down Expand Up @@ -120,6 +125,9 @@ public static Message decompile(JSONObject obj, DiscordJar discordJar) {
Interaction interaction;
Thread thread;
List<DisplayComponent> components = new ArrayList<>();
List<StickerItem> stickerItems = new ArrayList<>();
int position = 0;
RoleSubscriptionData roleSubscriptionData = null;

try {
id = obj.getString("id");
Expand Down Expand Up @@ -317,7 +325,17 @@ public static Message decompile(JSONObject obj, DiscordJar discordJar) {
throw new RuntimeException(e);
}

return new Message(id, channelId, author, content, timestamp, editedTimestamp, tts, mentionEveryone, mentions, mentionRoles, mentionChannels, attachments, embeds, reactions, nonce, pinned, webhookId, type, activity, application, applicationId, messageReference, flags, referencedMessage, interaction, thread, components, discordJar);
if (obj.has("sticker_items") && !obj.isNull("sticker_items")) {
JSONArray stickerItemsArray = obj.getJSONArray("sticker_items");
stickerItemsArray.forEach(stickerItem -> {
stickerItems.add(StickerItem.decompile((JSONObject) stickerItem));
});
}

if (obj.has("position")) position = obj.getInt("position");
if (obj.has("role_subscription_data")) roleSubscriptionData = RoleSubscriptionData.decompile(obj.getJSONObject("role_subscription_data"));

return new Message(id, channelId, author, content, timestamp, editedTimestamp, tts, mentionEveryone, mentions, mentionRoles, mentionChannels, attachments, embeds, reactions, nonce, pinned, webhookId, type, activity, application, applicationId, messageReference, flags, referencedMessage, interaction, thread, components, stickerItems, position, roleSubscriptionData, discordJar);
}

@Override
Expand Down Expand Up @@ -438,6 +456,54 @@ public String getFormattedText() {
return formatted;
}

public record StickerItem(
String id,
String name,
StickerFormat format
) implements Compilerable {

@Override
public JSONObject compile() {
return new JSONObject()
.put("id", id)
.put("name", name)
.put("format_type", format.getCode());
}

public static StickerItem decompile(JSONObject obj) {
return new StickerItem(
obj.getString("id"),
obj.getString("name"),
StickerFormat.getStickerFormatByCode(obj.getInt("format_type"))
);
}
}

public record RoleSubscriptionData(
String roleSubscriptionListingId,
String tierName,
int totalMonthsSubscribed,
boolean isRenewal
) implements Compilerable {

@Override
public JSONObject compile() {
return new JSONObject()
.put("role_subscription_listing_id", roleSubscriptionListingId)
.put("tier_name", tierName)
.put("total_months_subscribed", totalMonthsSubscribed)
.put("is_renewal", isRenewal);
}

public static RoleSubscriptionData decompile(JSONObject obj) {
return new RoleSubscriptionData(
obj.getString("role_subscription_listing_id"),
obj.getString("tier_name"),
obj.getInt("total_months_subscribed"),
obj.getBoolean("is_renewal")
);
}
}
/**
* Pins the message within the channel.
* Note that the maximum pinned messages per channel is 50.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum MessageType {
STAGE_RAISE_HAND(30, true),
STAGE_TOPIC_CHANGE(31, true),
GUILD_APPLICATION_PREMIUM_SUBSCRIPTION(32, false),
ROLE_SUBSCRIPTION_PURCHASE(25,true)

;

Expand Down
41 changes: 18 additions & 23 deletions src/main/java/com/seailz/discordjar/model/role/RoleTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
public record RoleTag(
String botId,
String integrationId,
boolean isPremiumSubscriber
boolean isPremiumSubscriber,
String subscriptionListingId, // the id of this role's subscription sku and listing
boolean availableForPurchase, // whether this role is available for purchase
boolean guildConnections // whether this role is a guild's linked role
) implements Compilerable {

@Override
Expand All @@ -20,29 +23,21 @@ public JSONObject compile() {

@NonNull
public static RoleTag decompile(JSONObject obj) {
String botId;
String integrationId;
String botId = null;
String integrationId = null;
boolean isPremiumSubscriber;

try {
botId = obj.getString("bot_id");
} catch (Exception e) {
botId = null;
}

try {
integrationId = obj.getString("integration_id");
} catch (Exception e) {
integrationId = null;
}

try {
isPremiumSubscriber = obj.getBoolean("is_premium_subscriber");
} catch (Exception e) {
isPremiumSubscriber = false;
}

return new RoleTag(botId, integrationId, isPremiumSubscriber);
String subscriptionListingId = null;
boolean availableForPurchase = false;
boolean guildConnections = false;

if (obj.has("bot_id") && !obj.isNull("bot_id")) botId = obj.getString("bot_id");
if (obj.has("integration_id") && !obj.isNull("integration_id")) integrationId = obj.getString("integration_id");
if (obj.has("subscription_listing_id") && !obj.isNull("subscription_listing_id")) subscriptionListingId = obj.getString("subscription_listing_id");
isPremiumSubscriber = obj.has("premium_subscriber");
availableForPurchase = obj.has("available_for_purchase");
guildConnections = obj.has("guild_connections");

return new RoleTag(botId, integrationId, isPremiumSubscriber, subscriptionListingId, availableForPurchase, guildConnections);
}

}

0 comments on commit f332443

Please sign in to comment.