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] User Apps #290

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions src/main/java/com/seailz/discordjar/DiscordJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.seailz.discordjar.model.emoji.sticker.StickerPack;
import com.seailz.discordjar.model.guild.Guild;
import com.seailz.discordjar.model.guild.Member;
import com.seailz.discordjar.model.interaction.InteractionContextType;
import com.seailz.discordjar.model.invite.Invite;
import com.seailz.discordjar.model.invite.internal.InviteImpl;
import com.seailz.discordjar.model.monetization.SKU;
Expand Down Expand Up @@ -1067,6 +1068,7 @@ public void registerCommands(boolean push, boolean overwrite, CommandListener...
Permission[] defaultMemberPermissions = (ann instanceof SlashCommandInfo) ? ((SlashCommandInfo) ann).defaultMemberPermissions() : ((ContextCommandInfo) ann).defaultMemberPermissions();
boolean canUseInDms = (ann instanceof SlashCommandInfo) ? ((SlashCommandInfo) ann).canUseInDms() : ((ContextCommandInfo) ann).canUseInDms();
boolean nsfw = (ann instanceof SlashCommandInfo) ? ((SlashCommandInfo) ann).nsfw() : ((ContextCommandInfo) ann).nsfw();
InteractionContextType[] contextTypes = (ann instanceof ContextCommandInfo) ? ((ContextCommandInfo) ann).contexts() : ((SlashCommandInfo) ann).contexts();
if (overwrite) list.add(new Command(
name,
listener.getType(),
Expand All @@ -1076,7 +1078,8 @@ public void registerCommands(boolean push, boolean overwrite, CommandListener...
descriptionLocales,
defaultMemberPermissions,
canUseInDms,
nsfw
nsfw,
Arrays.stream(contextTypes).toList()
));
else {
new Thread(() -> {
Expand All @@ -1090,7 +1093,8 @@ public void registerCommands(boolean push, boolean overwrite, CommandListener...
descriptionLocales,
defaultMemberPermissions,
canUseInDms,
nsfw
nsfw,
Arrays.stream(contextTypes).toList()
)
);
}, "djar--command-register").start();
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/seailz/discordjar/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.seailz.discordjar.command.annotation.Locale;
import com.seailz.discordjar.core.Compilerable;
import com.seailz.discordjar.model.interaction.InteractionContextType;
import com.seailz.discordjar.utils.flag.BitwiseUtil;
import com.seailz.discordjar.utils.permission.Permission;
import org.json.JSONArray;
Expand Down Expand Up @@ -30,8 +31,10 @@ public record Command(
Locale[] nameLocalizations,
Locale[] descriptionLocalizations,
Permission[] defaultMemberPermissions,
@Deprecated
boolean canUseInDms,
boolean nsfw
boolean nsfw,
List<InteractionContextType> contexts
) implements Compilerable {
@Override
public JSONObject compile() {
Expand Down Expand Up @@ -75,6 +78,10 @@ public JSONObject compile() {
obj.put("dm_permission", canUseInDms);
if (nsfw) obj.put("nsfw", true);

JSONArray contextsJson = new JSONArray();
contexts.forEach((context) -> contextsJson.put(context.getCode()));
obj.put("contexts", contextsJson);

return obj;
}

Expand All @@ -88,6 +95,7 @@ public static Command decompile(JSONObject obj) {
Permission[] defaultMemberPermissions = new Permission[0];
boolean canUseInDms = true;
boolean nsfw = false;
List<InteractionContextType> contexts = new ArrayList<>();

if (obj.has("name_localizations") && !obj.isNull("name_localizations")) {
JSONObject nameLocalesJson = obj.getJSONObject("name_localizations");
Expand Down Expand Up @@ -124,6 +132,12 @@ public static Command decompile(JSONObject obj) {
}
}

if (obj.has("contexts") && obj.get("contexts") != JSONObject.NULL) {
for (Object v : obj.getJSONArray("contexts")) {
contexts.add(InteractionContextType.fromCode((int) v));
}
}

return new Command(
name,
type,
Expand All @@ -133,7 +147,8 @@ public static Command decompile(JSONObject obj) {
descriptionLocales.entrySet().stream().map((entry) -> newLocale(entry.getKey(), entry.getValue())).toArray(Locale[]::new),
defaultMemberPermissions,
canUseInDms,
nsfw
nsfw,
contexts
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.seailz.discordjar.command.annotation;

import com.seailz.discordjar.model.interaction.InteractionContextType;
import com.seailz.discordjar.utils.permission.Permission;

import java.lang.annotation.ElementType;
Expand Down Expand Up @@ -29,7 +30,16 @@

Permission[] defaultMemberPermissions() default {};

/**
* @deprecated Use {@link #contexts()} instead.
*/
@Deprecated
boolean canUseInDms() default true;

boolean nsfw() default false;

/**
* Contexts where this command can be used. Default is all contexts.
*/
InteractionContextType[] contexts() default {InteractionContextType.BOT_DM, InteractionContextType.GUILD, InteractionContextType.PRIVATE_CHANNEL};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.seailz.discordjar.command.annotation;

import com.seailz.discordjar.command.listeners.slash.SlashCommandListener;
import com.seailz.discordjar.model.interaction.InteractionContextType;
import com.seailz.discordjar.utils.permission.Permission;

import java.lang.annotation.ElementType;
Expand Down Expand Up @@ -37,9 +38,18 @@

Permission[] defaultMemberPermissions() default {};

/**
* @deprecated Use {@link #contexts()} instead.
*/
@Deprecated
boolean canUseInDms() default true;

boolean nsfw() default false;

/**
* Contexts where this command can be used. Default is all contexts.
*/
InteractionContextType[] contexts() default {InteractionContextType.BOT_DM, InteractionContextType.GUILD, InteractionContextType.PRIVATE_CHANNEL};


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.core.Compilerable;
import com.seailz.discordjar.model.guild.Guild;
import com.seailz.discordjar.model.interaction.IntegrationType;
import com.seailz.discordjar.model.scopes.InstallParams;
import com.seailz.discordjar.model.team.Team;
import com.seailz.discordjar.model.user.User;
Expand Down Expand Up @@ -78,15 +79,15 @@ public record Application(
InstallParams installParams,
String roleConnectionsVerificationUrl,
int approximateGuildCount,
HashMap<IntegrationTypes, IntegrationTypeConfiguration> integrationTypes,
HashMap<IntegrationType, IntegrationTypeConfiguration> integrationTypes,
DiscordJar discordJar
) implements Compilerable, Snowflake {

@Override
public JSONObject compile() {
JSONObject integrationTypes = new JSONObject();
this.integrationTypes.forEach((key, val) -> {
integrationTypes.put(String.valueOf(key.code), val.compile());
integrationTypes.put(String.valueOf(key.getCode()), val.compile());
});
return new JSONObject()
.put("id", id)
Expand Down Expand Up @@ -142,7 +143,7 @@ public static Application decompile(JSONObject obj, DiscordJar discordJar) {
InstallParams installParams;
String roleConnectionsVerificationUrl;
int approximateGuildCount;
HashMap<IntegrationTypes, IntegrationTypeConfiguration> integrationTypesConfiguration = null;
HashMap<IntegrationType, IntegrationTypeConfiguration> integrationTypesConfiguration = null;

try {
id = obj.getString("id");
Expand Down Expand Up @@ -293,7 +294,7 @@ public static Application decompile(JSONObject obj, DiscordJar discordJar) {
integrationTypesConfiguration = new HashMap<>();
JSONObject integrationTypesConfig = obj.getJSONObject("integration_types_config");
for (String code : integrationTypesConfig.keySet()) {
integrationTypesConfiguration.put(IntegrationTypes.getByCode(Integer.parseInt(code)), IntegrationTypeConfiguration.decompile(integrationTypesConfig.getJSONObject(code)));
integrationTypesConfiguration.put(IntegrationType.fromCode(Integer.parseInt(code)), IntegrationTypeConfiguration.decompile(integrationTypesConfig.getJSONObject(code)));
}
}

Expand Down Expand Up @@ -442,32 +443,6 @@ public int id() {
}
}


public enum IntegrationTypes {

GUILD_INSTALL(0),
USER_INSTALL(1),
UNKNOWN(-1)
;

private final int code;

IntegrationTypes(int code) {
this.code = code;
}

public int getCode() {
return code;
}

public static IntegrationTypes getByCode(int code) {
for (IntegrationTypes value : values()) {
if (value.getCode() == code) return value;
}
return UNKNOWN;
}
}

public record IntegrationTypeConfiguration(
InstallParams installParams
) implements Compilerable {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.seailz.discordjar.model.interaction;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum IntegrationType {

GUILD_INSTALL(0),
USER_INSTALL(1),
UNKNOWN(-1);

private final int code;

public static IntegrationType fromCode(int code) {
for (IntegrationType type : values()) {
if (type.code == code) {
return type;
}
}
return UNKNOWN;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;

/**
Expand Down Expand Up @@ -67,8 +68,10 @@ public class Interaction implements Compilerable {
private final DiscordJar djar;
@Deprecated
private final String channelId;
private final InteractionContextType context;
private final HashMap<IntegrationType, String> authorizingIntegrationOwners;

public Interaction(String id, Application application, InteractionType type, InteractionData data, String guild, Channel channel, JSONObject member, User user, String token, int version, Message message, String appPermissions, String locale, String guildLocale, List<Entitlement> entitlements, String raw, String channelId, DiscordJar discordJar) {
public Interaction(String id, Application application, InteractionType type, InteractionData data, String guild, Channel channel, JSONObject member, User user, String token, int version, Message message, String appPermissions, String locale, String guildLocale, List<Entitlement> entitlements, String raw, String channelId, InteractionContextType context, HashMap<IntegrationType, String> authorizingIntegrationOwners, DiscordJar discordJar) {
this.id = id;
this.application = application;
this.type = type;
Expand All @@ -92,6 +95,8 @@ public Interaction(String id, Application application, InteractionType type, Int
this.channelId = channelId;
this.djar = discordJar;
this.entitlements = entitlements;
this.context = context;
this.authorizingIntegrationOwners = authorizingIntegrationOwners;
}

@Deprecated
Expand Down Expand Up @@ -169,6 +174,13 @@ public String raw() {
public List<Entitlement> entitlements() {
return entitlements;
}
public InteractionContextType context() {
return context;
}

public HashMap<IntegrationType, String> authorizingIntegrationOwners() {
return authorizingIntegrationOwners;
}

@Override
public JSONObject compile() {
Expand Down Expand Up @@ -196,6 +208,11 @@ public JSONObject compile() {
}
}

JSONObject authIntOwners = new JSONObject();
this.authorizingIntegrationOwners.forEach((key, val) -> {
authIntOwners.put(String.valueOf(key.getCode()), val);
});

return new JSONObject()
.put("id", id)
.put("application", application.id())
Expand All @@ -211,7 +228,9 @@ public JSONObject compile() {
.put("locale", locale)
.put("guild_locale", guildLocale)
.put("channel_id", channelId)
.put("entitlements", entitlements);
.put("entitlements", entitlements)
.put("context", context.getCode())
.put("authorizing_integration_owners", authIntOwners);
}

@NotNull
Expand All @@ -231,6 +250,7 @@ public static Interaction decompile(JSONObject json, DiscordJar discordJar) thro
String locale = json.has("locale") ? json.getString("locale") : null;
String guildLocale = json.has("guild_locale") ? json.getString("guild_locale") : null;
String channelId = json.has("channel_id") ? json.getString("channel_id") : null;
InteractionContextType context = json.has("context") ? InteractionContextType.fromCode(json.getInt("context")) : null;

JSONArray entitlements = json.has("entitlements") ? json.getJSONArray("entitlements") : null;
List<Entitlement> entitlementList = new ArrayList<>();
Expand All @@ -240,7 +260,17 @@ public static Interaction decompile(JSONObject json, DiscordJar discordJar) thro
}
}

return new Interaction(id, application, type, data, guildId, channel, json.has("member") ? json.getJSONObject("member") : null, user, token, version, message, appPermissions, locale, guildLocale, entitlementList, json.toString(), channelId, discordJar);
HashMap<IntegrationType, String> authIntegrationOwners = null;

if (json.has("authorizing_integration_owners")) {
authIntegrationOwners = new HashMap<>();
JSONObject authIntegrationOwnersJson = json.getJSONObject("authorizing_integration_owners");
for (String code : authIntegrationOwnersJson.keySet()) {
authIntegrationOwners.put(IntegrationType.fromCode(Integer.parseInt(code)), authIntegrationOwnersJson.getString(code));
}
}

return new Interaction(id, application, type, data, guildId, channel, json.has("member") ? json.getJSONObject("member") : null, user, token, version, message, appPermissions, locale, guildLocale, entitlementList, json.toString(), channelId, context, authIntegrationOwners, discordJar);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.seailz.discordjar.model.interaction;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum InteractionContextType {

GUILD(0),
BOT_DM(1),
PRIVATE_CHANNEL(2),
UNKNOWN(-1);

private final int code;

public static InteractionContextType fromCode(int code) {
for (InteractionContextType type : values()) {
if (type.code == code) {
return type;
}
}
return UNKNOWN;
}

}
Loading
Loading