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

HivenClient ContextManager #73

Description

@Luna-Klatzer

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

conceptLogical Concept for OpenHiven.pyenhancementNew feature or requestrequestRequest for information or a feature

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions