Skip to content

Commit

Permalink
Don't subclass GatewayBot (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
circuitsacul authored Jan 22, 2022
1 parent 19def24 commit 9dd7627
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
15 changes: 9 additions & 6 deletions examples/basic/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from hikari import GatewayBot

from hikari_clusters import Cluster, ClusterLauncher, Server


class MyBot(Cluster):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class MyBot(GatewayBot):
cluster: Cluster
# purely optional typehint. ClusterLauncher will set this on init

def __init__(self):
super().__init__(token="discord token")

# load modules & events here

Expand All @@ -35,7 +40,5 @@ def run() -> None:
host="localhost",
port=8765,
token="ipc token",
cluster_launcher=ClusterLauncher(
MyBot, bot_init_kwargs={"token": "discord token"}
),
cluster_launcher=ClusterLauncher(MyBot),
).run()
32 changes: 18 additions & 14 deletions hikari_clusters/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
)


class Cluster(GatewayBot):
class Cluster:
"""A subclass of :class:`~hikari.GatewayBot` designed for
use with hikari-clusters.
Expand Down Expand Up @@ -71,9 +71,10 @@ def __init__(
shard_count: int,
server_uid: int,
certificate_path: pathlib.Path | None,
init_kwargs: dict[str, Any],
bot: GatewayBot,
) -> None:
super().__init__(**init_kwargs)
self.bot = bot
self.bot.cluster = self # type: ignore

self.shard_ids = shard_ids
"""The shard ids for this cluster."""
Expand Down Expand Up @@ -112,7 +113,7 @@ def cluster_id(self) -> int:
def ready(self) -> bool:
"""Whether or not this cluster is fully launched."""

return len(self.shards) == len(self.shard_ids)
return len(self.bot.shards) == len(self.shard_ids)

@property
def shard_count(self) -> int:
Expand All @@ -134,9 +135,9 @@ async def start(self, **kwargs) -> None:
kwargs["shard_count"] = self.shard_count
kwargs["shard_ids"] = self.shard_ids

await super().start(**kwargs)
await self.bot.start(**kwargs)

async def join(self, *args, **kwargs) -> None:
async def join(self) -> None:
"""Wait for the bot to close, and then return.
Does not ask the bot to close. Use :meth:`~Cluster.stop` to tell
Expand All @@ -148,6 +149,7 @@ async def join(self, *args, **kwargs) -> None:
[self.stop_future, self.ipc.stop_future],
return_when=asyncio.FIRST_COMPLETED,
)
await self.bot.join()

async def close(self) -> None:
self.ipc.stop()
Expand All @@ -156,7 +158,7 @@ async def close(self) -> None:
self.__tasks.cancel_all()
await self.__tasks.wait_for_all()

await super().close()
await self.bot.close()

def stop(self) -> None:
"""Tells the bot and IPC to close."""
Expand Down Expand Up @@ -191,13 +193,15 @@ class ClusterLauncher:

def __init__(
self,
bot_class: Type[Cluster] = Cluster,
bot_class: Type[GatewayBot],
bot_init_kwargs: dict[str, Any] | None = None,
bot_start_kwargs: dict[str, Any] | None = None,
cluster_class: Type[Cluster] = Cluster,
) -> None:
self.bot_class = bot_class
self.bot_init_kwargs = bot_init_kwargs or {}
self.bot_start_kwargs = bot_start_kwargs or {}
self.cluster_class = cluster_class

def launch_cluster(
self,
Expand All @@ -213,24 +217,24 @@ def launch_cluster(
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

bot = self.bot_class(
cluster = self.cluster_class(
ipc_uri,
ipc_token,
shard_ids,
shard_count,
server_uid,
certificate_path,
self.bot_init_kwargs,
self.bot_class(**self.bot_init_kwargs),
)

def sigstop(*args, **kwargs) -> None:
bot.stop()
cluster.stop()

loop.add_signal_handler(signal.SIGINT, sigstop)

loop.run_until_complete(bot.start())
loop.run_until_complete(bot.join())
loop.run_until_complete(bot.close())
loop.run_until_complete(cluster.start())
loop.run_until_complete(cluster.join())
loop.run_until_complete(cluster.close())


_C = CommandGroup()
Expand Down

0 comments on commit 9dd7627

Please sign in to comment.