Skip to content

Commit

Permalink
Migrated from discord4j to JDA & Added message edited IP updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ixf1nity committed Dec 29, 2023
1 parent 76f394f commit 318398b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 28 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.infinity</groupId>
<artifactId>ngrokcom</artifactId>
<version>1.2-SNAPSHOT</version>
<version>1.3-SNAPSHOT</version>
<packaging>jar</packaging>

<name>NgrokCommunication</name>
Expand Down Expand Up @@ -76,9 +76,9 @@
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>com.discord4j</groupId>
<artifactId>discord4j-core</artifactId>
<version>3.2.6</version>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>5.0.0-beta.18</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
79 changes: 56 additions & 23 deletions src/main/java/me/infinity/ngrokcom/NgrokCommunication.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,30 @@
import com.github.alexdlaird.ngrok.protocol.Proto;
import com.github.alexdlaird.ngrok.protocol.Region;
import com.github.alexdlaird.ngrok.protocol.Tunnel;

import discord4j.common.util.Snowflake;
import discord4j.core.DiscordClientBuilder;
import discord4j.core.GatewayDiscordClient;
import discord4j.core.object.entity.channel.GuildMessageChannel;
import discord4j.core.spec.MessageCreateSpec;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.api.events.session.ReadyEvent;
import net.dv8tion.jda.api.hooks.EventListener;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import net.dv8tion.jda.api.utils.messages.MessageEditData;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class NgrokCommunication extends JavaPlugin {
public final class NgrokCommunication extends JavaPlugin implements EventListener {

private GatewayDiscordClient client;
private JDA client;
private NgrokClient ngrokClient;
private String publicIp;

Expand All @@ -46,12 +55,18 @@ public void onEnable() {
return;
}

this.client = DiscordClientBuilder.create(botToken)
.build()
.login()
.block();
this.client = JDABuilder.createDefault(botToken)
.setStatus(OnlineStatus.DO_NOT_DISTURB)
.enableIntents(Arrays.asList(GatewayIntent.DIRECT_MESSAGES, GatewayIntent.MESSAGE_CONTENT, GatewayIntent.GUILD_MESSAGES))
.build();

this.client.addEventListener(this);

if (this.client != null) this.discordModuleStatus = true;
try {
this.client.awaitReady();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

final JavaNgrokConfig javaNgrokConfig = new JavaNgrokConfig.Builder()
Expand All @@ -77,17 +92,28 @@ public void onEnable() {
if (discordModuleStatus) {
String updateMessage = this.getConfig().getString("DISCORD_UPDATES.UPDATE_MESSAGE");
if (updateMessage != null && !updateMessage.isEmpty()) {
client.getChannelById(Snowflake.of(Objects.requireNonNull(this.getConfig().getString("DISCORD_UPDATES.UPDATE_CHANNEL_ID"))))
.ofType(GuildMessageChannel.class)
.flatMap(guildMessageChannel -> guildMessageChannel.createMessage(MessageCreateSpec.builder()
.content(updateMessage.replace("%server_ip%", publicIp))
.build()
)).subscribe();
TextChannel messageChannel = client.getTextChannelById(Objects.requireNonNull(this.getConfig().getString("DISCORD_UPDATES.UPDATE_CHANNEL_ID")));
if (messageChannel != null) {
long updateMessageId = this.getConfig().getLong("DISCORD_UPDATES.UPDATE_MESSAGE_ID");
if (updateMessageId == 0) {
CompletableFuture<Message> message = messageChannel.sendMessage(MessageCreateData.fromContent(updateMessage.replace("%server_ip%", publicIp))).submit();
try {
this.getConfig().set("DISCORD_UPDATES.UPDATE_MESSAGE_ID", message.get().getIdLong());
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
} else {
messageChannel.editMessageById(updateMessageId, MessageEditData.fromContent(updateMessage.replace("%server_ip%", publicIp))).queue();
}
} else {
this.getLogger().warning("IP update channel is null. Update message not sent.");
}
} else {
this.getLogger().warning("IP update message is missing in the config. Update message not sent.");
}
}


this.getLogger().info("Listening server on port " + ngrokPort + ", IP: " + publicIp);
}

Expand All @@ -99,10 +125,17 @@ public void onDisable() {
this.ngrokClient.kill();
}
if (discordModuleStatus) {
if (this.client != null) {
this.client.logout().block();
}
this.client.shutdown();
}
} catch (Exception ignored) {}
this.saveConfig();
} catch (Exception ignored) {
}
}

@Override
public void onEvent(@NotNull GenericEvent genericEvent) {
if (genericEvent instanceof ReadyEvent) {
this.discordModuleStatus = true;
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ DISCORD_UPDATES:
BOT_TOKEN: ""
UPDATE_CHANNEL_ID: ""
UPDATE_MESSAGE: "**Updated Server IP** : %server_ip%"
# DO NOT TOUCH, THIS IS MODIFIED BY THE PLUGIN.
UPDATE_MESSAGE_ID: 0
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ main: me.infinity.ngrokcom.NgrokCommunication
api-version: 1.20
load: STARTUP
libraries:
- com.discord4j:discord4j-core:3.2.6
- net.dv8tion:JDA:5.0.0-beta.18

0 comments on commit 318398b

Please sign in to comment.