Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

HivenClient ContextManager #73

Open
Luna-Klatzer opened this issue Apr 16, 2021 · 0 comments
Open

HivenClient ContextManager #73

Luna-Klatzer opened this issue Apr 16, 2021 · 0 comments
Assignees
Labels
concept Logical Concept for OpenHiven.py enhancement New feature or request request Request for information or a feature

Comments

@Luna-Klatzer
Copy link
Owner

Luna-Klatzer commented Apr 16, 2021

General Concept:
The idea behind using a ContextManager with a HivenClient is to be able to quickly use the Client and interact with the Hiven-API without having to focus too much on Event-listening or running it correctly. Still, more that you start the bot, run code and then the bot will stop as soon as the code finished you wanted to execute.

This could be useful for small interactions and quick changes, mainly consisting of a few lines, which can be then quickly executed. To that, if a user just wants to await something with HivenClient.wait_for('on_message_create'), they could do that in a few lines and then immediately stop the bot with a nice and more concise syntax.

on_ready and on_init won't be usable with that syntax since __aenter__ only returns when the Client received the ready state.

Usage:

  • Standard:
    async with openhivenpy.HivenClient(token="insert token") as client:
        # Only runs in this scope and will stop when the code finished executing
  • With Event-Listeners:
    async with openhivenpy.HivenClient(token="insert token") as client:
        @client.event()
        async def on_message_create(msg):
            print("Received message")
    or
    async def on_message_create(msg):
        print("Received message")
    
    async with openhivenpy.HivenClient(token="insert token") as client:
        client.event(on_message_create) # Calling the decorator directly for wrapping and adding the function

Changes:

  • Implementing __aenter__ and __aexit__ which will run and stop the bot. __aenter__ will then need to return when the ready function was called running parallel to the websocket.
  • Adding token as a static parameter to the HivenClient, which will make it not needed to provide a token on HivenClient.run(), but directly on construction
@Luna-Klatzer Luna-Klatzer added enhancement New feature or request request Request for information or a feature concept Logical Concept for OpenHiven.py labels Apr 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
concept Logical Concept for OpenHiven.py enhancement New feature or request request Request for information or a feature
Projects
None yet
Development

No branches or pull requests

2 participants