<?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: Psuedoo</title>
    <description>The latest articles on DEV Community by Psuedoo (@psuedoo).</description>
    <link>https://dev.to/psuedoo</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%2F484655%2Fdb173cfb-a5a2-4438-9f81-d42eb908e093.png</url>
      <title>DEV Community: Psuedoo</title>
      <link>https://dev.to/psuedoo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/psuedoo"/>
    <language>en</language>
    <item>
      <title>Handling Arguments in Twitchio for Beginners</title>
      <dc:creator>Psuedoo</dc:creator>
      <pubDate>Sun, 11 Oct 2020 22:18:33 +0000</pubDate>
      <link>https://dev.to/psuedoo/handling-arguments-in-twitchio-for-beginners-2can</link>
      <guid>https://dev.to/psuedoo/handling-arguments-in-twitchio-for-beginners-2can</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;As programmers, it is often referred to as a rite of passage to create your own chatbot. When I first started creating my own Twitch bot, issues started arriving when handling arguments in messages. Depending on how in-depth the Twitch chatbot is going to be, the passing of arguments is a common thing that should be simple.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitchio.readthedocs.io/en/rewrite/twitchio.html#getting-started"&gt;Getting Started&lt;/a&gt; has a quick and easy bot example that seems to be the only Twitchio documentation section where it mentions how you can handle passing arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;   &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;event_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
       &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
       &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;handle_commands&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;While it is not referring to how arguments should be passed within chat messages, it covers how you should pass the message to handle it as a command. &lt;/p&gt;

&lt;h3&gt;
  
  
  My Improper Fix
&lt;/h3&gt;

&lt;p&gt;I first encountered the problem when I wrote a quick function to handle the arguments. The argument would take the message as an argument and give me the individual words after the command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_args&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
       &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;has_args&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
           &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clean_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  The Proper Fix
&lt;/h3&gt;

&lt;p&gt;When I started creating a Discord chatbot, I quickly realized how easy handling arguments are without my quick fix mentioned above. There is no need for another function to handle the arguments. The &lt;a href="https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#parameters"&gt;Parameter&lt;/a&gt; section of Discordpy documentation covers everything someone would need to be able to write commands that handle arguments, even in Twitchio!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;   &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
       &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Passing the parameter in the command definition is a valid way and exactly how you would handle it if you were just making a function for passing arguments in Python. &lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>beginners</category>
      <category>python</category>
    </item>
  </channel>
</rss>
