Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Role Subscriptions #136

Merged
merged 5 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
69 changes: 68 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 @@ -9,6 +9,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 @@ -83,6 +84,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 @@ -115,6 +120,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 @@ -312,7 +320,17 @@ public static Message decompile(JSONObject obj, DiscordJar discordJar) {
thread = null;
}

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 @@ -420,5 +438,54 @@ public String getFormattedText() {
formatted = formatted.replaceAll("<@&" + role.id() + ">", "@" + role.name());
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 totalMonthsSubbed,
seailz marked this conversation as resolved.
Show resolved Hide resolved
boolean isRenewal
) implements Compilerable {

@Override
public JSONObject compile() {
return new JSONObject()
.put("role_subscription_listing_id", roleSubscriptionListingId)
.put("tier_name", tierName)
.put("total_months_subbed", totalMonthsSubbed)
seailz marked this conversation as resolved.
Show resolved Hide resolved
seailz marked this conversation as resolved.
Show resolved Hide resolved
.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_subbed"),
seailz marked this conversation as resolved.
Show resolved Hide resolved
seailz marked this conversation as resolved.
Show resolved Hide resolved
obj.getBoolean("is_renewal")
);
}
}
}

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);
}

}