With regards to crypto communities, Discord is an especially widespread platform. Therefore, we need to present you methods to construct a Discord bot for on-chain occasions, and due to the Moralis Streams API, you’ll be able to construct a crypto monitor Discord bot effortlessly. All it takes is a free Moralis account, a Discord server, and a few easy JavaScript programming. Furthermore, constructing a Discord bot is lots less complicated than you may suppose, and on this article, we’re going to perform this in 5 easy steps:
- Setting Up a Easy NodeJS Categorical Backend
- Including a New Moralis Stream
- Verifying the Webhook Sender
- Setting Up a Discord Bot
- Implementing an Instance Blockchain Discord Bot
Moreover, it shouldn’t take greater than twenty minutes to set issues up. Additionally, because of Moralis’ cross-chain interoperability, the perfect factor about our Discord bot is you can hearken to any on-chain occasion on Ethereum and different EVM-compatible chains. However, for the sake of this tutorial, we’ll concentrate on the Polygon testnet (Mumbai).
Shifting ahead, we’ll first be sure that you all know what a Discord bot is. Then, we’ll dive into immediately’s tutorial. Nevertheless, earlier than we present you methods to construct a Discord bot, we’ll check out a demo, which is able to enable you higher perceive what a crypto monitor Discord bot is. As well as, by seeing our demonstration, you’ll additionally be capable of determine whether or not or not you need to concentrate on constructing a Discord bot to your personal functions. With that stated, if you happen to already know what a Discord bot is, be happy to dive proper into our blockchain Discord bot tutorial.

What’s a Discord Bot?
If you happen to’ve accomplished any on-line purchasing, you’ve in all probability encountered chatbots. These bots are packages that assist automate sure processes. Furthermore, when these kinds of AI-driven instruments are used to automate duties on Discord servers, they’re known as Discord bots. As chances are you’ll know, Discord servers supply many options, a lot of which might be automated.
So, what precisely is a blockchain Discord bot? It’s a Discord bot that triggers a particular motion on a particular Discord server when a specific on-chain occasion (the one which the bot listens to) happens. Therefore, a crypto monitor Discord bot is a really sensible instrument for all kinds of crypto Discord communities. In any case, it will probably add a number of worth to personal and public Discord channels.
It’s additionally value mentioning that the Discord bot function is obtainable on Discord’s developer portal. As such, you don’t should do any severe coding to create Discord bots. As an alternative, you’ll be able to merely choose among the many given choices and use Discord’s pre-built options. You’ll get to see this, as we present you methods to construct a Discord bot within the part under.
The best way to Construct a Discord Bot for Blockchain Occasions
Constructing a Discord bot for crypto is easy, because of Moralis’ Streams API. This highly effective instrument lets you stream blockchain knowledge into your backend (on this case, Discord’s backend) by way of webhooks. To entry this highly effective instrument, you solely want a free Moralis account. In fact, you additionally have to determine what on-chain occasions curiosity you. This may decide whether or not you’ll be able to hearken to an current good contract or if you happen to’ll have to create and deploy your good contract first. Nevertheless, for the sake of this tutorial, we’ll use an current good contract. In truth, to keep away from any confusion, begin by specializing in our instance Web3 contract.
Additionally, to construct your personal crypto monitor Discord bot following our lead, you will want the next instruments:
- A Moralis account, which you’ll create without spending a dime!
- Some kind of code editor or IDE. We’ll be utilizing Visible Studio Code (VSC).
- A Discord server and entry to Discord’s developer portal.
- A blockchain explorer. Our instance Web3 contract is on the Mumbai community, so we’ll be utilizing PolygonScan.
- The Postman platform (optionally available). It is a instrument that can assist you make API calls to completely different posts, get endpoints, and so forth.
Furthermore, we’ll take you thru the next steps:
- Setting Up a Easy NodeJS Categorical Backend
- Including a New Moralis Stream
- Verifying the Webhook Sender
- It’s essential to be sure that solely Moralis could make a webhook put up.
- Setting Up a Discord Bot
- Implementing an Instance Blockchain Discord Bot
- Sending messages with Discord.js
Now, as talked about above, earlier than we present you methods to construct a Discord bot, let’s check out an illustration.
Our Crypto Monitor Discord Bot – Demo
So, that is our instance Discord server, which we created for the sake of this tutorial:
Trying on the above screenshot, you’ll be able to see our crypto monitor Discord bot (“DonationBot”) in the precise sidebar. Subsequent, we use PolygonScan to work together with our instance “donationExample” good contract. Clearly, that is the good contract that our instance blockchain Discord bot listens to.
The above picture exhibits you that by clicking on the “Write” button on PolygonScan’s web page for our instance Web3 contract, we really execute an on-chain transaction utilizing MetaMask. Additionally, as you’ll be able to see above, we donated six MATIC tokens. Lastly, as quickly as our transaction goes via, our Discord bot posts a message informing our Discord members about this donation:
As you’ll be able to see, the message contains the donor’s tackle and the donated quantity. Nevertheless, the important thing level is that the on-chain occasion (a donation transaction) triggered our Discord bot.
If you happen to discover this function helpful, make certain to comply with our lead within the upcoming sections, the place we’ll cowl constructing a Discord bot that listens to on-chain occasions (just like the one introduced on this demo).
Setting Up a Easy NodeJS Categorical Backend
If you wish to discover ways to construct a Discord bot for on-chain occasions the simple means, begin with a easy NodeJS Categorical dapp. You will discover the starter code in our GitHub repo for this tutorial. Additionally, be sure you have “ngrok” put in to create a “ngrok” tunnel to your Categorical dapp. To do that, open a brand new terminal and enter the next command:
ngrok http 3000
Observe: In case you’re utilizing a unique port, make certain to switch “3000” accordingly.
Because of working the above command, you’re going to get an tackle the place your Categorical dapp will probably be working:
Subsequent, you need to set up all of the dependencies with this command:
npm i categorical moralis discord.js dotenv
Furthermore, we suggest that you just additionally set up “nodemon” so that you just’ll be capable of view modifications while not having to cease and restart your server. Therefore, additionally use the command under:
npm i nodemon
With the dependencies in place, open your “bundle.json” file and add the “begin” script for “nodemon“:
Lastly, you’re able to run your dapp utilizing the command under:
npm run begin
Including a New Moralis Stream
Along with your dapp working, it’s time to create a brand new Moralis Stream. To do that, you want your Moralis account. So, in case you haven’t created your account but, accomplish that now. Utilizing your credentials, you’ll entry your admin space. From there, you’ll be capable of go to the “Streams” web page, the place you want to click on on the “New Stream” button:
Then, choose the “Create From Scratch” possibility:
Subsequent, you want to enter a number of particulars associated to the good contract you need to hearken to. That is a necessary step of the “methods to construct a Discord bot” course of. Furthermore, sooner or later, you’ll need to use your personal good contract or different already deployed contracts. Nevertheless, for the sake of this tutorial, we suggest you employ our instance good contract (the “exampleDonation” contract).
Acquiring and Getting into Good Contract Particulars
By specializing in the “exampleDonation” contract, go to PolygonScan and duplicate this good contract’s tackle:
Then, return to your “Add new Stream” window and paste the above-copied tackle into the designated spot:
Shifting on, scroll down so as to add your new Moralis Stream’s description. This may be no matter you need; nevertheless, to keep away from any pointless confusion, you should use “New Donation”:
Subsequent, paste your “ngrok“ tackle within the “Webhook URL” entry discipline and add “/webhook” on the finish:
So far as the tag goes, you’ll be able to go along with “NewDonation”:
With regards to deciding on networks, you want to be sure you choose the community on which the good contract you need to hearken to is. If you happen to keep in mind, the “exampleDonation” contract is on Polygon’s testnet. Thus, make certain to pick out the “Polygon Mumbai” possibility:
Then, you need to choose the “Native Transactions” possibility (donations are made in MATIC):
Moreover, make certain to activate “Occasion Emittance”:
Subsequent, return to PolygonScan to repeat the good contract’s ABI:
By pasting the copied ABI within the designated space, you’ll get to pick out the occasion of this good contract:
Lastly, create your new Moralis Stream by clicking on the “Create Stream” button within the bottom-right nook. Consequently, it’s best to get the “New Stream Created” success message. You also needs to be capable of see your Moralis Stream on the backside of the “Stream” web page:
Now you can check your Moralis Stream. To see precisely how to try this, use the video on the backside of the article, beginning at 5:36.
Verifying Webhook Sender
With the present “index.js” code (as offered within the “starter.js” file”), anybody along with your webhook tackle could make a put up request. As such, you have to implement the required tweaks to confirm the webhook sender. First, import Moralis on the prime of your “index.js” file:
const Moralis = require("moralis").default;
Then, initialize an occasion of Moralis by including the next strains of code:
Moralis.begin({ apiKey: course of.env.APIKEY, })
Trying on the strains of code above, you see that you just want your Moralis Web3 API key. It’s essential to copy it out of your Moralis admin space:
Subsequent, return to VSC, create a brand new file (“.env”), and paste your Web3 key subsequent to “APIKEY”:
Along with your Web3 API key in place, return to the “index.js” file and require “dotenv” on the prime:
require("dotenv").config();
Now, you need to ensure that your dapp begins solely after beginning Moralis by placing “app.pay attention” inside “then”:
Moralis.begin({ apiKey: course of.env.APIKEY, }).then(() => { app.pay attention(port, () => { console.log(`Listening to streams`); }); });
Then, you additionally need to take a look at “headers” and add Moralis’ “verifySignature”:
Observe: We encourage you to check your present progress by following our in-house skilled’s lead (video under at 10:20). That is the place you’ll use Postman.
Setting Up a Discord Bot
Demonstrating methods to construct a Discord bot wouldn’t be full with out us displaying you methods to arrange a Discord bot. So, begin by going to your Discord account. There, create a brand new server (“Blockchain Notify”). If you happen to’ve by no means created a Discord server earlier than, make certain to make use of the video under (11:30).
After you have your server prepared, it’s time so as to add a brand new Discord bot to that server. To do this, go to Discord’s developer portal and register. Then go to the “Purposes” web page and click on on the “New Utility” button:
Subsequent, title your software, test the field, and hit the “Create” button:
You too can add an icon to your software:
Then, you need to choose the “Bot” possibility, click on on the “Add Bot” button, and ensure with a click on on “Sure, do it!”:
Shifting ahead, you need to increase “OAuth2” and click on on the “URL Generator” possibility:
Subsequent, as indicated within the picture above, test the “bot” scope. Then, scroll down and test the “Ship Message” permission:
As proven within the above screenshot, copy the generated URL. By pasting that URL into your browser, you’ll be capable of choose a server to which you need to add that bot:
To finalize this step of constructing a Discord bot for crypto, you’ll additionally have to click on on the “Authorize” button and ensure that you’re human:
Now, you’ll be able to shut that tab and go to your Discord. If you happen to’ve adopted our directions, it’s best to now have the “DonationBot” bot in your “Blockchain Notify” Discord server:
Implementing an Instance Blockchain Discord Bot – Sending Messages with Discord.js
On this last step of our “methods to construct a Discord bot” quest, you want to add the required strains of code to make sure that the above-created bot turns into a crypto monitor Discord bot. Therefore, return to your “index.js” script and import “discord.js” on the prime:
const discord = require("discord.js"); Subsequent, create a Discord shopper just under the imports: const shopper = new discord.Consumer({ intents: [], }); shopper.login(course of.env.PASS);
Trying on the backside line of the code above, you’ll be able to see that you want to add the “PASS” variable to your “.env” file. To get this variable, return to your “DonationBot” software (the “Bot” possibility) and click on on the “Reset Token” button. Then copy the token:
Shifting on, go to your “.env” file, create the “PASS” variable, and paste within the above-copied token:
Subsequent, go to your Discord server, and duplicate your channel ID (right-click on the channel through which you need your bot to put up messages):
Return to the “.env” file and create a brand new variable that can retailer the above-copied ID:
Along with your variables in place, you’ll be able to add the required strains of code inside “strive”:
let from = physique.txs[0].fromAddress; let quantity = Quantity(physique.txs[0].worth / 1E18); const channel = await shopper.channels.fetch(course of.env.CHANNEL); channel.ship(`New Donation submitted by ${from}, for ${quantity.toFixed(2)} MATIC!!!!`);
Observe: You’ll be able to entry the last code on GitHub.
With the above strains of code in place, you’ve efficiently accomplished all 5 steps concerned within the “methods to construct a Discord bot” implementation. Now, make certain to take your bot for a spin. For steerage, use the video under (17:34).
Final however not least, right here’s the video model of our “methods to construct a Discord bot” tutorial we’ve been referencing all through the article:
Blockchain Discord Bot – Construct a Discord Bot for On-Chain Occasions – Abstract
We lined fairly a distance in immediately’s article. First, you had an opportunity to be taught what a Discord bot is. We additionally made certain you realize what a crypto monitor Discord bot is. With the fundamentals below your belt, you had a possibility to comply with our lead. Furthermore, by taking you thru the 5 main steps, we offered you with all the small print associated to constructing a Discord bot. These 5 steps included:
- Setting Up a Easy NodeJS Categorical Backend
- Including a New Moralis Stream
- Verifying the Webhook Sender
- Setting Up a Discord Bot
- Implementing an Instance Blockchain Discord Bot
Nonetheless, we additionally shared a video tutorial containing further particulars about constructing a Discord bot for on-chain occasions.
We advise you employ the “methods to construct a Discord bot” data obtained above to construct your personal crypto monitor Discord bot for one more good contract. That means, you’ll correctly solidify your data. Alternatively, chances are you’ll need to discover different blockchain growth matters and create all kinds of dapps. If that’s the case, it’s best to use the Moralis docs, the Moralis YouTube channel, and the Moralis weblog. In any case, these retailers have the facility that can assist you develop into a Web3 developer without spending a dime. For instance, a few of our newest matters present you methods to program good contracts on Solana, add recordsdata to IPFS, mint a coin on Solana, methods to create an NFT venture, and rather more.
In fact, you may as well take a extra skilled method to your crypto training. By enrolling in Moralis Academy, you’ll be able to develop into blockchain licensed and considerably enhance your possibilities of touchdown a crypto job. To create a correct basis, we encourage you to start out with the course specializing in blockchain and Bitcoin fundamentals.