@getsolara/solara.canvas
Version:
Optional canvas and image manipulation functionality for @getsolara/solara.js
40 lines • 2.79 kB
JavaScript
const { AttachmentBuilder } = require('discord.js');
module.exports = {
name: "$spotifyCard",
description: "Generates a Spotify status card image. Args: title;author;album;imageURL;[startTimestampMs?];[endTimestampMs?];[blur?];[overlayOpacity?]",
takesBrackets: true,
execute: async (context, args) => {
if (context.client.canvasInitialized === false) {
return "[Error: $spotifyCard requires canvas features to be enabled. Ensure @getsolara/solara.canvas is installed and configured correctly.]";
}
if (args.length < 4) return "[Error: $spotifyCard requires title, author, album, and imageURL]";
const [title, author, album, image, startMsStr, endMsStr, blurStr, opacityStr] = args;
const startMs = startMsStr ? parseInt(startMsStr, 10) : undefined;
const endMs = endMsStr ? parseInt(endMsStr, 10) : undefined;
const blur = blurStr ? parseInt(blurStr, 10) : undefined;
const opacity = opacityStr ? parseFloat(opacityStr) : undefined;
if (startMs !== undefined && isNaN(startMs)) return "[Error: Invalid start timestamp for $spotifyCard. Must be a number.]";
if (endMs !== undefined && isNaN(endMs)) return "[Error: Invalid end timestamp for $spotifyCard. Must be a number.]";
if (blur !== undefined && isNaN(blur)) return "[Error: Invalid blur value for $spotifyCard. Must be a number.]";
if (opacity !== undefined && isNaN(opacity)) return "[Error: Invalid opacity value for $spotifyCard. Must be a number.]";
try {
const canvafy = require("canvafy");
const builder = new canvafy.Spotify().setTitle(title).setAuthor(author).setAlbum(album).setImage(image);
if (startMs !== undefined && endMs !== undefined) builder.setTimestamp(startMs, endMs);
if (blur !== undefined) builder.setBlur(blur);
if (opacity !== undefined) builder.setOverlayOpacity(opacity);
const imageBuffer = await builder.build();
const attachment = new AttachmentBuilder(imageBuffer, { name: `spotify-${context.user?.id || Date.now()}.png` });
context.attachments = context.attachments || [];
context.attachments.push(attachment);
return "";
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND' && e.message.includes('canvafy')) {
console.error("Solara.canvas ($spotifyCard): 'canvafy' module not found. This is a dependency of @getsolara/solara.canvas.");
return "[Error: $spotifyCard - canvafy module missing.]";
}
console.error("Solara.canvas Error ($spotifyCard):", e);
return `[Error generating spotify card: ${e.message}]`;
}
}
};