<?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: Ricardo Ramierz</title>
    <description>The latest articles on DEV Community by Ricardo Ramierz (@elrick97).</description>
    <link>https://dev.to/elrick97</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%2F286974%2F9579ae5c-3e81-443b-a3e2-61bec93dbdfd.jpeg</url>
      <title>DEV Community: Ricardo Ramierz</title>
      <link>https://dev.to/elrick97</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/elrick97"/>
    <language>en</language>
    <item>
      <title>Python Discord Bot Tutorial</title>
      <dc:creator>Ricardo Ramierz</dc:creator>
      <pubDate>Sat, 05 Feb 2022 00:14:24 +0000</pubDate>
      <link>https://dev.to/elrick97/python-discord-bot-tutorial-3k06</link>
      <guid>https://dev.to/elrick97/python-discord-bot-tutorial-3k06</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This will be a two-part beginner's tutorial on how to set up, code, and deploy your first discord bot to your audience. By the end of this first part, you will have the fundamentals on how to make the bot respond to commands given by the user and have it live on your Discord server. In part 2, we are going to dive a little deeper into more advanced features delivering beautifully-formatted responses and adding a small API to add value to your bot.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tech stack and Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Python 3.5.3 or higher: to code the functionality.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://discordpy.readthedocs.io/en/stable/"&gt;discord.py&lt;/a&gt;: will be our API wrapper for discord.&lt;/li&gt;
&lt;li&gt;pip3: to install Python dependencies.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To verify you have both Python3 and pip3, run the following commands:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python &lt;span class="nt"&gt;-m&lt;/span&gt; pip &lt;span class="nt"&gt;--version&lt;/span&gt;
or 
python3 &lt;span class="nt"&gt;-m&lt;/span&gt; pip &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Depending on how you have set up your environments, you might need to refer to it as python3 or python. If the output of both is something like this, everything should be good!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--96wzBNEZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1i2kvvmui5tz8r0b4tib.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--96wzBNEZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1i2kvvmui5tz8r0b4tib.png" alt="version check image" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you need help installing either of those, follow these instructions:&lt;/p&gt;

&lt;p&gt;For Unix users, you can install both Python3 and pip3 via &lt;a href="https://brew.sh/index_es"&gt;Homebrew&lt;/a&gt; with the following command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Another option is to download Python3 from the &lt;a href="https://www.python.org/downloads/"&gt;official page&lt;/a&gt; and then install pip using this &lt;a href="https://pip.pypa.io/en/stable/getting-started/"&gt;guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Run the commands on the top of this section to verify your installation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Create a discord application.
&lt;/h3&gt;

&lt;p&gt;Head to your &lt;a href="https://discord.com/developers/applications"&gt;discord developer portal&lt;/a&gt; and sign in with your account. Once in, you should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pcuaNGFU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sakpbzl4qqzdiil5u2rp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pcuaNGFU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sakpbzl4qqzdiil5u2rp.png" alt="Discord developer portal" width="880" height="560"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click the &lt;em&gt;New Application&lt;/em&gt; button, give your app a name (don't stress too much about the name, you can always change it later), and click &lt;em&gt;Create&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CG4o0UDL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tm7gr0sut4a7azzsd2uy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CG4o0UDL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tm7gr0sut4a7azzsd2uy.png" alt="App creation" width="880" height="749"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have our app, we want to add a new bot. Navigate to the &lt;em&gt;bot&lt;/em&gt; tab on the left-hand side menu and click &lt;em&gt;Add Bot&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rlEE9-z4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hire1arezr5itu6zz6l6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rlEE9-z4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hire1arezr5itu6zz6l6.png" alt="Adding Bot" width="880" height="560"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we have our bot, we want to copy the token. It is very important to always keep the token to yourself since it is your access to your bot and its functionality.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iVEl-kUd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nq584kjjzddvwcytjkt1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iVEl-kUd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nq584kjjzddvwcytjkt1.png" alt="Copy token" width="880" height="560"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have our bot token in our clipboard, let us jump into your favorite editor and bring it to life.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code the bot
&lt;/h3&gt;

&lt;p&gt;I am going to use visual studio code, but you can follow along with any editor. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open a new command line&lt;/li&gt;
&lt;li&gt;Navigate to the directory you want to save it.&lt;/li&gt;
&lt;li&gt;Clone the starter code: &lt;a href="https://github.com/elrick97/discord-bot-starter/"&gt;https://github.com/elrick97/discord-bot-starter/&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you have any trouble cloning the repository follow this &lt;a href="https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository"&gt;tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CCpvRBTJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m497hjnozag8gf0x54hj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CCpvRBTJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m497hjnozag8gf0x54hj.png" alt="clone repo" width="880" height="594"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the project directory, run the following command on your terminal to install the dependencies (in this case only discord.py).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt
or
pip3 &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything went well you should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2qgXvUef--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjd5qcf4499pdtfod2nu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2qgXvUef--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjd5qcf4499pdtfod2nu.png" alt="install requirements" width="880" height="785"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yes, I will upgrade my pip version after this I swear... Anyways now that we have everything set up we can finally get to coding.&lt;/p&gt;

&lt;p&gt;Open the folder on your favorite code editor. You will see seven files in the navigator, most of them are to facilitate things when we deploy the finished bot. For now, let us go to &lt;strong&gt;bot.py&lt;/strong&gt; and add the following code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7dzmNUvS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3cbx1zc6y7dhps66879l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7dzmNUvS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3cbx1zc6y7dhps66879l.png" alt="bot.py code" width="880" height="1110"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will go through each point explaining it in detail.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The first point is just to import the libraries discord.py and datetime.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Here we are setting the command prefix to be the character &lt;strong&gt;!&lt;/strong&gt; (exclamation point), you can set any command prefix you want, any time you want to call a bot command you will have to use it as a prefix. &lt;/p&gt;

&lt;p&gt;For example the command !time. &lt;/p&gt;

&lt;p&gt;We are also creating our client instance, which represents the class to interact with the Discord WebSocket API.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You want to paste here the bot token you previously copied from your app dashboard in the Discord developer portal. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;On the library we are using we can interact with events that happen on our server. Some examples are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;on_ready: this function is called when the bot is ready.&lt;/li&gt;
&lt;li&gt;on_member_join: this function is called when a user joins the server.&lt;/li&gt;
&lt;li&gt;on_typing: called when someone begins typing a message.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And many more, for now we are going to use only the on_ready, but feel free to play around with the others. You can find all methods in the &lt;a href="https://discordpy.readthedocs.io/en/stable/api.html?highlight=event#event-reference"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Not only you can use event and utility methods provided by the API, but you can create your own custom commands. Let’s dive and decompose our function for a better understanding.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First we tell our client that the following function will be a command.&lt;/li&gt;
&lt;li&gt;We define our &lt;strong&gt;time&lt;/strong&gt; function passing &lt;strong&gt;ctx&lt;/strong&gt; (context) as the only argument. Context represents the context in which the command is being invoked, comes with a lot of metadata, for example author, channel, guild, valid, etc. For further reference check out this &lt;a href="https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=context#discord.ext.commands.Context"&gt;link&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Then we just get the current time with help from the datetime library and return the message in the same context with the &lt;strong&gt;send&lt;/strong&gt; method.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, we call the &lt;strong&gt;run&lt;/strong&gt; method with our &lt;strong&gt;token&lt;/strong&gt; to start the bot. It is important to have this method at the end of your code since anything below it will not be executed. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Before we can test our bot, we need to add it to a server. To do this, go back to your app dashboard in your Discord developer portal. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to URL Generator from OAuth2 menu on the sidebar.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Bot&lt;/strong&gt; on scopes.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Administrator&lt;/strong&gt; on permissions (just to simplify the process you can change this later).&lt;/li&gt;
&lt;li&gt;Scroll down and copy the link.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SvbmJms7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9iogxzbpuoy7i45bfgnm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SvbmJms7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9iogxzbpuoy7i45bfgnm.png" alt="url generator" width="880" height="560"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open the link in a new tab and add the bot to the server you want to test. You have to be the owner of the server to add it, you might as well want to create a test server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MrlEt1lZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sgi6a07ryadgdlx9ofpv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MrlEt1lZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sgi6a07ryadgdlx9ofpv.png" alt="connect bot" width="880" height="560"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After adding the bot, it should appear offline on your server:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FU_iFkhe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p3ali45gt7gwt8ypyt7g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FU_iFkhe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p3ali45gt7gwt8ypyt7g.png" alt="add to server" width="880" height="560"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To bring him to life, run the following command on your working directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python bot.py
or
python3 bot.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you got this message, the bot is ready and online on your server!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w4ndGy4Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nh9h2x93vf6bui4maja4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w4ndGy4Y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nh9h2x93vf6bui4maja4.png" alt="run python script" width="880" height="262"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now go test it on your server, type the command &lt;strong&gt;!time&lt;/strong&gt; and your bot should answer you back with the current time as shown below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FWORtGGd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jfy1lu8h3sgl28lqduw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FWORtGGd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jfy1lu8h3sgl28lqduw.png" alt="working test" width="880" height="560"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we conclude this first part of our tutorial. In the second part, we will dive further into commands by adding arguments, implementing an API, delivering embedded messages, and deploying your bot.&lt;/p&gt;

&lt;p&gt;If you run into any trouble feel free to DM me on &lt;a href="https://twitter.com/elrickrmz"&gt;Twitter&lt;/a&gt; and follow me for updates on part two and more content.&lt;/p&gt;

</description>
      <category>python</category>
      <category>discord</category>
      <category>codenewbie</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
