Jump to content

How to Pull an API into a Discord Bot Nickname [nodejs]


Recommended Posts

A basic example of a Discord.js bot that can autoupdate every 5 seconds based on an API using fetch.

image.png.3daca5cedb4c5debad6e0dee3148840b.png

Install nodejs and some packages. Linux x64 or ARM, example below is 64-bit.

wget https://nodejs.org/dist/v16.0.0/node-v16.0.0-linux-x64.tar.xz
tar xvf node-v16.0.0-linux-x64.tar.xz
cd node-v16.0.0-linux-x64/
sudo cp -R bin/* /usr/bin/
sudo cp -R lib/* /usr/lib/
npm install discord.js
npm install node-fetch

blockheight.js https://gist.github.com/buzzkillb/63a66f27ff65c6dd880a6c3eae740602

//npm install discord.js node-fetch
//get blockheight from Chainz CryptoID and update Bots Name as Blockheight
const Discord = require('discord.js');
const fetch = require('node-fetch');
const config = require("./config.json");

var chainzApi = "https://chainz.cryptoid.info/d/api.dws?q=getblockcount"

const client = new Discord.Client();

client.on('ready', async () => {
  client.user.setActivity('denarius.io');
  const GUILD_ID = client.guilds.cache.map(guild => guild.id);
  const guild = await client.guilds.fetch(GUILD_ID);
  console.log('Bot is connected...');


setInterval(async function(){

fetch(chainzApi)
        .then(function (response) {
                // Get a JSON object from the response
                // This is a weird quirk of Fetch
                return response.json();
        }).then(function (data) {

                // Log the data to the console, block height
                console.log(data);
                var blockHeight = "Height: " + data
                guild.me.setNickname(blockHeight);
        })

}, 5000); 


});

client.login(config.BOT_TOKEN);

config.json

{
        "BOT_TOKEN": "DISCORDGENERATEDTOKENGOESHERE"
}

How to add the bot to a server and get your BOT_TOKEN

Authorizing Discord User

  • Visit https://discord.com/developers/applications and create 'New Application'.
  • Set a bot name like BlockHead
  • On the page that follows, set the account name and save. Then click Bot
  • Create a Bot account and Save. Click 'Copy' on the Token; this is the API Key you use in the bot script
  • Visit the OAuth2 tab. Under Scopes, select 'bot'
  • The resulting URL is what you (or anyone) use to add your bot instance to a server.

If you enjoy my content please consider donating to the Denarius creator - https://www.patreon.com/carsenk
Join Denarius Discord - https://discord.gg/JQEmXwb

Link to comment
Share on other sites

To add some info to underneath the name try replacing these lines

setInterval(async function(){

fetch(chainzApi)
        .then(function (response) {
                // Get a JSON object from the response
                // This is a weird quirk of Fetch
                return response.json();
        }).then(function (data) {

                // Log the data to the console, Total balance of Token
                console.log(data);
                var blockHeight = "Height: " + data
                guild.me.setNickname(blockHeight);

                var coinexplorerFS = "https://www.coinexplorer.net/api/v1/D/masternode/count "
                return fetch(coinexplorerFS);
        }).then(function (response) {
                // Get a JSON object from the response
                return response.json();
        }).then(function (data) {

                var fsCount = "FS " + data.result.total
                client.user.setActivity(fsCount);

        })



}, 3000);

image.png.1f5b846a925124167104734014612c7b.png

If you enjoy my content please consider donating to the Denarius creator - https://www.patreon.com/carsenk
Join Denarius Discord - https://discord.gg/JQEmXwb

Link to comment
Share on other sites

How to pull Binance API with USDT as the price and BTC as the playing part underneath, example for FTM.

image.png.d73ce827ce4fd12b7623c6b34aeb68ca.png

const Discord = require('discord.js');
const fetch = require('node-fetch');
const config = require("./config.json");

var binanceApi = "https://api.binance.com/api/v3/ticker/price?symbol=FTMUSDT"
var binanceApiBTC = "https://api.binance.com/api/v3/ticker/price?symbol=FTMBTC"

let refreshint = 5000;

function sleep(ms) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}

const client = new Discord.Client();

client.on('ready', async () => {
    client.user.setActivity('binanceUSDT');
    const GUILD_ID = client.guilds.cache.map(guild => guild.id);
    const guild = await client.guilds.fetch(GUILD_ID[0]);
    const guildt = await client.guilds.fetch(GUILD_ID[1]);
    console.log('Bot is connected...');

    setInterval(async function(){
        //Fetch the binanceApi FTM/USDT
        fetch(binanceApi)
            .then(function (data) {
                if (data.status !== 200) {
                    console.log('Looks like there was a FTM problem. Status Code: ', data.status);
                    return;
                }
                data.json().then(function (data) {
                    // Log the data to the console, Total balance of Token
                    console.log(data.price);
                    var coinPrice = "FTM $" + data.price
                    guild.me.setNickname(coinPrice);
                    guildt.me.setNickname(coinPrice);

                    // var binanceApiBtc = "https://api.binance.com/api/v3/ticker/price?symbol=FTMBTC"
                    // return fetch(binanceApiBtc);
                });
                }
            )
            .catch(function (e) {
                console.log("Error pulling FTM price data:", e);
            })

        //Fetch the binanceApi FTM/BTC
        fetch(binanceApiBTC)
            .then(function (data) {
                if (data.status !== 200) {
                    console.log('Looks like there was a BTC problem. Status Code: ', data.status);
                    return;
                }
                data.json().then(function (data) {
                    // Log the data to the console, Total balance of Token
                    var coinBtc = "BTC " + data.price
                    client.user.setActivity(coinBtc);
                    console.log(coinBtc);
                });
                }
            )
            .catch(function (e) {
                console.log("Error pulling BTC price data:", e);
            })

    }, refreshint)

});

client.login(config.BOT_TOKEN);

 

If you enjoy my content please consider donating to the Denarius creator - https://www.patreon.com/carsenk
Join Denarius Discord - https://discord.gg/JQEmXwb

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...