Skip to content

Commit

Permalink
Put latest Camelot version in help command
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Aug 24, 2024
1 parent 0293478 commit 1b83f66
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,28 @@
import net.dv8tion.jda.api.interactions.Interaction;
import net.dv8tion.jda.api.utils.messages.MessageEditBuilder;
import net.dv8tion.jda.api.utils.messages.MessageEditData;
import net.neoforged.camelot.commands.Commands;
import net.neoforged.camelot.BotMain;
import net.neoforged.camelot.commands.Commands;
import net.neoforged.camelot.commands.PaginatableCommand;
import net.neoforged.camelot.configuration.Common;
import net.neoforged.camelot.util.CachedOnlineData;
import net.neoforged.camelot.util.jda.ButtonManager;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;

import java.net.URI;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class HelpCommand extends PaginatableCommand<PaginatableCommand.SimpleData> {
private static final CachedOnlineData<String> LATEST_VERSION = CachedOnlineData.builder()
.client(BotMain.HTTP_CLIENT)
.uri(URI.create("https://search.maven.org/solrsearch/select?q=g:net.neoforged.camelot+AND+a:camelot&core=gav&rows=1&wt=json"))
.cacheDuration(Duration.ofHours(1))
.json()
.map(o -> o.getAsJsonObject("response").getAsJsonArray("docs").get(0).getAsJsonObject().get("v").getAsString())
.build();

public HelpCommand(ButtonManager buttonManager) {
super(buttonManager);
Expand Down Expand Up @@ -44,8 +55,14 @@ public CompletableFuture<MessageEditData> createMessage(int page, SimpleData dat
*/
public EmbedBuilder getHelpStartingAt(int index) {
EmbedBuilder embed = new EmbedBuilder();
embed.setAuthor(Common.NAME_WITH_VERSION, Common.REPO, BotMain.get().getSelfUser().getAvatarUrl());
embed.setDescription("## Latest commits:\n");
embed.setAuthor(Common.NAME_WITH_VERSION, Common.WEBSITE, BotMain.get().getSelfUser().getAvatarUrl());

var latest = LATEST_VERSION.getOrBust();
if (!latest.equals(Common.VERSION)) {
embed.appendDescription("You're running an outdated Camelot version. The latest version is **" + latest + "**.\n");
}

embed.appendDescription("## Last commits of this version:\n");

appendCommits: try (var is = HelpCommand.class.getResourceAsStream("/gitlog")) {
if (is == null) break appendCommits;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/neoforged/camelot/configuration/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ public class Common {

// The name of the bot.
public static final String NAME = "Camelot";
public static final String VERSION;
// The name of the bot following by " v${version}" if not in dev.
public static final String NAME_WITH_VERSION;

static {
final String version = Common.class.getPackage().getImplementationVersion();
VERSION = (version == null ? "dev" : version);
NAME_WITH_VERSION = version == null ? NAME : (NAME + " v" + version);
}

// The repository where development of Camelot happens.
public static final String REPO = "https://github.com/NeoForged/Camelot";
public static final String WEBSITE = "https://camelot.rocks/";
}
10 changes: 10 additions & 0 deletions src/main/java/net/neoforged/camelot/util/CachedOnlineData.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.neoforged.camelot.util;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -107,6 +108,14 @@ public Builder<T> gsonDecode(Gson gson, TypeToken<T> token) {
));
}

/**
* Decodes the response as a {@link JsonObject}.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public Builder<JsonObject> json() {
return gsonDecode(CODImpl.GSON, (TypeToken) TypeToken.get(JsonObject.class));
}

/**
* Changes the type of this builder by replacing the handler with one that maps the result of the previous handler.
*/
Expand Down Expand Up @@ -149,6 +158,7 @@ class CODImpl<T> implements CachedOnlineData<T> {
thread.setDaemon(true);
return thread;
});
static final Gson GSON = new Gson();

private final HttpClient client;
private final HttpRequest request;
Expand Down

0 comments on commit 1b83f66

Please sign in to comment.