discord-notify
Version:
A Discord API-compliant notification service for Node.js applications with file attachments, thread support, and rich embeds
162 lines âĸ 6.06 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
function DiscordNotifyFactory(config) {
const { webhookUrl, appName = 'Discord Notify', environment = 'development', username, avatarUrl, threadId } = config;
const defaultColor = 0x3180c6; // Blue
const successColor = 0x36a64f; // Green
const errorColor = 0xff0000; // Red
const warningColor = 0xffa500; // Orange
const infoColor = 0x00ccff; // Light Blue
const sendWebhook = async (payload) => {
if (!webhookUrl) {
console.warn('Discord webhook URL not provided');
return { success: false, error: 'Webhook URL not provided' };
}
try {
const webhookPayload = {
...payload,
username: payload.username || username,
avatar_url: payload.avatar_url || avatarUrl,
thread_id: payload.thread_id || threadId
};
const response = await fetch(webhookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(webhookPayload),
});
if (!response.ok) {
const errorText = await response.text();
console.error(`Discord webhook failed: ${response.status} ${response.statusText} - ${errorText}`);
return {
success: false,
status: response.status,
statusText: response.statusText,
error: errorText
};
}
// Discord returns the message object on success
const messageData = await response.json();
return {
success: true,
status: response.status,
messageId: messageData.id
};
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
console.error('Error sending Discord webhook:', error);
return {
success: false,
error: errorMessage
};
}
};
const createEmbed = (args, color = defaultColor) => {
if (typeof args === 'string') {
return {
description: args,
color: color,
timestamp: new Date().toISOString(),
footer: {
text: `${appName} - ${environment}`
}
};
}
return {
title: args.title,
description: args.description || args.text,
color: args.color || color,
fields: args.fields,
timestamp: args.timestamp || new Date().toISOString(),
footer: args.footer || {
text: `${appName} - ${environment}`
},
thumbnail: args.thumbnail,
image: args.image,
author: args.author,
url: args.url
};
};
const notifier = {
async send(args) {
const embed = createEmbed(args);
const content = typeof args === 'string' ? undefined : args.content;
await sendWebhook({
content,
embeds: [embed]
});
},
extend(args) {
return {
async send(extendArgs) {
const baseEmbed = createEmbed(args);
const extendEmbed = createEmbed(extendArgs);
const content = typeof extendArgs === 'string' ? undefined : extendArgs.content;
await sendWebhook({
content,
embeds: [baseEmbed, extendEmbed]
});
}
};
},
async success(args) {
const embed = createEmbed(args, successColor);
embed.title = embed.title ? `â
${embed.title}` : 'â
Success';
const content = typeof args === 'string' ? undefined : args.content;
await sendWebhook({
content,
embeds: [embed]
});
},
async error(args) {
const embed = createEmbed(args, errorColor);
embed.title = embed.title ? `đ¨ ${embed.title}` : 'đ¨ Error';
const content = typeof args === 'string' ? undefined : args.content;
await sendWebhook({
content,
embeds: [embed]
});
},
async alert(args) {
const embed = createEmbed(args, warningColor);
embed.title = embed.title ? `â ī¸ ${embed.title}` : 'â ī¸ Alert';
const content = typeof args === 'string' ? undefined : args.content;
await sendWebhook({
content,
embeds: [embed]
});
},
async info(args) {
const embed = createEmbed(args, infoColor);
embed.title = embed.title ? `âšī¸ ${embed.title}` : 'âšī¸ Info';
const content = typeof args === 'string' ? undefined : args.content;
await sendWebhook({
content,
embeds: [embed]
});
},
async sendFile(args, file) {
const embed = createEmbed(args);
const content = typeof args === 'string' ? undefined : args.content;
await sendWebhook({
content,
embeds: [embed],
files: [file]
});
},
async sendToThread(args, threadId) {
const embed = createEmbed(args);
const content = typeof args === 'string' ? undefined : args.content;
await sendWebhook({
content,
embeds: [embed],
thread_id: threadId
});
}
};
return notifier;
}
exports.default = DiscordNotifyFactory;
//# sourceMappingURL=index.js.map
;