|
| 1 | +/* |
| 2 | + * MMDBot - https://github.com/MinecraftModDevelopment/MMDBot |
| 3 | + * Copyright (C) 2016-2023 <MMD - MinecraftModDevelopment> |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; |
| 8 | + * Specifically version 2.1 of the License. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this library; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + * USA |
| 19 | + * https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html |
| 20 | + */ |
| 21 | +package com.mcmoddev.mmdbot.commander.updatenotifiers.blockbench; |
| 22 | + |
| 23 | +import com.mcmoddev.mmdbot.commander.config.Configuration; |
| 24 | +import com.mcmoddev.mmdbot.commander.updatenotifiers.UpdateNotifier; |
| 25 | +import com.mcmoddev.mmdbot.commander.util.StringSerializer; |
| 26 | +import com.mcmoddev.mmdbot.core.util.Utils; |
| 27 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 28 | +import net.dv8tion.jda.api.entities.MessageEmbed; |
| 29 | +import org.jetbrains.annotations.NotNull; |
| 30 | + |
| 31 | +import javax.annotation.Nullable; |
| 32 | +import java.io.IOException; |
| 33 | +import java.time.Instant; |
| 34 | +import java.util.Comparator; |
| 35 | +import java.util.stream.Collectors; |
| 36 | +import java.util.stream.Stream; |
| 37 | + |
| 38 | +public class BlockbenchUpdateNotifier extends UpdateNotifier<GithubRelease> { |
| 39 | + public BlockbenchUpdateNotifier() { |
| 40 | + super(NotifierConfiguration.<GithubRelease>builder() |
| 41 | + .name("blockbench") |
| 42 | + .channelGetter(Configuration.Channels.UpdateNotifiers::blockbench) |
| 43 | + .versionComparator(Comparator.comparing(release -> Instant.parse(release.published_at()))) |
| 44 | + .serializer(StringSerializer.json(StringSerializer.RECORD_GSON, GithubRelease.class)) |
| 45 | + .webhookInfo(new WebhookInfo("Blockbench Updates", "https://www.blockbench.net/favicon.png")) |
| 46 | + .build()); |
| 47 | + } |
| 48 | + |
| 49 | + @Nullable |
| 50 | + @Override |
| 51 | + protected GithubRelease queryLatest() throws IOException { |
| 52 | + return BlockbenchVersionHelper.getLatest(loggingMarker); |
| 53 | + } |
| 54 | + |
| 55 | + @NotNull |
| 56 | + @Override |
| 57 | + protected EmbedBuilder getEmbed(@Nullable final GithubRelease oldVersion, final @NotNull GithubRelease newVersion) { |
| 58 | + return new EmbedBuilder() |
| 59 | + .setTitle("New Blockbench %s: %s".formatted(newVersion.prerelease() ? "pre-release" : "release", newVersion.name()), newVersion.html_url()) |
| 60 | + .setColor(newVersion.prerelease() ? 0x29CFD8 : 0x1E93D9) |
| 61 | + .setDescription(Utils.truncate(Stream.of(newVersion.body().split("\n")) |
| 62 | + .map(str -> str.trim().startsWith("#") ? "**" + str.replace("#", "") + "**" : str) |
| 63 | + .collect(Collectors.joining("\n")), MessageEmbed.DESCRIPTION_MAX_LENGTH / 2)); // 4k char embed is big... |
| 64 | + } |
| 65 | +} |
0 commit comments