PageSourceSearch

https://discordjs-guide.netlify.app/assets/main-file.html.a2a17027.js

js discordjs-guide.netlify.app collected 2026-10-03 10:39:47 UTC 7,264 bytes, 17 lines download raw bytes

1import{r as e,o as i,c as r,a as s,b as a,w as o,F as l,d as n,f as p}from"./app.1a0d0096.js";const u={},d=s("h1",{id:"creating-the-main-file",tabindex:"-1"},[s("a",{class:"header-anchor",href:"#creating-the-main-file","aria-hidden":"true"},"#"),n(" Creating the main file")],-1),h={class:"custom-container tip"},k=s("p",{class:"custom-container-title"},"TIP",-1),m=n("This page assumes you've already prepared the "),g=n("configuration files"),b=n(" from the previous page. We're using the "),y=s("code",null,"config.json",-1),f=n(" approach, however feel free to substitute your own!"),_=p(`<p>Open your code editor and create a new file. We suggest that you save the file as <code>index.js</code>, but you may name it whatever you wish.</p><p>Here&#39;s the base code to get you started:</p><div class="language-javascript ext-js line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// Require the necessary discord.js classes</span>
2<span class="token keyword">const</span> <span class="token punctuation">{</span> Client<span class="token punctuation">,</span> Events<span class="token punctuation">,</span> GatewayIntentBits <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&#39;discord.js&#39;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
3<span class="token keyword">const</span> <span class="token punctuation">{</span> token <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&#39;./config.json&#39;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
4
5<span class="token comment">// Create a new client instance</span>
6<span class="token keyword">const</span> client <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Client</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">intents</span><span class="token operator">:</span> <span class="token punctuation">[</span>GatewayIntentBits<span class="token punctuation">.</span>Guilds<span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
7
8<span class="token comment">// When the client is ready, run this code (only once).</span>
9<span class="token comment">// The distinction between \`client: Client&lt;boolean&gt;\` and \`readyClient: Client&lt;true&gt;\` is important for TypeScript developers.</span>
10<span class="token comment">// It makes some properties non-nullable.</span>
11client<span class="token punctuation">.</span><span class="token function">once</span><span class="token punctuation">(</span>Events<span class="token punctuation">.</span>ClientReady<span class="token punctuation">,</span> <span class="token parameter">readyClient</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
12	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">\`</span><span class="token string">Ready! Logged in as </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">\${</span>readyClient<span class="token punctuation">.</span>user<span class="token punctuation">.</span>tag<span class="token interpolation-punctuation punctuation">}</span></span><span class="token template-punctuation string">\`</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
13<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
14
15<span class="token comment">// Log in to Discord with your client&#39;s token</span>
16client<span class="token punctuation">.</span><span class="token function">login</span><span class="token punctuation">(</span>token<span class="token punctuation">)</span><span class="token punctuation">;</span>
17</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>This is how you create a client instance for your Discord bot and log in to Discord. The <code>GatewayIntentBits.Guilds</code> intents option is necessary for the discord.js client to work as you expect it to, as it ensures that the caches for guilds, channels, and roles are populated and available for internal use.</p><div class="custom-container tip"><p class="custom-container-title">TIP</p><p>The term &quot;guild&quot; is used by the Discord API and in discord.js to refer to a Discord server.</p></div><p>Intents also define which events Discord should send to your bot, and you may wish to enable more than just the minimum. You can read more about the other intents on the <a href="/popular-topics/intents">Intents topic</a>.</p><h2 id="running-your-application" tabindex="-1"><a class="header-anchor" href="#running-your-application" aria-hidden="true">
17#</a> Running your application</h2>`,7),w=n("Open your terminal and run "),v=s("code",null,"node index.js",-1),C=n(` to start the process. If you see "Ready!" after a few seconds, you're good to go! The next step is to start adding `),j=n("slash commands"),x=n(" to develop your bot's functionality."),I=p('<div class="custom-container tip"><p class="custom-container-title">TIP</p><p>You can open your <code>package.json</code> file and edit the <code>&quot;main&quot;: &quot;index.js&quot;</code> field to point to your main file. You can then run <code>node .</code> in your terminal to start the process!</p><p>After closing the process with <code>Ctrl + C</code>, you can press the up arrow on your keyboard to bring up the latest commands you&#39;ve run. Pressing up and then enter after closing the process is a quick way to start it up again.</p></div><h4 id="resulting-code" tabindex="-1"><a class="header-anchor" href="#resulting-code" aria-hidden="true">#</a> Resulting code</h4>',2);function T(q,R){const t=e("RouterLink"),c=e("ResultingCode");return i(),r(l,null,[d,s("div",h,[k,s("p",null,[m,a(t,{to:"/creating-your-bot/#creating-configuration-files"},{default:o(()=>[g]),_:1}),b,y,f])]),_,s("p",null,[w,v,C,a(t,{to:"/creating-your-bot/slash-commands.html"},{default:o(()=>[j]),_:1}),x]),I,a(c,{path:"creating-your-bot/initial-files"})],64)}u.render=T;export{u as default};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.