<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Savvy Insights]]></title><description><![CDATA[Savvy Insights]]></description><link>https://savvyinsights.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 12 Sep 2026 07:00:38 GMT</lastBuildDate><atom:link href="https://savvyinsights.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Start Building Your First Discord Bot with Discord.js v13]]></title><description><![CDATA[Hello, In this article, we are going to build a simple discord bot with nodejs. This tutorial is for beginners so if you are one of them then let's get started.
What you'll need

Node.js 16.6.0 or newer is required
A Discord account
Basic knowledge o...]]></description><link>https://savvyinsights.hashnode.dev/start-building-your-first-discord-bot-with-discordjs-v13</link><guid isPermaLink="true">https://savvyinsights.hashnode.dev/start-building-your-first-discord-bot-with-discordjs-v13</guid><category><![CDATA[Node.js]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Ayush Patil]]></dc:creator><pubDate>Wed, 18 Aug 2021 15:43:46 GMT</pubDate><content:encoded><![CDATA[<p>Hello, In this article, we are going to build a simple discord bot with nodejs. This tutorial is for beginners so if you are one of them then let's get started.</p>
<h3 id="what-youll-need">What you'll need</h3>
<ul>
<li>Node.js 16.6.0 or newer is required</li>
<li>A Discord account</li>
<li>Basic knowledge of javascript</li>
<li>Code editor of your choice</li>
<li>A discord server for testing</li>
</ul>
<h2 id="step-1-creating-the-bot">Step 1: Creating the bot</h2>
<p>Go to  <a target="_blank" href="https://discord.com/developers/applications/">Discord Developer Portal</a> and now you have to sign in to create a developer account. Once you did that click on "New Application" button and then give your bot a name, and click "Create"</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629291136094/Fzje8P2L3.png" alt="New Application" /></p>
<p>Once that is done, navigate to the "Bot" page and click on "Add Bot"</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629291660689/6NwgyVJni.png" alt="Bot page" /></p>
<p>Now, a section will appear 
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629292368099/M69Qs2dTZ.png" alt="Screenshot from 2021-08-18 18-42-26.png" /></p>
<p>Click on "Copy" to copy the token and save it for later</p>
<h2 id="step-2-adding-the-bot-to-your-server">Step 2: Adding the bot to your server</h2>
<p>Now, for adding the bot to your server navigate to "OAuth2" and select "Bot" and click on "Copy" at the bottom right corner 
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629292681071/ONb2ih2Nl.png" alt="Screenshot from 2021-08-18 18-47-30.png" /></p>
<p>After copying the url paste it in the browser. You will be redirected to a page </p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629293019003/aDL6RAU_U.png" alt="Screenshot from 2021-08-18 18-53-24.png" />
Just select a server and click on "Authorize"
If you follow every step then you will see a offline bot in your server. Let's bring it online</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629293312500/O6roaOIun.png" alt="Screenshot from 2021-08-18 18-58-20.png" /></p>
<h2 id="step-3-setting-up-your-project">Step 3: Setting up your project</h2>
<p>Create a new folder and name it whatever you want and open it in terminal and run the following command:</p>
<pre><code class="lang-bash">npm init
</code></pre>
<p>Once that is done, let's install required npm packages for this bot</p>
<pre><code class="lang-bash">npm install discord.js
</code></pre>
<h2 id="step-4-coding-the-bot">Step 4: Coding the bot</h2>
<p>Create a <code>index.js</code> file in the project folder<br /></p>
<p>In the <code>index.js</code> we will write a function which print something to in console when bot come online</p>
<pre><code class="lang-JavaScript"><span class="hljs-comment">// index.js</span>
<span class="hljs-keyword">const</span> { Client, Intents } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'discord.js'</span>);
<span class="hljs-built_in">require</span>(<span class="hljs-string">'dotenv'</span>).config();

<span class="hljs-keyword">const</span> client = <span class="hljs-keyword">new</span> Client({ <span class="hljs-attr">intents</span>: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

client.on(<span class="hljs-string">'ready'</span>, <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Logged in as <span class="hljs-subst">${client.user.tag}</span>!`</span>);
});

client.login(process.env.TOKEN); <span class="hljs-comment">// Paste the bot token here</span>
</code></pre>
<p>Also, don't forget to create a <code>.env</code> file and paste the bot token we copied earlier</p>
<pre><code class="lang-env">TOKEN='bot token'
</code></pre>
<p>Now you just have to run the following command and your bot will be online:</p>
<pre><code class="lang-bash">node index.js
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629299351803/GmP_y465M.png" alt="Screenshot from 2021-08-18 20-39-03.png" /></p>
<h2 id="step-5-creating-commands">Step 5: Creating Commands</h2>
<p>Let's start by creating a command which reply with <code>pong</code>, when we type <code>ping</code></p>
<pre><code><span class="hljs-selector-tag">client</span><span class="hljs-selector-class">.on</span>(<span class="hljs-string">'messageCreate'</span>, message =&gt; { <span class="hljs-comment">// This function will run when anyone sends a message in guild</span>
    <span class="hljs-selector-tag">if</span> (message.content == <span class="hljs-string">'ping'</span>) { <span class="hljs-comment">// reply with pong</span>
         <span class="hljs-selector-tag">message</span><span class="hljs-selector-class">.channel</span><span class="hljs-selector-class">.send</span>(<span class="hljs-string">'pong'</span>);
    }
    <span class="hljs-selector-tag">console</span><span class="hljs-selector-class">.log</span>(message.content)
})
</code></pre><p>Run the index.js file and go to any channel in the guild</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629300982441/gtm7M0Yb1v.png" alt="Screenshot from 2021-08-18 21-06-09.png" /></p>
<h2 id="conclusion">Conclusion</h2>
<p>You can create more cool things like music bot, moderation bot with discord.js. You can visit  <a target="_blank" href="https://discord.js.org/#/docs/main/stable/general/welcome">discord.js documentation</a> for info</p>
]]></content:encoded></item></channel></rss>