<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Bad Programming - Medium]]></title>
        <description><![CDATA[Trying to learn to program in all kinds of different languages. Help me become a better programmer. - Medium]]></description>
        <link>https://medium.com/bad-programming?source=rss----6cc24508191f---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Bad Programming - Medium</title>
            <link>https://medium.com/bad-programming?source=rss----6cc24508191f---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 02:36:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/bad-programming" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Making a Cool Discord Bot in Python 3]]></title>
            <link>https://medium.com/bad-programming/making-a-cool-discord-bot-in-python-3-e6773add3c48?source=rss----6cc24508191f---4</link>
            <guid isPermaLink="false">https://medium.com/p/e6773add3c48</guid>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[discord]]></category>
            <dc:creator><![CDATA[Paul Jerome]]></dc:creator>
            <pubDate>Sun, 25 Mar 2018 05:15:14 GMT</pubDate>
            <atom:updated>2018-03-25T16:01:31.257Z</atom:updated>
            <content:encoded><![CDATA[<p>Discord has a wonderful API that allows you to creates bots that can join Discord servers and channels and help manage as well as add extra functionality for it’s users.</p><p>In this tutorial I will show you how to make a basic Discord bot written in Python 3 that will be able to take handle custom commands using the <strong>discord.py</strong> package available through pip.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*L613vjPIEp0o2TeZnEbg0w.jpeg" /></figure><h3>Step 1: Getting Setup</h3><p>Having a good editor can make the process of writing and debugging code in any language much easier. Some good free editors that I have personally used are <a href="https://www.jetbrains.com/pycharm/download/">PyCharm Community Edition</a> and <a href="https://www.sublimetext.com/">Sublime Text 3</a>.</p><p>You will also need to make sure that you have Python 3 installed on you computer. If you are on Windows you can download an installer from the following link.</p><p><a href="https://www.python.org/">Welcome to Python.org</a></p><p>You will also need to go to the Discord website and create your bot, add it to a server, and grant it permissions. This can be done at the following link.</p><p><a href="https://discordapp.com/developers">Discord API Docs for Bots and Developers</a></p><p>The following eight pictures will guide you through setting up you bot and adding it to your Discord server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fl9ZQEOW3hexEnkwghAoCQ.jpeg" /><figcaption>Navigate to the link above and click on “My Apps”</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kqTMQvSul3RFnO1C8PhW7g.jpeg" /><figcaption>Click on “New App”</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UV9LRps7K9ED-cPynGVDzQ.jpeg" /><figcaption>Give you bot a name, a description, and an icon. These are what will be seen in Discord.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yg6v5L7wZQMHpFicr0rzPg.jpeg" /><figcaption>Make your new app a bot by clicking on “Create Bot User”</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rZWZ12bEX4MXIomBg4pLUg.jpeg" /><figcaption>Make your bot public. You can also see you bot token here, we will need to copy this later.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1VYm-_Hpts6fX9vycH8bmw.jpeg" /><figcaption>Click on “Generate OAuth2 URL”</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AkjDNc-Ray_7-WMiy_50bQ.jpeg" /><figcaption>Check the “bot” box, Check the “Administrator” box, and then go the the link generated.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w8cWpIO2RfCB8WzPy4iKlg.jpeg" /><figcaption>Finally, select the server that you would like to connect it to and click “Authorize”.</figcaption></figure><p>You should now be able to see your bot in the Discord server you added it too and it should have a little tag that says “Bot” next to it’s name.</p><h3>Step 2: Starting the Code</h3><p>Create a folder that you will use for bot and in it create a file called <strong>bot.py</strong>. We are going to start by adding the boilerplate code that is required.</p><p>But first we need to install <strong>discord.py</strong> with Python’s package manager called pip. You can do this by navigating to you Command Prompt in Windows and entering the following.</p><pre>pip install discord.py</pre><p>After this has completed successfully we can open our <strong>bot.py</strong> and begin. The code below imports the <strong>discord.py</strong> package that we just installed as well as creates our Discord client object that we will use to interact with the API. The <strong>token</strong> variable has our bot’s token that we copied earlier from the Discord developers page.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/629d34c48b178eae97779c0082ae30be/href">https://medium.com/media/629d34c48b178eae97779c0082ae30be/href</a></iframe><p>The <strong>client</strong> variable seen on line 4 is a Discord client object and will facilitate all the communication of the bot.</p><p>The <strong>on_ready()</strong> function above will be called when the bot starts and is connects to the API and will print the bot’s username, client ID, and the version of <strong>discord.py</strong> that is being used as seen beneath the <strong>try</strong> on line 12, 13, 14.</p><p>The <strong>on_message()</strong> function above will be called when ever a new message is posted in the Discord server and the message will be passed as the <strong>message</strong> variable seen entered as it’s only argument. As we have the function right now it will print that message to the console when received.</p><p>Try running this code, if it works you should see your bot’s username and client id printed in the console. If you go to the Discord channel that bot was added to and type a message you should see it appear in the console where you bot is running, you might also get an error directly after it which you can ignore for now.</p><h3>Step 3: Building a Command Handler</h3><p>So at this point our bot is able to read every message that is posted in it’s Discord server. To make this useful we need to implement some kind function that evaluates the messages that are posted. We can do this by making a command handler which will read each message, determine if a command is being called, and relay the appropriate response.</p><p>Although, before we make our command handler we have to decide on how we are going to organize our commands to make them easy to implement and visualize. My favorite way to do this in Python is by using dictionaries. Dictionaries allow us to define multiple labeled properties to a variable. An example is shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00211d7a38ac7a43f62041db613f4252/href">https://medium.com/media/00211d7a38ac7a43f62041db613f4252/href</a></iframe><p>The function above starting on line 2 will take in the parameters <strong>message</strong>, <strong>clients</strong>, and <strong>args. </strong>The variable <strong>message </strong>is an object with the details of the message, the <strong>client</strong> variable is also an object we need in the function to send messages with, the <strong>args</strong> variable is and array that will contain the arguments for function. This function will be used as a variable in our dictionary.</p><p><strong>The Command Dictionary Parts</strong></p><ol><li><strong>trigger</strong>: is a string that will be what the command handler will look for.</li><li><strong>function</strong>: is a function that will be executed when it’s trigger is found.</li><li><strong>args_num</strong>: is an integer that marks the amount of args the function uses.</li><li><strong>args_name</strong>: is an array of strings that name the args for feedback purposes.</li><li><strong>description</strong>: is a string that describes what the command does.</li></ol><p>We could just write an independent function that would handle these commands but there is a better way to do it, creating a class. This class will contain methods to add commands, store commands, and evaluate commands. The class would look like this…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/994f82e55249c290c16795b12a4c70ea/href">https://medium.com/media/994f82e55249c290c16795b12a4c70ea/href</a></iframe><p>The <strong>__init__(self, client)</strong> method will be called when the class is instantiated and will bind the client object to itself and create an array, <strong>self.commands</strong>, to store the commands.</p><p>The <strong>add_command(self, command)</strong> method simply adds the command dictionary that we designed earlier to the <strong>self.commands</strong> array.</p><p>The <strong>command_handler(self, message)</strong> method will be used to evaluate each message that is sent on the Discord server. It basically just sees if the message starts with a command trigger and if it does it executes it’s corresponding function. The following is a pseudo code breakdown of what the command handler is doing for each message posted in the server.</p><ol><li>Checks if the message starts with a command <strong>trigger, </strong>if it doesn’t it stops.</li><li>If the message does start with a <strong>trigger</strong> if makes sure that the <strong>trigger</strong> is valid by comparing it to the command dictionary&#39;s <strong>trigger</strong> value, if it’s not it stops.</li><li>If the <strong>trigger</strong> is valid it removes the <strong>trigger</strong> from the message string and splits it at the spaces making an array named <strong>args</strong>.</li><li>Then it checks to see how many <strong>args</strong> the command’s function takes using the command dictionary&#39;s <strong>args_num</strong> value, if it’s zero it simply executes the function in the command dictionary’s <strong>function</strong> value. If the command needs more than zero <strong>args</strong> it makes sure that there are at least the required amount and it passes them to the command dictionary’s <strong>function</strong>.</li></ol><h3>Step 4: Bringing it all together</h3><p>Below is what all this code would look like together and should be completely functional.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fc5d18c9248f1bb5badab7d25a1c84d/href">https://medium.com/media/4fc5d18c9248f1bb5badab7d25a1c84d/href</a></iframe><p>This bot should have a fully functioning <strong>!hello</strong> command that takes in one argument.</p><h3>Step 5: Adding useful Functions &amp; Examples</h3><p>So what we have now is a functional “platform” to expand on. In this step I will demonstrate some of the more advanced things you could do with this setup and some ways to make it more user friendly.</p><p><strong>Note:</strong> I removed the <strong>!hello</strong> command from earlier because it useless.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79ab570a8e2bba142c5846c31f24e373/href">https://medium.com/media/79ab570a8e2bba142c5846c31f24e373/href</a></iframe><p>In this example I added two new commands, <strong>!commands</strong> and <strong>!ip</strong>.</p><p>The <strong>!commands</strong> command will list all the available commands and there descriptions.</p><p>The <strong>!ip</strong> command will take in an argument called <strong>IP/Domain </strong>and it uses the Free IP-API and the Python 3 <strong>requests</strong> package to query the API with the given Domain or IP and returns information from the API response. The Python 3 <strong>json</strong> package is also needed to turn the API response into a Python dictionary which we can work with.</p><p>Also if you notice, you also can use the Discord formatting markdown shown in the link below to style your outputs, the <strong>**text** </strong>will show up as bold in Discord. You can also use Unicode Symbols to mark new lines (<strong>\n</strong>).</p><p><a href="https://support.discordapp.com/hc/en-us/articles/210298617-Markdown-Text-101-Chat-Formatting-Bold-Italic-Underline-">Markdown Text 101 (Chat Formatting: Bold, Italic, Underline)</a></p><p>At this point you can do allot. Any information you can get into Python (which is allot) you can style and print to the users. You can add allot of cool features just from using different API’s. Below are a couple other API’s that I have made commands with.</p><ul><li><a href="https://openweathermap.org/api">Weather API</a></li><li><a href="https://developer.oxforddictionaries.com/">Oxford Dictionaries API</a></li><li><a href="https://fortnitetracker.com/site-api">Fortnite Tracker - API</a></li></ul><p>I was also able to make and <strong>!sql</strong> command using this setup that you could use to query a remote MySQL database with which I plan to make a guide on in the future.</p><h3>Resources</h3><p>All the code featured in this guide is available at my GitHub.</p><p><a href="https://gist.github.com/Bert-Macklin">Build software better, together</a></p><p>I highly recommenced reading more of the documentation on <strong>discord.py</strong> because it has allot more features than are touched on in this guide.</p><p><a href="http://discordpy.readthedocs.io/en/latest/">Welcome to discord.py&#39;s documentation! - discord.py 0.16.12 documentation</a></p><p>This is the documentation for the <strong>requests</strong> package that I used in the last example. You can read about making requests with custom headers which is required for some API’s (really simple).</p><p><a href="http://docs.python-requests.org/en/master/">Requests: HTTP for Humans™ - Requests 2.21.0 documentation</a></p><p>This is a link to the API that I featured in the last example, the IP-API. This API is really useful, cool, and super easy to use but best of all free :)</p><p><a href="http://ip-api.com/">IP-API.com - Free Geolocation API</a></p><h4>Thanks for reading!</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6773add3c48" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bad-programming/making-a-cool-discord-bot-in-python-3-e6773add3c48">Making a Cool Discord Bot in Python 3</a> was originally published in <a href="https://medium.com/bad-programming">Bad Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>