🔥🤖 Automate MEMEs posting to your Discord with NodeJS and Novu 🚀🚀
Tags: webdev, javascript, programming, tutorial
Published: 2023-08-21 12:12:54

# 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/97uxr09zo8jxws5okhkv.gif)

<hr />

## Novu: Open-source notification infrastructure 🚀

Just a quick background about us. Novu is an open-source [notification infrastructure](https://novu.co). 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bojvpv96zk4noelozjjc.png)
](https://github.com/novuhq/novu)

<hr />

## Let's set it up 🔥

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

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

Install [Novu SDK](https://docs.novu.co/overview/quickstart/get-started-with-node.js), [Nodemon](https://www.npmjs.com/package/nodemon), [Node Cron](https://www.npmjs.com/package/node-cron), and [Fast XML Parser](https://github.com/NaturalIntelligence/fast-xml-parser).

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

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.

```bash
touch index.js
```

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.

```json
//👇🏻 In server/package.json

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

Congratulations!🎉 You can now start the server by using the command below.

```bash
npm start
```

<hr />

## Getting memes from Reddit 🤖

Copy the code below into the `index.js` file.

```jsx
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();
```

The code snippet above fetches the XML data (RSS feed) from the [ProgrammerHumor](https://www.reddit.com/r/ProgrammerHumor/.rss) subreddit, converts it to JSON format using the XMLParser, and retrieves only the memes from the large amount of data returned.

<hr />

## Reddit to Discord using Novu 🪄 ✨

Here, you'll learn how to send messages to your Discord channels with [Novu](https://github.com/novuhq/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k7ahws5rnutwxk4dnd0s.png)


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


![Webhook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zucg9kbfkg67oru3vxhf.png)


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


![Hook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nne5oq4dq6qmj4v3d1nf.png)

### Creating a Novu project

Run the code snippet below to create your Novu account.

```bash
npx novu init
```

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`.

```bash
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.
```

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


![Novu1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhm62vjziyt3b6o4qeva.png)

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

Finally, create a workflow that triggers the Chat notification.

![Novu2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s4c5385x7llluyf799vi.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e70piyg24z5qoncn1608.png)


### Sending messages to the Discord channel

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

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

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

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

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

```jsx
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();
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/98ytvtqk7xsxotonbmnp.gif)

<hr />

## Check MEMEs every hour with Node Cron ⏰

Here, you'll learn how to schedule tasks with [Node Cron](https://www.npmjs.com/package/node-cron) - tiny task scheduler in pure JavaScript for Node.js based on GNU crontab.

Import the package into the `index.js` file.

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

Then, call the `sendChatMessage` function using Node Cron.

```jsx
//👇🏻 schedules the task hourly
const hourlyTask = cron.schedule("0 * * * *", sendChatMessage);

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

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](https://github.com/node-cron/node-cron).

<hr />

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bojvpv96zk4noelozjjc.png)
](https://github.com/novuhq/novu)