Skip to content

Instantly share code, notes, and snippets.

@comhad
Last active December 19, 2022 13:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comhad/bd42b56a0399dbfa645aa83fe3cf4c8a to your computer and use it in GitHub Desktop.
Save comhad/bd42b56a0399dbfa645aa83fe3cf4c8a to your computer and use it in GitHub Desktop.
How to make discord bot listing webhooks without a dedicated server

Vote Webhooks

A vote webhook is a pre-defined URL that a POST request is made to containing information about a user that upvoted a bot (in JSON format) and an Authorization header. It's fired whenever a user upvotes a bot, it contains some various information about what user upvoted and some extra information like if this vote counted as double because it was the weekend, and if the user who upvoted was a site admin.

You may have issues with this soloution if you get over 500 upvotes per month, intergromat only allows so many operations to take place on a free account. Scroll down for details.

This tutorial is going to focus on discordbotlist for now, but by looking at the docs for other sites, you can find how the webhooks work (though you may need to create a second webhook on your side). This tutorial is going to use integromat to host webhooks. You can use github to make an account. (This tutorial assumes you have no experience with integromat)

Step 1. Making a integromat scenario for the webhook

Once you are logged into integromat, you will be at a URL like https://www.integromat.com/org/XXXXX. There should be a blue button in the top right saying Create a new scenario, click this and you will be taken to a page displaying several icons and a search bar. Search Webhooks and click the red icon with a triangle shape in the middle, then press continue in the top right. You should now see a mostly blank page with a question mark in the middle.

Step 2. Adding a webhook URL

Click the question mark, you should see a list containing Webhooks and Add another module, click Webhooks and pick Custom Webhook, that question mark should now be the red webhook icon. Click the webhook icon and you will see a blank URL box, click Add to the right of that, you will get a pop up, choose anyname for the webhook and leave the IP field blank and click save on the pop up. It will tell you it is now listening for data and provides a stop button and Copy address to clipboard button. Click the copy button. It should look like this : Reference image. (I've been stupid here and forgot to censor the URL before uploading, i had to delete it from intergromat, you shoud never share your webhook URL, not intergromat nor discord, if this happens you can delete them by going to the home menu and looking for webhooks on the left hand window)

Step 3. Testing your URL

(This assumes you are using discordbotlist.com, the steps will be similar for other sites.) Sign into discordbotlist.com, go to your bot, and press Edit, scroll down and you will see a webhook section. Paste the URL in the Upvote webhook box and paste anything in the Webhook secret box, and press test webhook. Reference image. After a few seconds, integromat should say the data structure was successfuly determined, this means it has worked. (If you don't see that, open up the console on the discordbotlist tab and try again, it should tell you why it isn't) Don't forget to click edit to save it afterwards.

Acting based on the data

Once a webhook has been delivered, you can perform actions with this data, just put your mouse over the right of the webhook icon so it highlights the little circle and click it, you'll see another question mark. Press add another module and search through to see what you would like to do, if it does not exist, you can use the HTTP module and use an API for the service.

Common use : Discord Webhooks

I haven't found any webhooks which are directly compatiable with discord webhooks, so my original use for this service was to send webhooks to discord, where my bot would pick them up and process them. I'm going to go over the basics of executing a discord webhook using the HTTP module, since I've been told the discord module sometimes does not work for webhooks.

Step 1 : Create a webhook and add it

First you must create a discord webhook, this can be done by going to Server Settings > Intergrations, depending on if you have webhooks here already, you may see New Webhook or View Webhooks, if you see View Webhooks, click that and New Webhook. Now you will be at a screen displaying a default profile picture, a name, and a channel, you can either choose the name and profile picture now, or have them set by integromat when it executes, the channel it posts into can not be changed by integromat. Click Copy webhook URL.

Step 2 : Post to the webhook using the HTTP module

Click on the right of the webhook icon, and click Add another module, search and pick HTTP, then Make a request. You will now have a blue HTTP icon linked to your webhook, click that icon to open the menu. Paste the discord webhook into the URL field, set the method to POST, set the body type to Raw. A Content-Type field should appear, select JSON( application/json) in the request body put :

{ "content" : "test" }

You have to click Ok to save it. Reference image. (You may or may not have that red circle near the HTTP icon, if you do this probably means you haven't saved it or filled in all the required fields, in that case, just go back over this step adding variables to the request and make sure to save) Right-click the HTTP module and click Run this module only. If this was set up correctly, a webhook should fire and send "test" to the channel you chose, this means the webhook is set up correctly.

Step 3 : Customize output

People have messaged me in the past when they've had some trouble with this and I've noticed that they are on mobile, this step requires you to click inside the textbox and then click the variables, if you are on mobile, it may be best to save this and come back to it later when you have access to a computer.

You can modify the message the webhook sends, you can change the content via changing the content in the JSON, when you click on this, you should see a window pop up beside it containing some various red buttons, these will differ based on what service you are using (example for this window is shown here). For discordbotlist, these will be id, username, discriminator, avatar and admin, you can click these buttons to insert them into it as variables that will be corresponding to the user who upvoted. Here is the basic webhook fields you can use, more here.

field purpose
content The contents of the actual message that gets sent over the webhook
username The username that the webhook appears as
avatar_url The avatar URL used for the webhook, can be an image hosted on discord or any other site (The avatar sent over webhook is not the same as a URL)
embeds It's possible to make the webhook display an embed, see the docs for more info

Example

To make the webhook use the users username as it's own name use your site logo, use this. (The parts in angle brackets represent variables)

{ "username" : "<1.name>", "content" : "Thanks!", "avatar_url" : "https://your.site/link/to/image.png" }

Reference image.

Tips

  • If you want to ping users using the webhook, you can use the id field, just put "<@" before the id variable and a ">" after it.
  • If you want to count the votes from webhooks using the bot, see counter.py
  • The avatar field is not the same as an URL, it is a hash and putting it into the URL avatar will not work, instead you can put https://cdn.discordapp.com/avatars/<1.id>/<1.avatar>.png into the avatar_url field instead, this works following these discord docs.

If this guide helped you please star this.

Limits

As stated, integromat only allows so many operations on a free account (1000 per month), if you follow everything in this guide, your scenario will take 2 operations, meaning you can have 500 upvotes per month routed through integromat before this becomes an issue (testing webhooks also counts as operations, provided there was testing in this tutorial, you will be down some upvotes).

Do not wait until your bot upvotes are over 500 per month before looking for another soloution, start looking for other soloutions at around 450. You can either get past these limits by either upgrading your integromat plan or investing in a server.

If you add more stuff to your scenario, your operation toll will be more, thus you will have less upvotes per month avaliable to you, you can see how many operations a scenario takes by going to the intergromat dashboard and clicking the History tab.

'''
- - - Upvote counter code snippet for discord.py bot - - -
This code snippet has been stripped down from a bot i use to use to
count user upvotes, this is not the full code for a bot but rather
a rough template of code to add to your own bot.
When a webhook is sent to discord, it comes in the format of a message
that users and bots can read, meaning it triggers the "on_message" in
discord.py. We can use this so whenever a webhook fires, the bot can
process it.
You will need a dedicated channel to webhooks that regular users can
not send to, else a user could figure out how this works and possibly
spam upvote records with any id they want.
Whenever a message arrives, the code checks if it comes from the
dedicated channel, if it does it splits it up and begins to process it.
I've added some regex here because my webhooks use to use pings, which
were user ids surrounded by characters i didn't want in the database,
the regex might not be required depending on what's in your webhook.
My webhook message was :
"<@XXXXXXXXXXXXXX> : Thank you for upvoting QuoteBot ! :D"
This meant i picked index 0, which would pick the ping, and then i
would put it through the regex to strip out everything till it was
just the user id number left, you may need to change the index
depending on what your chosen message is.
- - - Note - - -
This code assumes you are using the commands module and treats it as
such, if you are using a regular discord.py client instead, you may
need to make some changes to the on_message attribute and references
to the bot.
'''
import re
def addUpvoteRecord(userid) :
# Process the upvote and add it to the database
@bot.event
async def on_message(message) :
# This is called whenever a message is sent, possibly our webhook
if message.channel.id == XXXXXXXXXXXXXXXX : # Make sure that this is the dedicated channel id, in an integer format
data = message.content.split(" ") # Split the contents of the webhook message
user = re.sub("\D", "", data[0]) # Reducing a ping to just a user id, see above
# print(user)
# This is for debugging, make sure you choose the right index
addUpvoteRecord(user) # Call the method to add it to database or something
# . . .
# Do anything else here that you want to do
await bot.process_commands(message)
'''
If you are not already using on_message in your code and
instead the default commands you will need to add this so
the bot still knows to process the commands
'''
@varchasvkhare
Copy link

i want to restrict a command to voters only how can i do that please help me

@comhad
Copy link
Author

comhad commented Jul 29, 2021

i want to restrict a command to voters only how can i do that please help me

@varchasvkhare use counter.py and add your own code to the addUpvoteRecord function, this code can be storing the user id in something like a sqlite3 database (what i use) or a JSON file, there's good examples of how to manipulate JSON files here.

@varchasvkhare
Copy link

i want to restrict a command to voters only how can i do that please help me

@varchasvkhare use counter.py and add your own code to the addUpvoteRecord function, this code can be storing the user id in something like a sqlite3 database (what i use) or a JSON file, there's good examples of how to manipulate JSON files here.

since i am newbie can you help me if you are free i am unable to do it

@comhad
Copy link
Author

comhad commented Jul 29, 2021

i want to restrict a command to voters only how can i do that please help me

@varchasvkhare use counter.py and add your own code to the addUpvoteRecord function, this code can be storing the user id in something like a sqlite3 database (what i use) or a JSON file, there's good examples of how to manipulate JSON files here.

since i am newbie can you help me if you are free i am unable to do it

@varchasvkhare it's hard for me to help you given I can't see your code, but try making two functions, one to save a userid and one to retrieve it, once they work, add the save function to this code, and add the retrieve function to yours, and then when a user calls the command you want to restrict to people who have upvoted, use the retrieve function to check that they have upvoted the bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment