UNPKG

djs-systems

Version:

The simplest way to build complex Discord bots.

140 lines (139 loc) 6.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.meme = void 0; const discord_js_1 = require("discord.js"); const misc_1 = require("./misc"); const SimplyError_1 = require("./error/SimplyError"); // ------------------------------ // ------ F U N C T I O N ------- // ------------------------------ /** * The memes are sent automatically, so others will able to laugh at the jokes without having to do anything ! * @param clientOrChannel * @param options * @link `Documentation:` https://simplyd.js.org/docs/systems/meme * @example simplydjs.meme(client, { channelId: '1234567890123' }) * @example simplydjs.meme(channel, { sub: ["coding", "memes"] }) */ async function meme(clientOrChannel, options = { strict: false }) { return new Promise(async () => { try { // Get a channel from option or its Id const ch = options.channelId; if (!ch) { // If user is strict, throw an error or just console log. if (options?.strict) throw new SimplyError_1.SimplyError({ function: 'meme', title: 'Channel/channelId is not specified', tip: `Expected channelId as string in options.. | Received ${ch + ` (${typeof ch})` || 'undefined'}` }); else console.log(`SimplyError - meme | Channel/channelId is not specified\n\n Expected channelId as string in options.. | Received ${ch + ` (${typeof ch})` || 'undefined'}`); } // Default subreddits. Can override by array of strings in sub option let sub = [ 'meme', 'me_irl', 'memes', 'dankmeme', 'dankmemes', 'ComedyCemetery', 'terriblefacebookmemes', 'funny' ]; if (Array.isArray(options.sub)) { sub = options.sub; } else if (!Array.isArray(options.sub)) { sub.push(options.sub); } // Getting random subreddit let interval; if (options?.interval) { if (options?.interval < 60000) { if (options?.strict) throw new SimplyError_1.SimplyError({ function: 'meme', title: 'Provide an interval time above 60000ms', tip: `Expected Interval time above 60000ms (1 minute) | Received ${options.interval || 'undefined'}` }); else console.log(`SimplyError - meme | Provide an interval time above 60000ms\n\nExpected Interval time above 60000ms (1 minute) | Received ${options.interval || 'undefined'}`); } interval = options.interval; } else { interval = (0, misc_1.ms)('10m'); // 600k ms (10 minutes) } setInterval(async () => { const random = Math.floor(Math.random() * sub.length); // Getting the channel from Discord let channel; if (clientOrChannel.id) channel = clientOrChannel; else if (clientOrChannel) channel = await clientOrChannel.channels.fetch(ch, { force: true }); // If its unavailable, throw an error. if (!channel) { if (options?.strict) throw new SimplyError_1.SimplyError({ function: 'meme', 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 - meme | Invalid Channel (or) No VIEW_CHANNEL permission\n\nCheck the permissions (or) Try using another Channel ID.\nReceived ${options.channelId || 'undefined'}`); } let subreddit = sub[random]; if (!subreddit) subreddit = 'meme'; // Get a random reddit post from the subreddit const response = await (0, misc_1.https)(`https://meme-api.com/gimme/${subreddit}`); if (!response) return; if (response.nsfw) return; // Get all the data from its API const url = response.postLink; const memeImage = response.url || response.preview[3]; const title = response.title; const up = response.ups; // Building an Embed to send it. const embed = new discord_js_1.EmbedBuilder() .setTitle(options.embed?.title || `${title}`) .setURL(`${url}`) .setImage(memeImage) .setColor(options.embed?.color || (0, misc_1.toRgb)('#406DBC')) .setFooter({ text: `🔺 ${up}` }); if (options?.embed?.fields) embed.setFields(options.embed?.fields); if (options?.embed?.author) embed.setAuthor(options.embed?.author); if (options?.embed?.thumbnail) embed.setThumbnail(options.embed?.thumbnail); if (options?.embed?.timestamp) embed.setTimestamp(options.embed?.timestamp); if (options?.embed?.title) embed.setTitle(options.embed?.title); if (options?.embed?.url) embed.setURL(options.embed?.url); await channel.send({ embeds: [embed] }); }, interval); } catch (err) { if (options?.strict) throw new SimplyError_1.SimplyError({ function: 'meme', title: 'An Error occured when running the function ', tip: err.stack }); else console.log(`SimplyError - meme | Error: ${err.stack}`); } }); } exports.meme = meme;