100 lines
2.7 KiB
JavaScript
100 lines
2.7 KiB
JavaScript
const { EmbedBuilder } = require('discord.js');
|
|
const { DebugBuilder } = require("./utilities/debugBuilder");
|
|
const log = new DebugBuilder("server", "libUtils");
|
|
const { NodeHtmlMarkdown } = require('node-html-markdown');
|
|
|
|
exports.EmmeliaEmbedBuilder = class PostEmbedBuilder extends EmbedBuilder {
|
|
constructor() {
|
|
super()
|
|
this.setTimestamp();
|
|
this.setFooter({ text: 'Brought to you by Emmelia.' });
|
|
}
|
|
}
|
|
|
|
/**
|
|
* sleep - sleep/wait
|
|
* @constructor
|
|
*/
|
|
exports.runAfter = async (toRun, timeout = 10000) => {
|
|
log.DEBUG(`Running '${toRun}' after ${timeout / 1000} seconds`);
|
|
setTimeout(toRun, timeout)
|
|
}
|
|
|
|
/**
|
|
* Normalize a port into a number, string, or false.
|
|
*
|
|
* @param {*} val Value to be normalized
|
|
* @returns Normalized value
|
|
*/
|
|
exports.normalizePort = (val) => {
|
|
var port = parseInt(val, 10);
|
|
|
|
if (isNaN(port)) {
|
|
// named pipe
|
|
return val;
|
|
}
|
|
|
|
if (port >= 0) {
|
|
// port number
|
|
return port;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Event listener for HTTP server "error" event.
|
|
*/
|
|
exports.onError = (error) => {
|
|
if (error.syscall !== 'listen') {
|
|
throw error;
|
|
}
|
|
|
|
var bind = typeof port === 'string'
|
|
? 'Pipe ' + port
|
|
: 'Port ' + port;
|
|
|
|
// handle specific listen errors with friendly messages
|
|
switch (error.code) {
|
|
case 'EACCES':
|
|
log.ERROR(bind + ' requires elevated privileges');
|
|
process.exit(1);
|
|
break;
|
|
case 'EADDRINUSE':
|
|
log.ERROR(bind + ' is already in use');
|
|
process.exit(1);
|
|
break;
|
|
default:
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
exports.sendPost = (post, source, channel, callback) => {
|
|
const postTitle = post.title;
|
|
const postLink = post.link;
|
|
const postContent = NodeHtmlMarkdown.translate(post.content);
|
|
const postId = post.postId;
|
|
const postPubDate = new Date(post.pubDate).toISOString() ?? new Date().toISOString();
|
|
var postSourceLink = new URL(source.link);
|
|
postSourceLink = postSourceLink.hostname;
|
|
const postImage = post.image ?? undefined;
|
|
log.DEBUG("Sending an RSS post to discord", postTitle, postId)
|
|
try{
|
|
const rssMessage = new this.EmmeliaEmbedBuilder()
|
|
.setColor(0x0099FF)
|
|
.setTitle(postTitle)
|
|
.setURL(postLink)
|
|
.addFields({ name: "Post Content", value: postContent.slice(0,1024), inline: false })
|
|
.addFields({ name: 'Published', value: postPubDate, inline: true })
|
|
.addFields({ name: 'Source', value: postSourceLink, inline: true });
|
|
|
|
if (postImage) rssMessage.setImage(postImage);
|
|
|
|
channel.send({ embeds: [rssMessage] });
|
|
return callback(undefined, true);
|
|
}
|
|
catch (err){
|
|
log.ERROR("Error sending message: ", err);
|
|
return callback(err, undefined);
|
|
}
|
|
} |