UNPKG

djs-systems

Version:

The simplest way to build complex Discord bots.

191 lines (190 loc) 9.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.starboard = void 0; const discord_js_1 = require("discord.js"); const SimplyError_1 = require("./error/SimplyError"); // ------------------------------ // ------ F U N C T I O N ------- // ------------------------------ /** * Efficient yet Simplest starboard system ever existed ! * * `NOTE:` **Only Use it in `messageReactionAdd`, `messageReactionRemove` and `messageDelete` events.** * @param reaction * @param options * @link `Documentation:` https://simplyd.js.org/docs/systems/starboard * @example simplydjs.starboard(reaction, { channelId: '1234567890123' }) */ async function starboard(reactionOrMessage, options = { strict: false }) { return new Promise(async () => { const { client } = reactionOrMessage; const minimumRequired = options.min || 2; let extMessage; let react; if (reactionOrMessage.emoji) react = reactionOrMessage; else if (reactionOrMessage.author) extMessage = reactionOrMessage; if (!minimumRequired || Number.isNaN(minimumRequired) || minimumRequired == 0) { if (options?.strict) throw new SimplyError_1.SimplyError({ function: 'starboard', title: `Minimum number of stars [min] option is not a number.`, tip: `Expected an Integer/Number. Received ${minimumRequired || 'undefined'}.` }); else console.log(`SimplyError - starboard | Minimum number of stars [min] option is not a number.\n\nExpected an Integer/Number. Received ${minimumRequired || 'undefined'}.`); } if (!options.channelId) { if (options?.strict) throw new SimplyError_1.SimplyError({ function: 'starboard', title: `Provide an Channel ID to set the starboard channel` }); else console.log(`SimplyError - starboard | Provide an Channel ID to set the starboard channel`); } try { if (extMessage && !reactionOrMessage.message) { if (!extMessage.guild) return; const starboard = await client.channels.fetch(options.channelId, { force: true, cache: true }); if (!starboard) { if (options?.strict) throw new SimplyError_1.SimplyError({ function: 'starboard', title: `Invalid Channel (or) No VIEW_CHANNEL permission`, tip: `Check the permissions (or) Try using another Channel ID.\nReceived ${options.channelId || 'undefined'}` }); else console.log(`SimplyError - starboard | Invalid Channel (or) No VIEW_CHANNEL permission\n\nCheck the permissions (or) Try using another Channel ID.\n Received ${options.channelId || 'undefined'}`); } const messages = await starboard?.messages.fetch({ limit: 100 }); const existing = messages.find((msg) => msg.embeds[0]?.footer?.text == '⭐ | ID: ' + extMessage.id); if (existing) { return await existing.delete(); } } else if (react) { if (react.emoji.id == options.emoji || react.emoji.name == '⭐' || react.emoji.name == '🌟') { const minmax = react.count; const starboard = await client.channels.fetch(options.channelId, { force: true, cache: true }); if (minmax < minimumRequired) { const messages = await starboard?.messages.fetch({ limit: 100 }); const existing = messages.find((msg) => msg.embeds[0]?.footer?.text == '⭐ | ID: ' + extMessage.id); if (existing) { return await existing.delete(); } } if (!react.message.guild) return; if (starboard.guild.id !== react.message.guild.id) return; if (!starboard) { if (options?.strict) throw new SimplyError_1.SimplyError({ function: 'starboard', title: `Invalid Channel (or) No VIEW_CHANNEL permission`, tip: `Check the permissions (or) Try using another Channel ID.\nReceived ${options.channelId || 'undefined'}` }); else console.log(`SimplyError - starboard | Invalid Channel (or) No VIEW_CHANNEL permission\n\nCheck the permissions (or) Try using another Channel ID.\n Received ${options.channelId || 'undefined'}`); } const fetch = await react.message.fetch(true); const attachment = fetch.attachments.first(); let url = attachment ? attachment.url : null; if (fetch.content.match(/\bhttps?:\/\/\S+/gi)) { url = fetch.content.match(/\bhttps?:\/\/\S+/gi)[0]; } if (fetch.embeds[0]?.data?.thumbnail) { url = fetch.embeds[0]?.data?.thumbnail?.url; } if (fetch.embeds[0]?.image) { url = fetch.embeds[0].image.url; } const embed = new discord_js_1.EmbedBuilder() .setAuthor(options?.embed?.author || { name: fetch.author.username, iconURL: fetch.author.displayAvatarURL({ forceStatic: false }) }) .setColor(options.embed?.color || '#FFC83D') .setDescription(options.embed?.description || fetch.content || '** **') .setTitle(options.embed?.title || `Jump to message`) .setURL(fetch.url) .setFooter({ text: '⭐ | ID: ' + fetch.id }); if (url) embed.setImage(url); if (options?.embed?.fields) embed.setFields(options.embed?.fields); if (options?.embed?.thumbnail) embed.setThumbnail(options.embed?.thumbnail); if (options?.embed?.timestamp) embed.setTimestamp(options.embed?.timestamp); const messages = await starboard?.messages.fetch({ limit: 100 }); const starEmoji = options.emoji ? client.emojis.cache.get(options?.emoji) || '⭐' : '⭐'; const btn = new discord_js_1.ButtonBuilder() .setLabel((react.count ? react.count : 1).toString()) .setEmoji(starEmoji) .setCustomId('starboard') .setDisabled(true) .setStyle(discord_js_1.ButtonStyle.Primary); const link = new discord_js_1.ButtonBuilder() .setLabel(`Jump to message`) .setStyle(discord_js_1.ButtonStyle.Link) .setURL(fetch.url); const row = new discord_js_1.ActionRowBuilder().addComponents([ btn, link ]); const existing = messages.find((msg) => msg.embeds[0]?.footer?.text == '⭐ | ID: ' + fetch.id); if (existing) { if (react.count < minimumRequired) return await existing.delete(); else await existing.edit({ content: `**${starEmoji} ${react.count}${attachment?.contentType?.startsWith('video') ? `\n${url}` : ''}**`, embeds: [embed], components: [row] }); } else { await starboard.send({ content: `**${starEmoji} ${react.count}${attachment?.contentType?.startsWith('video') ? `\n${url}` : ''}**`, embeds: [embed], components: [row] }); } } } } catch (err) { if (options?.strict) throw new SimplyError_1.SimplyError({ function: 'starboard', title: 'An Error occured when running the function ', tip: err.stack }); else console.log(`SimplyError - starboard | Error: ${err.stack}`); } }); } exports.starboard = starboard;