Skip to content

Commit

Permalink
Move to server mode instead of socket mode
Browse files Browse the repository at this point in the history
  • Loading branch information
polypixeldev committed Mar 22, 2024
1 parent 811eb14 commit d553cc1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 64 deletions.
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 6 additions & 41 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ java {

dependencies {
implementation("com.google.code.gson:gson:2.10.1")
implementation("com.slack.api:bolt-socket-mode:1.37.0")
implementation("com.slack.api:bolt:1.38.3")
implementation("com.slack.api:bolt-servlet:1.38.3")
implementation("com.slack.api:bolt-jetty:1.38.3")
implementation("javax.websocket:javax.websocket-api:1.1")
implementation("org.glassfish.tyrus.bundles:tyrus-standalone-client:1.19")
implementation("org.slf4j:slf4j-simple:2.0.5")
implementation("org.apache.commons:commons-text:1.10.0")
implementation("org.apache.commons:commons-text:1.11.0")
implementation("org.apache.commons:commons-collections4:4.4")

compileOnly("com.frengor:ultimateadvancementapi:2.2.3")
Expand Down
47 changes: 26 additions & 21 deletions src/main/java/com/hackclub/hccore/slack/SlackBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.slack.api.Slack;
import com.slack.api.bolt.App;
import com.slack.api.bolt.AppConfig;
import com.slack.api.bolt.jetty.SlackAppServer;
import com.slack.api.bolt.request.builtin.SlashCommandRequest;
import com.slack.api.bolt.socket_mode.SocketModeApp;
import com.slack.api.methods.MethodsClient;
import com.slack.api.methods.SlackApiException;
import com.slack.api.methods.response.chat.ChatPostMessageResponse;
import com.slack.api.methods.response.users.profile.UsersProfileGetResponse;
import com.slack.api.model.User;
import com.slack.api.model.event.MessageBotEvent;
import com.slack.api.model.event.MessageChannelJoinEvent;
import com.slack.api.model.event.MessageDeletedEvent;
import com.slack.api.model.event.MessageEvent;
import io.papermc.paper.event.player.AsyncChatEvent;
Expand All @@ -43,14 +41,14 @@
import java.util.Map.Entry;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.regex.Pattern;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.apache.commons.collections4.map.PassiveExpiringMap;
import org.apache.commons.text.StringEscapeUtils;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.advancement.Advancement;
Expand All @@ -76,21 +74,24 @@ public class SlackBot implements Listener {
public static final String playerServerJoinAvatarUrl = "https://cloud-if9tepzbn-hack-club-bot.vercel.app/1hccorejoin.png";
public static final String playerAdvancementAvatarUrl = "https://cloud-obk2f29h4-hack-club-bot.vercel.app/0achievement.png";
private final HCCorePlugin plugin;
private final SocketModeApp socket;
private final App app;
private final SlackAppServer server;
private final String commandBase;
private final PassiveExpiringMap<UUID, String> mcLinkCodes;

public SlackBot(HCCorePlugin plugin) throws Exception {
this.plugin = plugin;
App app = new App(AppConfig.builder().singleTeamBotToken(getBotToken()).build());
this.app = new App(AppConfig.builder().singleTeamBotToken(getBotToken()).signingSecret(getSigningSecret()).build());
commandBase = plugin.getConfig().getString("settings.slack-link.base-command", "minecraft");
mcLinkCodes = new PassiveExpiringMap<>(
plugin.getConfig().getLong("settings.slack-link.link-code-expiration", 60 * 10) * 1000);

Pattern sdk = Pattern.compile(".*");
app.message(sdk, (payload, ctx) -> {
MessageEvent event = payload.getEvent();
String text = StringEscapeUtils.unescapeHtml4(event.getText());
// Weird bug with this right now...
// String text = StringEscapeUtils.unescapeHtml4(event.getText());
String text = event.getText();
String channelId = event.getChannel();
String mainChannel = getSlackChannel();
if (!channelId.equals(mainChannel)) {
Expand Down Expand Up @@ -176,8 +177,6 @@ public SlackBot(HCCorePlugin plugin) throws Exception {

app.event(MessageDeletedEvent.class, (payload, ctx) -> ctx.ack());

app.event(MessageChannelJoinEvent.class, (payload, ctx) -> ctx.ack());

CommandDispatcher<SlashCommandRequest> dispatcher = new CommandDispatcher<>();
dispatcher.register(
LiteralArgumentBuilder.<SlashCommandRequest>literal("/%s".formatted(commandBase)).then(
Expand Down Expand Up @@ -333,9 +332,15 @@ public SlackBot(HCCorePlugin plugin) throws Exception {
return ctx.ack();
}));

SocketModeApp socket = new SocketModeApp(getAppToken(), app);
this.socket = socket;
socket.startAsync();
SlackAppServer server = new SlackAppServer(app);
this.server = server;
CompletableFuture.runAsync(() -> {
try {
server.start();
} catch(Exception e) {
e.printStackTrace();
}
});
this.plugin.getLogger().info("HackCraft Slack started!");
sendMessage(":large_green_circle: *Server Started*", serverConsoleAvatarUrl, "Console");
}
Expand All @@ -347,7 +352,7 @@ public SlackBot(HCCorePlugin plugin) throws Exception {

public void disconnect() throws Exception {
sendMessage(":tw_octagonal_sign: *Server Stopped*", serverConsoleAvatarUrl, "Console");
this.socket.stop();
this.server.stop();
}


Expand Down Expand Up @@ -475,18 +480,18 @@ private String getBotToken() {
return botToken;
}

private String getAppToken() {
String appToken = this.plugin.getConfig().getString("settings.slack-link.app-token");
private String getSigningSecret() {
String signingSecret = this.plugin.getConfig().getString("settings.slack-link.signing-secret");

if (appToken == null) {
throw new IllegalStateException("Slack app token is not set!");
if (signingSecret == null) {
throw new IllegalStateException("Slack signing secret is not set!");
}

return appToken;
return signingSecret;
}

public User getUserInfo(String id) throws IOException {
MethodsClient client = this.socket.getApp().getClient();
MethodsClient client = app.getClient();
try {
var res = client.usersInfo(r -> r.token(getBotToken()).user(id));

Expand All @@ -497,7 +502,7 @@ public User getUserInfo(String id) throws IOException {
}

void sendMessage(String msg, String iconURL, String username) throws IOException {
MethodsClient client = Slack.getInstance().methods();
MethodsClient client = app.getClient();

try {
var res = client.chatPostMessage(
Expand All @@ -513,7 +518,7 @@ void sendMessage(String msg, String iconURL, String username) throws IOException
}

public boolean sendVerificationMessage(String id, String mcName, String uuid) throws IOException {
MethodsClient client = this.socket.getApp().getClient();
MethodsClient client = app.getClient();

try {
var res = client.chatPostMessage(r -> r.token(getBotToken()).channel(id).text(
Expand Down

0 comments on commit d553cc1

Please sign in to comment.