Skip to content

Commit

Permalink
Merge pull request #250 from discord-jar/main
Browse files Browse the repository at this point in the history
main
  • Loading branch information
seailz authored Oct 26, 2023
2 parents e0df2fe + 1b5cd00 commit e429c46
Show file tree
Hide file tree
Showing 19 changed files with 803 additions and 33 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@ jobs:
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{matrix.language}}"
category: "/language:${{matrix.language}}"


25 changes: 25 additions & 0 deletions .github/workflows/qodana.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Qodana
on:
workflow_dispatch:
pull_request:
push:
branches:
- main
- 'releases/*'

jobs:
qodana:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
checks: write
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }} # to check out the actual pull request commit, not the merge commit
fetch-depth: 0 # a full history is required for pull request analysis
- name: 'Qodana Scan'
uses: JetBrains/qodana-action@v2023.2
env:
QODANA_TOKEN: ${{ secrets.QODANA_TOKEN }}
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230618</version>
<version>20231013</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand All @@ -105,7 +105,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>3.1.4</version>
<version>3.1.5</version>
</dependency>
<!-- Used for marking methods and params -->
<dependency>
Expand All @@ -129,12 +129,12 @@
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.16.1</version>
<version>1.16.2</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.11.0</version>
<version>4.12.0</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/com/seailz/discordjar/DiscordJar.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.seailz.discordjar;

import com.seailz.discordjar.action.guild.GetCurrentUserGuildsAction;
import com.seailz.discordjar.action.sku.ListEntitlementRequest;
import com.seailz.discordjar.cache.Cache;
import com.seailz.discordjar.cache.CacheType;
import com.seailz.discordjar.cache.JsonCache;
Expand Down Expand Up @@ -34,6 +35,7 @@
import com.seailz.discordjar.model.guild.Member;
import com.seailz.discordjar.model.invite.Invite;
import com.seailz.discordjar.model.invite.internal.InviteImpl;
import com.seailz.discordjar.model.monetization.SKU;
import com.seailz.discordjar.model.status.Status;
import com.seailz.discordjar.model.user.User;
import com.seailz.discordjar.utils.Checker;
Expand Down Expand Up @@ -304,7 +306,6 @@ public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version, boo
.setTransportCompressionType(gwCompressionType)
.build();
}

}

/**
Expand Down Expand Up @@ -1507,6 +1508,36 @@ public void deleteInvite(String code) {
}
}

public List<SKU> getApplicationSkus() {
DiscordRequest req = new DiscordRequest(
new JSONObject(),
new HashMap<>(),
URLS.GET.APPLICATION.GET_APPLICATION_SKUS.replace("{application.id}", getSelfInfo().id()),
this,
URLS.GET.APPLICATION.GET_APPLICATION_SKUS,
RequestMethod.GET
);
JSONArray res = null;
try {
res = req.invoke().arr();
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new DiscordRequest.DiscordAPIErrorException(e);
}
List<SKU> skus = new ArrayList<>();
for (int i = 0; i < res.length(); i++) {
skus.add(SKU.decompile(res.getJSONObject(i), this));
}
return skus;
}

public SKU getSKUById(String id) {
return getApplicationSkus().stream().filter(s -> s.id().equals(id)).findFirst().orElse(null);
}

public ListEntitlementRequest getEntitlements() {
return new ListEntitlementRequest(this);
}

public boolean isDebug() {
return debug;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.seailz.discordjar.utils.rest.Response;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.File;
import java.util.HashMap;
import java.util.List;

Expand Down Expand Up @@ -129,6 +130,11 @@ public InteractionFollowupAction setAttachments(List<Attachment> attachments) {
return this;
}

public InteractionFollowupAction addFiles(File... files) {
this.getReply().addFiles(files);
return this;
}

public InteractionFollowupAction setAllowedMentions(AllowedMentions allowedMentions) {
this.getReply().setAllowedMentions(allowedMentions);
return this;
Expand All @@ -146,7 +152,7 @@ public InteractionMessageResponse getReply() {
public Response<InteractionHandler> run() {
Response<InteractionHandler> response = new Response<>();
try {
new DiscordRequest(
DiscordRequest req = new DiscordRequest(
getReply().compile(),
new HashMap<>(),
URLS.POST.INTERACTIONS.FOLLOWUP
Expand All @@ -155,7 +161,11 @@ public Response<InteractionHandler> run() {
discordJar,
URLS.POST.INTERACTIONS.FOLLOWUP,
RequestMethod.POST
).invoke();
);

if (getReply().useFiles()) req.invokeWithFiles(getReply().getFiles().toArray(new File[0]));
else req.invoke();

response.complete(InteractionHandler.from(token, id, discordJar));
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
response.completeError(new Response.Error(e.getCode(), e.getMessage(), e.getBody()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package com.seailz.discordjar.action.sku;

import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.model.monetization.Entitlement;
import com.seailz.discordjar.utils.URLS;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import com.seailz.discordjar.utils.rest.DiscordResponse;
import com.seailz.discordjar.utils.rest.Response;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ListEntitlementRequest {

private final DiscordJar jar;
private String userId;
private List<String> skuIds;
private String before;
private String after;
private int limit = 100;
private String guildId;
private boolean excludeEnded = false;

public ListEntitlementRequest(DiscordJar jar) {
this.jar = jar;
}

/**
* Sets the user ID to get entitlements for.
*/
public ListEntitlementRequest setUserId(String userId) {
this.userId = userId;
return this;
}

/**
* Sets the SKU IDs to get entitlements for.
*/
public ListEntitlementRequest setSkuIds(List<String> skuIds) {
this.skuIds = skuIds;
return this;
}

public ListEntitlementRequest addSkuId(String skuId) {
if (this.skuIds == null) this.skuIds = new ArrayList<>();
this.skuIds.add(skuId);
return this;
}

/**
* Limits entitlements to before this ID.
*/
public ListEntitlementRequest setBefore(String before) {
this.before = before;
return this;
}

/**
* Limits entitlements to after this ID.
*/
public ListEntitlementRequest setAfter(String after) {
this.after = after;
return this;
}

/**
* Limits entitlements to this amount. 1-100
*/
public ListEntitlementRequest setLimit(int limit) {
this.limit = limit;
return this;
}

/**
* Limits entitlements to this guild ID.
*/
public ListEntitlementRequest setGuildId(String guildId) {
this.guildId = guildId;
return this;
}

/**
* Whether or not to exclude ended entitlements.
*/
public ListEntitlementRequest setExcludeEnded(boolean excludeEnded) {
this.excludeEnded = excludeEnded;
return this;
}

public Response<List<Entitlement>> run() {
Response<List<Entitlement>> response = new Response<>();
if (limit < 1 || limit > 100) throw new IllegalArgumentException("Limit must be between 1 and 100");
new Thread(() -> {
String urlWithQuery = URLS.GET.APPLICATION.LIST_ENTITLEMENTS;
urlWithQuery += "?";
if (userId != null) urlWithQuery += "user_id=" + userId + "&";
if (skuIds != null) {
StringBuilder skuIdsString = new StringBuilder();
for (String skuId : skuIds) {
skuIdsString.append(skuId).append(",");
}
skuIdsString = new StringBuilder(skuIdsString.substring(0, skuIdsString.length() - 1));
urlWithQuery += "sku_ids=" + skuIdsString + "&";
}
if (before != null) urlWithQuery += "before=" + before + "&";
if (after != null) urlWithQuery += "after=" + after + "&";
urlWithQuery += "limit=" + limit + "&";
if (guildId != null) urlWithQuery += "guild_id=" + guildId + "&";
urlWithQuery += "exclude_ended=" + excludeEnded;

DiscordResponse req = null;
try {
req = new DiscordRequest(
new JSONObject(),
new HashMap<>(),
urlWithQuery.replace("{application.id}", jar.getSelfInfo().id()),
jar,
URLS.GET.APPLICATION.LIST_ENTITLEMENTS,
RequestMethod.GET
).invoke();
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
response.completeError(new Response.Error(e));
return;
}

List<Entitlement> entitlements = new ArrayList<>();
for (Object o : req.arr()) {
entitlements.add(Entitlement.decompile(jar, (JSONObject) o));
}
response.complete(entitlements);
}).start();
return response;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.seailz.discordjar.model.component.select;

import java.util.List;

/**
* Represents a select menu that is auto populated, such as a {@link com.seailz.discordjar.model.component.select.entity.ChannelSelectMenu ChannelSelectMenu}.
* @author Seailz
* @see AutoPopulatedSelect
*/
public interface AutoPopulatedSelect extends SelectMenu {

List<String> defaultValues();

}
Loading

0 comments on commit e429c46

Please sign in to comment.