Skip to content

Commit

Permalink
Merge branch 'discord-jar:main' into feature/webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
blueysh authored May 15, 2023
2 parents d03c6c8 + d7139d7 commit 6556ac8
Show file tree
Hide file tree
Showing 41 changed files with 1,243 additions and 484 deletions.
23 changes: 0 additions & 23 deletions .github/workflows/codesee-arch-diagram.yml

This file was deleted.

25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ account [here](https://discord.com/developers/applications).
To initialize a bot that uses the gateway (is able to receive events), you can use the following code:

```java
new DiscordJar("token");

DiscordJar djar = new DiscordJarBuilder("token").build();
```

You can specify intents to use with the gateway by using the following code:

```java
new DiscordJar("token", EnumSet.of(Intent.GUILDS, Intent.GUILD_MESSAGES));
DiscordJar djar = new DiscordJarBuilder("token")
.addIntents(Intent.GUILDS, Intent.GUILD_MESSAGES).build();
```

Doing so will override the default intents.

Note: You can use the `Intent.ALL` constant to specify all intents. This does not include privileged intents.

### Creating an HTTP-Only bot
Expand All @@ -57,11 +61,12 @@ To make your bot an <a href="https://discord.com/developers/docs/topics/gateway#
you'll need to specify a couple more parameters.

```java
new DiscordJar("token", true,
new HTTPOnlyInfo(
"interactions",
"EXAMPLE_APPLICATION_PUBLIC_KEY" // this cxan be found in your application's page in the dev panel
));
DiscordJar djar = new DiscordJarBuilder("token_here")
.setHTTPOnly(true)
.setHTTPOnlyInfo(new HTTPOnlyInfo(
"interactions",
"EXAMPLE_APPLICATION_PUBLIC_KEY"
)).build();
```

You should set `"interactions"` to whatever endpoint you want to use to receive post requests from Discord. This will be
Expand Down Expand Up @@ -187,11 +192,7 @@ License info can be found [here](https://github.com/discord-jar/discord.jar/blob
Our official Discord server:
https://discord.gg/tmvS8A57J4

## Donations
## Support me :)
<a href="https://github.com/sponsors/seailz">
<img alt="Ghsponsors Singular badge" height="56" href="https://github.com/seailz" src="https://cdn.jsdelivr.net/gh/intergrav/devins-badges/assets/cozy/donate/ghsponsors-singular_vector.svg">
</a>

<a href="https://ko-fi.com/discordjv" target="_blank">
<img alt="Kofi Singular badge" height="56" src="https://cdn.jsdelivr.net/gh/intergrav/devins-badges/assets/cozy/donate/kofi-singular_vector.svg">
</a>
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>3.0.5</version>
<version>3.0.6</version>
</dependency>
<!-- Used for marking methods and params -->
<dependency>
Expand All @@ -118,12 +118,12 @@
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.15.4</version>
<version>1.16.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>5.0.0-alpha.11</version>
<version>4.11.0</version>
</dependency>
</dependencies>
</project>
105 changes: 79 additions & 26 deletions src/main/java/com/seailz/discordjar/DiscordJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
import java.util.logging.Logger;

/**
* The main class of the discord.jar wrapper for the Discord API.
* The main class of the discord.jar wrapper for the Discord API. It is <b>HIGHLY</b> recommended that you use
* {@link DiscordJarBuilder} for creating new instances of this class as the other constructors are deprecated
* and will be set to protected/removed in the future.
*
* @author Seailz
* @since 1.0
Expand Down Expand Up @@ -113,23 +115,50 @@ public class DiscordJar {
* List of rate-limit buckets
*/
private List<Bucket> buckets;
/**
* The current status of the bot, or null if not set.
*/
private Status status;

public int gatewayConnections = 0;
public List<GatewayFactory> gatewayFactories = new ArrayList<>();

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version) throws ExecutionException, InterruptedException {
this(token, intents, version, false, null, false);
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version, boolean debug) throws ExecutionException, InterruptedException {
this(token, intents, version, false, null, debug);
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, APIVersion version) throws ExecutionException, InterruptedException {
this(token, EnumSet.of(Intent.ALL), version, false, null, false);
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, APIVersion version, boolean httpOnly, HTTPOnlyInfo httpOnlyInfo) throws ExecutionException, InterruptedException {
this(token, EnumSet.noneOf(Intent.class), version, httpOnly, httpOnlyInfo, false);
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, boolean httpOnly, HTTPOnlyInfo httpOnlyInfo) throws ExecutionException, InterruptedException {
this(token, EnumSet.noneOf(Intent.class), APIVersion.getLatest(), httpOnly, httpOnlyInfo, false);
}
Expand All @@ -153,7 +182,10 @@ public DiscordJar(String token, boolean httpOnly, HTTPOnlyInfo httpOnlyInfo) thr
* @param debug Should the bot be in debug mode?
* @throws ExecutionException If an error occurs while connecting to the gateway
* @throws InterruptedException If an error occurs while connecting to the gateway
*
* @deprecated Use {@link DiscordJarBuilder} instead. This constructor will be set to protected in the future.
*/
@Deprecated
public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version, boolean httpOnly, HTTPOnlyInfo httpOnlyInfo, boolean debug) throws ExecutionException, InterruptedException {
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
new RequestQueueHandler(this);
Expand All @@ -165,11 +197,7 @@ public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version, boo
this.queuedRequests = new ArrayList<>();
this.buckets = new ArrayList<>();
if (!httpOnly) {
try {
this.gatewayFactory = new GatewayFactory(this, debug);
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new RuntimeException(e);
}
this.gatewayFactory = new GatewayFactory(this, debug);
}
this.debug = debug;
this.guildCache = new Cache<>(this, Guild.class,
Expand Down Expand Up @@ -219,21 +247,33 @@ public DiscordJar(String token, EnumSet<Intent> intents, APIVersion version, boo
throw new RuntimeException(e);
}

if (gatewayFactory == null || !gatewayFactory.getSession().isOpen()) {
if (gatewayFactory == null || (gatewayFactory.getSession() != null && !gatewayFactory.getSession().isOpen())) {
restartGateway();
}
}
}).start();
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token) throws ExecutionException, InterruptedException {
this(token, EnumSet.of(Intent.ALL), APIVersion.getLatest());
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, boolean debug) throws ExecutionException, InterruptedException {
this(token, EnumSet.of(Intent.ALL), APIVersion.getLatest(), debug);
}

/**
* @deprecated Use {@link DiscordJarBuilder} instead.
*/
@Deprecated(forRemoval = true)
public DiscordJar(String token, EnumSet<Intent> intents) throws ExecutionException, InterruptedException {
this(token, intents, APIVersion.getLatest());
}
Expand Down Expand Up @@ -262,15 +302,13 @@ public GatewayFactory getGateway() {
* Kills the gateway connection and destroys the {@link GatewayFactory} instance.
* This method will also initiate garbage collection to avoid memory leaks. This probably shouldn't be used unless in {@link #restartGateway()}.
*/
public Status killGateway() {
Status status = gatewayFactory == null ? null : gatewayFactory.getStatus();
public void killGateway() {
try {
if (gatewayFactory != null) gatewayFactory.killConnection();
} catch (IOException ignored) {}
gatewayFactory = null;
// init garbage collection to avoid memory leaks
System.gc();
return status;
}

/**
Expand All @@ -282,13 +320,13 @@ public Status killGateway() {
* @see #killGateway()
*/
public void restartGateway() {
Status stat = killGateway();
killGateway();
try {
gatewayFactory = new GatewayFactory(this, debug);
} catch (ExecutionException | InterruptedException | DiscordRequest.UnhandledDiscordAPIErrorException e) {
gatewayFactories.add(gatewayFactory);
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
}
if (stat != null) setStatus(stat);
}

protected void initiateShutdownHooks() {
Expand All @@ -308,6 +346,10 @@ protected void initiateShutdownHooks() {
}));
}

public void setGatewayFactory(GatewayFactory gatewayFactory) {
this.gatewayFactory = gatewayFactory;
}

public List<Bucket> getBuckets() {
return buckets;
}
Expand Down Expand Up @@ -353,6 +395,11 @@ public void setStatus(@NotNull Status status) {
json.put("op", 3);
gatewayFactory.queueMessage(json);
gatewayFactory.setStatus(status);
this.status = status;
}

public Status getStatus() {
return status;
}

/**
Expand Down Expand Up @@ -682,19 +729,25 @@ public void registerCommands(CommandListener... listeners) throws DiscordRequest
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();
registerCommand(
new Command(
name,
listener.getType(),
description,
(listener instanceof SlashCommandListener) ? ((SlashCommandListener) listener).getOptions() : new ArrayList<>(),
nameLocales,
descriptionLocales,
defaultMemberPermissions,
canUseInDms,
nsfw
)
);
new Thread(() -> {
try {
registerCommand(
new Command(
name,
listener.getType(),
description,
(listener instanceof SlashCommandListener) ? ((SlashCommandListener) listener).getOptions() : new ArrayList<>(),
nameLocales,
descriptionLocales,
defaultMemberPermissions,
canUseInDms,
nsfw
)
);
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new RuntimeException(e);
}
}).start();
commandDispatcher.registerCommand(name, listener);

if (!(listener instanceof SlashCommandListener slashCommandListener)) continue ;
Expand Down
Loading

0 comments on commit 6556ac8

Please sign in to comment.