<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jodie E</title>
    <description>The latest articles on DEV Community by Jodie E (@jodiee).</description>
    <link>https://dev.to/jodiee</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F721116%2Ff0ea45ed-0b2b-4c03-8ed5-d414d91d8f06.png</url>
      <title>DEV Community: Jodie E</title>
      <link>https://dev.to/jodiee</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jodiee"/>
    <language>en</language>
    <item>
      <title>How To Create a Simple Bot on Discord</title>
      <dc:creator>Jodie E</dc:creator>
      <pubDate>Thu, 25 Nov 2021 16:16:53 +0000</pubDate>
      <link>https://dev.to/jodiee/how-to-create-a-simple-bot-on-discord-4l11</link>
      <guid>https://dev.to/jodiee/how-to-create-a-simple-bot-on-discord-4l11</guid>
      <description>&lt;p&gt;This tutorial will show you how to set up a simple bot using webhooks on a Discord channel. The bot will post a message to a channel. I recommend setting up your own personal server for testing purposes, before you unleash it on the world.&lt;/p&gt;

&lt;h2&gt;
  
  
  CREATE A BOT ON DISCORD
&lt;/h2&gt;

&lt;p&gt;Follow this &lt;a href="https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks" rel="noopener noreferrer"&gt;how-to and intro to webhooks&lt;/a&gt;, to the point where you get the webhook URL. You can give it a fun name and avatar. Have the webhook URL handy, so you can use it later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do not do &lt;em&gt;Quick Example: GitHub Webhook Integration&lt;/em&gt;, it is not part of this tutorial&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdndhxw7ei%2Fimage%2Fupload%2Fv1637698051%2Fblog%2Fdiscordbot%2FUntitled_d0rsmw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdndhxw7ei%2Fimage%2Fupload%2Fv1637698051%2Fblog%2Fdiscordbot%2FUntitled_d0rsmw.png" alt="Discord "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your bot is ready and waiting for input!&lt;/p&gt;

&lt;h2&gt;
  
  
  SET UP THE BOT
&lt;/h2&gt;

&lt;p&gt;Make sure you have Node.js installed on your computer so you can run your script in the terminal. You can check if it's installed by checking the version number in your terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you don't have node, you can get it &lt;a href="https://nodejs.org/en/download/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;sendmessage.js&lt;/code&gt; file with a console.log function for testing in the terminal. We'll build on this function as we go forward.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sendmessage.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sendMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run file by executing &lt;code&gt;node sendmessage.js&lt;/code&gt; in the terminal. This should print "Hello" on the next line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node sendmessage.js
Hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great! Your code is working and node is properly installed. Now you'll want to get the project started.&lt;/p&gt;

&lt;p&gt;Initialize your project using &lt;strong&gt;yarn&lt;/strong&gt; or &lt;strong&gt;npm&lt;/strong&gt;. They work similarly, but we'll be using &lt;strong&gt;npm&lt;/strong&gt; for this tutorial, because it comes bundled with node. Start it up by keying the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a package.json file. That's the library 'ingredient list' for your program. It keeps track of rules and dependencies that people will use to run your pgoram.&lt;/p&gt;

&lt;p&gt;You will be prompted to fill in various fields at this point. Unless you want to link it to github or customize with your information, you can accept the defaults (i.e. press 'Enter/return' for each). This basic tutorial doesn't cover any of this, so we are accepting the defaults.&lt;/p&gt;

&lt;p&gt;Now you'll want to get &lt;a href="https://www.npmjs.com/package/axios" rel="noopener noreferrer"&gt;axios&lt;/a&gt;, so you can simplify sending HTTP requests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great! Now you have your ingredient list and ingredients. We are ready to put things together.&lt;/p&gt;

&lt;h2&gt;
  
  
  MAKE YOUR BOT TALK
&lt;/h2&gt;

&lt;p&gt;We will be making an HTTP POST to our webhook URL. First, import axios at the top of your .js file. This links the axios library to your program, so you can use all of the perks you installed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure this is the first line of your code.&lt;/p&gt;

&lt;p&gt;Next, create a couple of variables to hold the data you are sending. Discord requires an object with a content key, which is why we've got the text written in &lt;code&gt;{key:value}&lt;/code&gt; format. If you tried to send it as a &lt;code&gt;string&lt;/code&gt;, it would be interpreted as an empty message. When your program runs, you wouldn't get an error, but your bot also would not post a message.&lt;/p&gt;

&lt;p&gt;To make this code resuable, we will add the text in a command line argument, using &lt;a href="https://nodejs.org/en/knowledge/command-line/how-to-parse-command-line-arguments/" rel="noopener noreferrer"&gt;Command Line Interface&lt;/a&gt; (CLI). It allows you to send a string when you run the program, instead of hardcoding it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This will pull the entry in the 3rd place on your array.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;commandLineText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="c1"&gt;// This object is needed because that is how Discord receives the information to post.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;messageData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;commandLineText&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now go to your &lt;code&gt;sendMessage&lt;/code&gt; function. Remove the console.log you had previously and replace it with this axios call. The comments will explain what each section does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;axios&lt;/span&gt;
    &lt;span class="c1"&gt;// This takes care of your HTTP POST. It needs two arguments.&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;// The webhook URL as a string&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Paste Your Discord Webhook URL Here&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="c1"&gt;// The variable that contains the object you are sending&lt;/span&gt;
    &lt;span class="nx"&gt;messageData&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;// axios is a promised-based .js library, so it has .then and .catch&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// This has been added so you can see all arguments being sent. It's not required&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! Save your file and get ready. Your code should look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;commandLineText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;messageData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;commandLineText&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sendMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;axios&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://just-a-placeholder-for-your-webhook-url&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;messageData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  RUN YOUR BOT!
&lt;/h2&gt;

&lt;p&gt;Open the Discord channel where your bot lives.&lt;/p&gt;

&lt;p&gt;Go to your terminal and make sure you are in the folder that holds your code.&lt;br&gt;
Type &lt;code&gt;node sendmessage.js "Hello!"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Peek over at your Discord channel. If all is well, you should see your bot pop up and say hi!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node sendmessage.js &lt;span class="s2"&gt;"Hello!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdndhxw7ei%2Fimage%2Fupload%2Fv1637638023%2Fblog%2Fdiscordbot%2Fcaptain_hook_hello_k3tyqv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdndhxw7ei%2Fimage%2Fupload%2Fv1637638023%2Fblog%2Fdiscordbot%2Fcaptain_hook_hello_k3tyqv.png" alt="Your Bot in Discord gives a friendly Hello!"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because the code also interprets the bash command as an array, it will print the string in the 3rd position, regardless of its size. You can sent it whatever text you'd like, as long as it is all part of the same string. Give it a try!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node sendmessage.js &lt;span class="s2"&gt;"What Lovely Weather We Be Havin'!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdndhxw7ei%2Fimage%2Fupload%2Fv1637638023%2Fblog%2Fdiscordbot%2Fcaptain_hook_weather_oezrir.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fres.cloudinary.com%2Fdndhxw7ei%2Fimage%2Fupload%2Fv1637638023%2Fblog%2Fdiscordbot%2Fcaptain_hook_weather_oezrir.png" alt="Discord Bot comments on the weather"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Way to go! Now you can set up a bot on another channel and share have some fun with friends, as long as you have the right permissions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;**Note:&lt;/em&gt;* If you'd like, you can add more to the array, but you'll have to adjust &lt;code&gt;process.argv[2]&lt;/code&gt; in &lt;code&gt;commandLineText&lt;/code&gt; to modify your argument display*&lt;/p&gt;

&lt;h2&gt;
  
  
  SUMMARY
&lt;/h2&gt;

&lt;p&gt;Today we learned how to set up a basic bot using webhooks on Discord. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thanks for checking out my tutorial!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>node</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
