In the event that you are in the Discord App you might have seen a considerable amount of Bot around whether its Banjo, Tatsumaki and so forth. You may think about how these bots be made, the answer is to write an application and run it on your local server or a public server to make it active. In this post, we will figure out how to make a discord bot utilizing Node.js.
On the off chance that you are not acquainted with Node.js, at that point its simple to begin with Node.js as there are such a large number of articles out there on web to kick you off. One such article I have composed on my blog Getting Started with Node.js. To get started we need to install Node.js from Nodejs.org. Once installed you can check the version of Node.js installed using command node -v in the Node.js Command Prompt.
Now its time to create a new Discord App. At first you need to visit Discord App Developer URL and create a new App. I have given my bot name as iamabot.
Once you do that you will get a User ID and a Token ID (Keep the Token ID Safe) and click on Public Bot (can be added on any server).
Our Bot has been created, now it's time to write some code in Node.js.
In the above code at first, we need to connect to Discord client using the token of the app. Now once it is connected it will emit an event and write it to the console that my bot is connected. Next, we will check which cryptocurrency the user has typed after the $price and get the price of that cryptocurrency using the coinmarketcap api.
Now run the index.js file using command node index.js as shown below:
https://discordapp.com/oauth2/authorize?&client_id=ClientId&scope=bot
When you open the link it will ask which server you want to add the bot too. Note that you can only add this bot to that server which you are managing, other servers will not be shown.
Once added you can see it on the right-hand side, now time to play with our new bot. See in the below pic, it's giving me the output.
In this way you can create a number of Discord Bot with minimal code. Happy Coding :)
Please like and share the CodingDefined.com blog if you find it useful and helpful.
On the off chance that you are not acquainted with Node.js, at that point its simple to begin with Node.js as there are such a large number of articles out there on web to kick you off. One such article I have composed on my blog Getting Started with Node.js. To get started we need to install Node.js from Nodejs.org. Once installed you can check the version of Node.js installed using command node -v in the Node.js Command Prompt.
Also Read an In-depth article : How to Make a Discord Bot: an Overview and Tutorial
Create new Discord App
Now its time to create a new Discord App. At first you need to visit Discord App Developer URL and create a new App. I have given my bot name as iamabot.
Next, since the bot will be operated as a user, you need to create it by clicking on Create a Bot User.
Once you do that you will get a User ID and a Token ID (Keep the Token ID Safe) and click on Public Bot (can be added on any server).
Our Bot has been created, now it's time to write some code in Node.js.
Node.js Application
At first, you need to create a package.json file. The content of the Package.json is shown below. We will be using discord.js node.js module to interact with Discord App.{ "name": "discordapp", "version": "1.0.0", "description": "A discord Bot", "main": "index.js", "scripts": { "start": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "discordie": "^0.11.0" } }The index.js file content is
var Discordie = require('discordie'); var request = require('request'); var Events = Discordie.Events; var client = new Discordie(); client.connect({ token: 'Your Bot Token' }) client.Dispatcher.on(Events.GATEWAY_READY, e => { console.log('Connected as: '+ client.User.username); }); client.Dispatcher.on(Events.MESSAGE_CREATE, e => { var content = e.message.content; if(content.indexOf("$price ") == 0) { var coin = content.replace("$price ", ""); var value = ''; try{ request('http://api.coinmarketcap.com/v1/ticker/' + coin + '/', function(error,res,body) { var obj = JSON.parse(body); console.log(obj[0]); if(obj[0] === undefined) { e.message.channel.sendMessage("You have entered a wrong id"); } else { value = coin.toUpperCase() + " : Current Price " + obj[0].price_usd + " | 24hr Percentage Change " + obj[0].percent_change_24h; e.message.channel.sendMessage(value); } }); } catch (err) { e.message.channel.sendMessage("Wrong ID, Have a Great Day"); } } });
In the above code at first, we need to connect to Discord client using the token of the app. Now once it is connected it will emit an event and write it to the console that my bot is connected. Next, we will check which cryptocurrency the user has typed after the $price and get the price of that cryptocurrency using the coinmarketcap api.
Now run the index.js file using command node index.js as shown below:
Add bot to the Server
Once Connected we need to add this bot to our server. For this, we need to take the Client ID of the bot and add it to the below urlhttps://discordapp.com/oauth2/authorize?&client_id=ClientId&scope=bot
When you open the link it will ask which server you want to add the bot too. Note that you can only add this bot to that server which you are managing, other servers will not be shown.
Once added you can see it on the right-hand side, now time to play with our new bot. See in the below pic, it's giving me the output.
In this way you can create a number of Discord Bot with minimal code. Happy Coding :)
Please like and share the CodingDefined.com blog if you find it useful and helpful.
Related articles
- How to create Simple Twitter Bot in Node.js
- Legacy Browser Support for using Babel
- Getting Started with Node.js Koa 2 Framework
- Web Scraping Libraries for Node.js Developers
- How to Create Simple RSS Reader in Ionic Application
- Simple RESTful API in Nodejs
- How to Get Latest Tweets of a particular HashTag using Nodejs
- How to Intercept HTTP requests in Node.js
- Node for Android
- Capture Screen of Web Pages through URL in Nodejs
No comments:
Post a Comment