Articles on: Community Tutorials

How to make a Voting Webhook (Node.js)

Welcome to SomethingHost!

In this guide, you will learn how to create a webhook using our subdomain system for your own Discord bot hosting package.

This webhook is set up for, but you will learn how to use it for other bot listing sites.

So how it works?

First, make sure you have already check out our subdomain guide which explains how to use a port to create an express app. To get started, we will need to install body-parser which helps your bot read the data from the webhooks. Put this code where you set up your express app.
const bodyParser = require("body-parser")

The way webhooks work is that they send a POST request to another URL. To be more clear, it is one application sending data to another. Here we will create a page to receive webhooks, you could have all your webhooks go to this one page, or create multiple pages for different webhooks. It is recommended just to use one. The following code will receive POST requests at"/votes", function (request, response) {
  response.sendStatus(200) // tells the website that sent the POST request that everything was OK
  console.log(request.body) // log the actual data being received

When creating a webhook for other sites, test out the request.body to check how to properly get information because every webhook sends different style data.

If you would like, you can test out your webhook using ReqBin. Put your full URL in the text box and change GET to POST. In the Content tab, you can use JSON data. For example, {"key":"value"}. Once you enter in your content, simply cick Send and it will send the content to your console (request.body).

Now we have to create the webhook from the site we are using. Here we will be using Go ahead and navigate to your Webhooks tab for your bot.

In the URL, you will want to use your site's URL without the port and ending with what you use in your app.get method. Example:

In the authorization box, you can type whatever you'd like. You will use that authorization token to verify that the webhook is coming from the source you want it to. It is recommended to make this token secure. Don't share it with anyone!

Going back to your webhook code, we must create a check and use the authorization token you created above. Every POST request sends a headers object, you can view this by typing console.log(request.headers) inside your app.get function. The following code will go inside your function. If you have multiple webhooks, you'll want to use repeated else if statements. Make sure to end with else return.
let auth = request.headers.authorization
if (auth === "authorization from webhook") {

} else return;

Every webhook that you use for bot listing sites will send the user ID in the request.body. For, you can get the voter's Discord ID by using request.body.user. Make sure to change your code if your client is something else or if you store users differently. Put the following code inside your authorization check.

let user = bot.users.cache.get(request.body.user)

Now that you have the user, you can do a variety of things with it! Rewards for your bot are a good way to get new servers. When creating more webhooks, make sure to check the request.body and create different authorization tokens for every one of them.

Updated on: 28/12/2022

Was this article helpful?

Share your feedback


Thank you!