DEV Community

Psuedoo
Psuedoo

Posted on

Handling Arguments in Twitchio for Beginners

Introduction

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.

Getting Started 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.

   async def event_message(self, message):
       print(message.content)
       await self.handle_commands(message)

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.

My Improper Fix

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.

   def get_args(self, ctx):
       if self.has_args(ctx):
           return self.clean_message(ctx)

The Proper Fix

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 Parameter section of Discordpy documentation covers everything someone would need to be able to write commands that handle arguments, even in Twitchio!

   async def test(ctx, arg):
       await ctx.send(arg)

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.

Top comments (0)