πŸ”₯πŸ€– Automate MEMEs posting to your Discord with NodeJS and Novu πŸš€πŸš€

Nevo David - Aug 21 '23 - - Dev Community

TL;DR

In this tutorial, you'll learn how to build an automated Discord bot with Novu. The bot will send memes from a subreddit to your Discord channel every hour ⏰

Automate


Novu: Open-source notification infrastructure πŸš€

Just a quick background about us. Novu is an open-source notification infrastructure. We basically help to manage all the product notifications. It can be In-App (the bell icon like you have in the Dev Community), Emails, SMSs and so on.

Like


Let's set it up πŸ”₯

Create your project folder and run npm init -y to add a package.json file.



mkdir discord-bot
cd discord-bot
npm init -y


Enter fullscreen mode Exit fullscreen mode

InstallΒ Novu SDK,Β Nodemon,Β Node Cron, andΒ Fast XML Parser.



npm install @novu/node fast-xml-parser node-cron nodemon


Enter fullscreen mode Exit fullscreen mode

Novu enables us to send Discord messages in Node.js, Node Cron helps with scheduling tasks, Fast XML Parser for converting XML files to JSON, and Nodemon is a Node.js tool that automatically restarts the server after detecting file changes.

Create an index.js file - the entry point to the web server.



touch index.js


Enter fullscreen mode Exit fullscreen mode

Configure Nodemon by adding the start command to the list of scripts in the package.json file. The code snippet below starts the server using Nodemon.



//πŸ‘‡πŸ» In server/package.json

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon index.js"
  },


Enter fullscreen mode Exit fullscreen mode

Congratulations!πŸŽ‰ You can now start the server by using the command below.



npm start


Enter fullscreen mode Exit fullscreen mode

Getting memes from Reddit πŸ€–

Copy the code below into the index.js file.



const { XMLParser } = require("fast-xml-parser");
const parser = new XMLParser();
//πŸ‘‡πŸ» Regex for retrieving memes image URL
const urlPattern = /https:\/\/i\.redd\.it[^"]*/g;

const getMemes = async () => {
    const matchedUrls = [];
    try {
        //πŸ‘‡πŸ» fetch XML data
        const fetchMeme = await fetch(
            "https://www.reddit.com/r/ProgrammerHumor/.rss"
        );
        const XMLdata = await fetchMeme.text();
        //πŸ‘‡πŸ» convert XML data to JSON format
        const jsonObj = parser.parse(XMLdata);
        const content = jsonObj.feed.entry;
        //πŸ‘‡πŸ» map through the memes data
        const contentArray = content.map((obj) => obj.content);
        contentArray.forEach((htmlString) => {
            const matches = htmlString.match(urlPattern);
            if (matches) {
                // Add memes into an array
                matchedUrls.push(...matches);
            }
        });
        return matchedUrls;
    } catch (err) {
        console.error(err);
    }
};

//πŸ‘‡πŸ» log memes array to the console (for testing purposes)
const logMemes = async () => {
    const memes = await getMemes();
    console.log(memes);
};

logMemes();


Enter fullscreen mode Exit fullscreen mode

The code snippet above fetches the XML data (RSS feed) from the ProgrammerHumor subreddit, converts it to JSON format using the XMLParser, and retrieves only the memes from the large amount of data returned.


Reddit to Discord using Novu πŸͺ„ ✨

Here, you'll learn how to send messages to your Discord channels withΒ NovuΒ - an open-source notification infrastructure that enables you to send in-app, SMS, chat, push, and e-mail notifications from a single dashboard.

Before we proceed, create your Discord server and right-click on the channel where you need to add the bot.

Proceed

Select Edit Channel > Integrations and Webhooks to create a new webhook URL.

Webhook

Copy the Copy Webhook URL and paste somewhere on your computer; we'll use it shortly.

Hook

Creating a Novu project

Run the code snippet below to create your Novu account.



npx novu init


Enter fullscreen mode Exit fullscreen mode

Enter your application name and sign in to your Novu dashboard. The code snippet below contains the steps you need to follow after running npx novu init.



Now let's setup your account and send your first notification
? What is your application name? Discord Bot
? Now lets setup your environment. How would you like to proceed? Create a free cloud account (Recommended)
? Create your account with: Sign-in with GitHub
? I accept the Terms and Conditions (https://novu.co/terms) and have read the Privacy Policy (https://novu.co/privacy) Yes
βœ” Created your account successfully.


Enter fullscreen mode Exit fullscreen mode

Visit the demo page, copy your subscriber ID from the page, and click the Skip Tutorial button.

Novu1

Next, activate the Discord chat channel on your Novu integrations store.

Finally, create a workflow that triggers the Chat notification.

Novu2

From the code snippet above, I added a variable - {{content}} as the Chat message content to enable us to send dynamic messages.

Remember to copy your API key from your dashboard, we'll use in the upcoming section.

Discord Messages

Sending messages to the Discord channel

Add the following imports to the top of your index.js file.



const { Novu, ChatProviderIdEnum } = require("@novu/node");
const novu = new Novu("<YOUR_API_KEY>");
const subscriberId = "<YOUR_SUBSCRIBER_ID>";


Enter fullscreen mode Exit fullscreen mode

Create a function that attach the webhook URL to your subscriber details.



const sendChatMessage = async () => {
    await novu.subscribers.setCredentials(
        subscriberId,
        ChatProviderIdEnum.Discord,
        {
            webhookUrl: "<DISCORD_WEBHOOK_URL>",
        }
    );
};


Enter fullscreen mode Exit fullscreen mode

Finally, update the function to loop through the memes retrieved from the subreddit and send them to the Discord channel.



const sendChatMessage = async () => {
    await novu.subscribers.setCredentials(
        subscriberId,
        ChatProviderIdEnum.Discord,
        {
            webhookUrl: "<DISCORD_WEBHOOK_URL>",
        }
    );
    //πŸ‘‡πŸ» gets the memes
    const memes = await getMemes();
    //πŸ‘‡πŸ» loops through the memes
    memes.forEach(async (meme) => {
        //πŸ‘‡πŸ» sends the meme via Novu
        await novu
            .trigger("discord", {
                to: {
                    subscriberId,
                },
                payload: { content: meme },
            })
            .then((res) => console.log(res.data.data));
    });
};

//πŸ‘‡πŸ» execute the function
sendChatMessage();


Enter fullscreen mode Exit fullscreen mode

The code snippet above gets the memes from the previous function -getMemes(), loops through the array and sends them via Novu.

Congratulations! the memes should be displayed with your Discord channel, as shown below.

Automating


Check MEMEs every hour with Node Cron ⏰

Here, you'll learn how to schedule tasks with Node Cron - tiny task scheduler in pure JavaScript for Node.js based on GNU crontab.

Import the package into the index.js file.



const cron = require("node-cron");


Enter fullscreen mode Exit fullscreen mode

Then, call the sendChatMessage function using Node Cron.



//πŸ‘‡πŸ» schedules the task hourly
const hourlyTask = cron.schedule("0 * * * *", sendChatMessage);

console.log("Scheduled task to run every hour.");


Enter fullscreen mode Exit fullscreen mode

The code snippet above executes the sendChatMessage function every hour, enabling it to fetch the latest and random memes from the subreddit.

Congratulations on making it thus far!πŸŽ‰Β You can learn more about scheduling and automation with Node CronΒ here.


Final notes πŸ“œ

So far, you've learnt how to:

  • convert XML data to JSON type,
  • send chat messages with Novu,
  • automate tasks with Node Cron,
  • and build an automated Discord bot with Node.js.

Bots are computer programs designed to perform automated tasks or engage in conversations with people - and Discord bots are extremely helpful in tasks like moderation, custom commands, managing members' requests, and many more.

Today, if you have a Discord channel, you need a bot to help automate most of your moderation tasks.

The source code for this tutorial is available here:Β https://github.com/novuhq/blog/tree/main/meme-discord-bot-with-novu-reddit.

Thank you for reading!

Like

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .