Update from Git Manager GUI
This commit is contained in:
75
util/helpers.js
Normal file
75
util/helpers.js
Normal file
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Shared utility functions for PluginBot
|
||||
* Centralised here to avoid duplication across commands
|
||||
*/
|
||||
|
||||
export function generateAvatarLink(authorID) {
|
||||
const idStr = authorID.toString();
|
||||
const splitPoint = Math.ceil(idStr.length / 2);
|
||||
const firstHalf = idStr.substring(0, splitPoint);
|
||||
return `https://www.spigotmc.org/data/avatars/l/${firstHalf}/${authorID}.jpg`;
|
||||
}
|
||||
|
||||
export function generateAuthorURL(authorName, authorID) {
|
||||
return `https://www.spigotmc.org/members/${authorName}.${authorID}/`;
|
||||
}
|
||||
|
||||
export function generateResourceIconURL(resource) {
|
||||
return resource.icon
|
||||
.fullUrl()
|
||||
.replace("orgdata", "org/data")
|
||||
.replace("https://spigotmc.org", "https://www.spigotmc.org");
|
||||
// www must be present – embeds don't render without it
|
||||
}
|
||||
|
||||
export function generateResourceURL(resourceID) {
|
||||
return `https://spigotmc.org/resources/.${resourceID}/`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts basic SpigotMC HTML to Discord Markdown.
|
||||
* BUG FIX: </i> was previously mapped to "**" (bold) instead of "*" (italic)
|
||||
*/
|
||||
export function formatText(description) {
|
||||
return description
|
||||
.replace(/<b>/gi, "**")
|
||||
.replace(/<\/b>/gi, "**")
|
||||
.replace(/<i>/gi, "*")
|
||||
.replace(/<\/i>/gi, "*") // ← was "**" before (bug)
|
||||
.replace(/<ul>/gi, "")
|
||||
.replace(/<\/ul>/gi, "")
|
||||
.replace(/<li>/gi, "• ")
|
||||
.replace(/<\/li>/gi, "\n")
|
||||
.replace(/<br\s*\/?>/gi, "\n")
|
||||
.replace(/<[^>]+>/g, ""); // strip any remaining HTML tags
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the latest version name for a Spiget resource.
|
||||
* Uses native fetch instead of the legacy xmlhttprequest package.
|
||||
*/
|
||||
export async function getLatestVersion(resourceID) {
|
||||
const res = await fetch(
|
||||
`https://api.spiget.org/v2/resources/${resourceID}/versions/latest`
|
||||
);
|
||||
if (!res.ok) throw new Error(`Spiget version request failed: HTTP ${res.status}`);
|
||||
const data = await res.json();
|
||||
return data.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the latest update description for a Spiget resource.
|
||||
* Returns a Discord-safe string (max 1024 chars).
|
||||
*/
|
||||
export async function getUpdateDescription(resourceID) {
|
||||
const res = await fetch(
|
||||
`https://api.spiget.org/v2/resources/${resourceID}/updates/latest`
|
||||
);
|
||||
if (!res.ok) throw new Error(`Spiget update request failed: HTTP ${res.status}`);
|
||||
const data = await res.json();
|
||||
const decoded = Buffer.from(data.description, "base64").toString("utf8");
|
||||
const formatted = formatText(decoded);
|
||||
return formatted.length > 1024
|
||||
? "Description is greater than 1024 characters – visit the resource page for details."
|
||||
: formatted;
|
||||
}
|
||||
Reference in New Issue
Block a user