-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider adding "on_command" decorator #61
Comments
I'd like to keep the Observer class a pretty lightweight Twitch Message -> Python Event class. In practice we always add this functionality so I think it would be good to add this logic in a specialized subclass. I'm thinking something like: class SimpleChatCommandObserver(TwitchChatObserver):
def on_command(*args, **kwargs):
"""Implementation details...""" Then it would be used basically as you proposed (thank you for the examples!). Thoughts? |
On the one hand, it is reasonable and pretty common thing to make another class which will extend functionality of the basic class with specific features (command handling, in this case), I've seen this pattern a lot. On the other hand, this is pretty core functionality and probably will be used most of the time, it correlates with event handling directly, since it is just kind of cool wrapper around it with few most-used things already implemented. It also can be confusing which class to pick to start doing things, it requires more time to read the docs (which is not bad, but not always necessary for such common things). I believe, user will end up using So, my point here: adding Both of the ways are acceptable, whatever you choose will be cool. It is just a matter of taste, i think. 😋 |
Oh, that was a hasty example. The API will certainly be more user friendly. 😄 |
Personally I would prefer adding But I get that @joshuaskelly wants to keep the initial Observer class lightweight. I think there are use cases where people aren't interested in commands and just want to listen for messages or log join/part events. Another thought: What about refactoring decorators (maybe we add even more in the future) to their own namespace? from twitchobserver.decorators import on_event, on_command Not sure how to handle the subscription process then tough. |
Since they rely on Observer's internal stuff, it is not possible or would result ugly API.
Still, I am not sure why is this a problem. Decorator and optional kwarg don't perform anything themselves, they just live inside of the class. Those people should and would ignore them. |
Commands vs Event ListenersA command is different from an event listener as commands are more mutable than event listeners. It's a very common use case for a chat moderator to add a new command during a stream. AliasingAlso, should aliases have a many to one relationship with command handlers? PrefixingPrefixing is useful to correctly identify user issued commands from normal chat. It is nice to allow defining custom prefix to avoid collision with other bots. Do we need a per command custom prefix, or is a global prefix fine? Proposed Class APIfrom twitchobserver import Observer
class SimpleChatCommandObserver(Observer):
def __init__(self, command_prefix='!'):
"""Constructor"""
def add_command(self, *alias, callback):
"""Add a command handler for the given aliases"""
def remove_command(self, alias):
"""Remove a command handler for the given alias"""
def update_command(self, alias):
"""Modify an exisiting command"""
def on_command(self, *alias):
"""Decorator for command handlers for given alias""" I'd appreciate feedback. I'm starting to worry that I'm over-engineering this. |
Yes
Both. A global one set per default and for each command a possible custom one.
Since you said that having commands change at runtime is a common use case I think you are not over-engineering it. |
I can see it now. Fair enough. |
I think, But in this case, it is not clear, which command could be deleted (by the moderator from chat, for example) and which couldn't. |
Since we already have
on_event
decorator, it could be also nice to have the same thing for chat commands.Examples
Another important moment about events handling: order of execution. Do you allow to set multiple handlers for the same event / command? If yes, in what order they will execute? If no, which one will execute?
I was thinking about
on_command
implementation. I wanted to re-use some of the logic fromon_event
, but could not come up with solution. It is just easier to create new decorator from scratch. So, I'd like to learn something new from you :)The text was updated successfully, but these errors were encountered: