Skip to content

Man0fCulture/deploy-your-first-discord-blockchain-bot

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Starton Logo

Deploy your first blockchain bot using Starton

Introduction

This repo is related to an Epitech workshop led by Starton.
This workshop will allow you to build your first (or not) Discord bot connected to the blockchain using Starton (it's very easy you'll see).
During the 2 following hours, you'll develop a Discord bot in NodeJS thanks to what you can airdrop NFTs to the users of your Discord server.

What is Starton ?

Starton is a (wonderful 👀) start-up that provide developers an easy way to interact with the blockchain through their API.
With only few lines, you can automatically deploy a smart-contract, launch an NFT collection, follow the activity of Vitalik Buterin and so on, without think about the blockchain complexity (the nonces, the gas fees, etc).

Let's start !

Step 0 - Requirement & Configuration

Fork the repository

For the needs of the workshop, you'll need to fork the GitHub repository on your profile.
This will help us to mark you as present. Please, fork it as a public repository, or we could not access to it, and you'll be marked as absent.

Hint : https://docs.github.com/en/get-started/quickstart/fork-a-repo

Then, clone the repository on your local machine.

git clone [email protected]:starton-io/deploy-your-first-discord-blockchain-bot.git

Create a Starton account

To interact with the blockchain, you'll use the Starton API, so you'll need to create an account.

Go on app.starton.io and create your account (with your Epitech email address, maybe some gifts will be drop 👀).

Generate an API key that will be useful to use our API.

Hint : https://docs.starton.io/docs/getting-started

Register on Discord Developer Portal

As we will create a Discord Bot, you'll need to create new application on the Discord Developer Portal
Generate a new token that you'll put in your future application.

Hint : https://discordjs.guide/preparations/setting-up-a-bot-application.html#creating-your-bot

Step 1 - Initialization

Now that you cloned the repository, you need to initialize it. For this, we will use NodeJS with Yarn as package manager.

yarn init

Some packages are also required to use the Discord API and to send HTTP requests, you need to install them.

yarn add axios discord.js dotenv

axios is used to send request to any API
discord.js is used to use the Discord API
dotenv is used to store environment variable securely

You will then need to create two files :

  • The first one called starton.js
  • The second one called discord.js

You also have in the repository a file called .env.example. It looks like this :

## Put your Starton API key on the following line (it begins with 'sk_live_...')
STARTON_API_KEY=

## Put your Discord Bot Token on the following line
BOT_TOKEN=

Rename it as .env and store in the file :

  • Your Starton API key in the field STARTON_API_KEY
  • Your Discord Bot token in the field BOT_TOKEN

You can then configure dotenv in both files and display the right environment variables to see if they are imported as expected.

Hint 1 : https://www.npmjs.com/package/dotenv
Hint 2 : https://developer.mozilla.org/fr/docs/Web/API/console/log

You can run your scripts with :

## To run Starton script
node starton.js

## To run Discord script
node discord.js

Step 2 - IPFS

IPFS is a distributed storage system that works in peer-to-peer. This means that once a file is stored on IPFS, as the data is duplicated, the file (or the folder) can't be deleted, or should be deleted on every node of the network that hosts the file.

This system has been built to avoid the censor and to be sure that our data are persistent.

For example, if you have a Google account, your data are stored on the Google's servers. It means that if you are an enemy of Google, they can block your account as they want and also, if they have a technical issue (like some burning datacenters) you can lose your data.

This scenario is not possible with IPFS.

It's the perfect technology for us : we want the picture and the metadata of our NFT persistent.

For that, you will upload on IPFS thanks to the Starton dashboard, the file called starton-nft.png in the assets/ folder of this repository.
This picture will be the content of our NFT.
Then, get de CID (Content IDentifier) of your picture and put it in the starton-nft-metadata.json file.
Your file looks like this :

{
  "name": "Starton x Epitech NFT",
  "description": "This NFT has been minted for the Starton x Epitech Workshop",
  // PUT YOUR IMAGE CID ON THE FOLLOWING LINE
  "image": "ipfs://ipfs/YOUR_IMAGE_CID",
  "attributes": {
    "size": "42",
    "media": "Picture",
    "type": "NFT",
    "date": "March, 29 of 2023",
    "company": "Starton"
  }
}

Then, upload the starton-nft-metadata.json content on IPFS in the JSON section.
This will be the metadata of your NFT, in other words, all the information related to our NFT (the IPFS picture link, the NFT name, the NFT description, the type, the probability to get it, etc).

It's this CID that we will put in NFT parameter.

Hint : https://docs.starton.io/docs/IPFS/uploading-json

Step 3 - Deploy your first Smart-Contract

Congrats ! Your file is now uploaded on IPFS.

We will use the Starton Dashboard for this step too. 😉

You will here deploy your first smart-contract. The smart-contract will allow you to create your NFT collection thanks to the cryptocurrency you own on our wallet.

Obviously, you will need to create a Wallet on the Starton's KMS in the appropriate section ("Wallet" section).
Then, let's deploy a Smart Contract in the eponym section.

Select Deploy with template > ERC721 NFT Smart Contract.

  1. Give a Name to your Smart Contract
  2. Give it a Description
  3. Give it the name you want to appear on the Blockchain
  4. Give it the symbol (for example, ETH is the symbol of Ethereum)
  5. Then, the Base URI token is the type of storage you want to give, here as I told you before, we will use IPFS, so, write ipfs://ipfs/ in the text field
  6. In the Initial Contract URI, put the CID of the metadata you uploaded on IPFS
  7. And finally, set the Initial owner with the address of the wallet your just created

Next step, choose the network and the wallet :

  • Select Polygon Mumbai in the Network field.
  • Choose then your default wallet as the signer wallet. The signer wallet is the wallet that will pay the fees of the deployment of the NFT.

And... DEPLOY !

Here you are, you successfully deployed a NFT smart-contract... but you don't have NFT yet... Let's fix this !

Hint 1 : https://docs.starton.io/docs/Tutorials/deploy-Nfts-with-Binance
Hint 2 : https://docs.starton.io/docs/Smart-contract/ERC721-Meta

Step 4 - Mint NFT

Mint is the action to 'create' an NFT. When you mint an NFT, you have the choice to mint it on every address you want.
In our case, we want to mint the NFT on the wallet of our Discord users.

We will first mint an NFT using the Starton Dashboard to understand how it works and then using API.

Dashboard minting

So, check the Smart Contract section, click on the smart contract you just deployed and on the right, click on the Interact tab.
You will see lots of things appear on the left. Those things are all the function of the smart contract.
Using the search bar, write mint and then click on the concerned function.

Fill the fields with the following information :

  • Use your newly created wallet.
  • In the to field, copy and paste you wallet too (this is a test, after, it will be the user's wallet).
  • And in the URI field, put the CID of the metadata file you uploaded on IPFS.

Now that you created your first NFT, you can check if it is really visible on the blockchain, for that, visit one of the following link and search the address of your contract:

API minting

First NFT deployed : CHECK

It's time to automate it... with the Starton API.

Setup your starton.js file with axios, and try to find a way to mint directly with your script (look at the hints they are useful).

You can run your script with :

node starton.js

Hint 1 : https://docs.starton.io/docs/Developer/Discovering-coding-interface#code
Hint 2 (less interesting) : https://reflectoring.io/tutorial-guide-axios/

Step 5 - Detect Discord command

Okay good, you can mint lots and lots of NFTs on every wallet you want.

Let's take a look on the Discord side.

First, setup your Discord Bot instance.

Hint 1 : https://discordjs.guide/creating-your-bot/main-file.html#running-your-application

We want that when executing the following command in the chat, the Bot reply Hello World ! :

/airdrop

Hint 2 : https://discordjs.guide/creating-your-bot/slash-commands.html#before-you-continue

You can run your script with :

node discord.js

Step 6 - Get Discord command content

Well done ! You know how to interact with the Discord chat and make your users send commands to the bot.

What we really want is to get the wallet of the Discord user to mint an NFT on their address.

Here is a desired example :

/airdrop 0xA76ed24122193CF53f81F6dBEbE2a1DfF8f9e902

DISCLAIMER : This is a good way to test but it's not secure for production project. Metamask is a better way to do this but we won't use it now.

You can run your script with :

node discord.js

Hint : the word data

Step 7 - Link both and have fun

It's almost the end !

Now that you know how to mint an NFT on a wallet and that you know how to get the content of a Discord command (so... the wallet) you just have to link both to make magic happens...

And voilà ! All is working !

You should run the following final command :

node discord.js

Step ∞ - The only limit is your imagination

Now that you finished the workshop, be free to continue to use Starton, to develop projects and to build application using the (incredible) blockchain technology.

Documentation

You can access to the Starton Documentation here : Starton Documentation
If you need test faucet, you can get some on the official Polygon Faucet : Polygon Mumbai Faucet

Authors

Tibo Pendino - Backend Developer @starton.io (GitHub, LinkedIn)

About

Workshop bot blockchain

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%