Skip to content

Commit

Permalink
trying to fix user and bot id being zero
Browse files Browse the repository at this point in the history
  • Loading branch information
rking32 committed Feb 28, 2022
1 parent e5ab826 commit 6b73137
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
23 changes: 11 additions & 12 deletions userge/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ async def start(self):

if self.is_bot:
RawClient.BOT_ID = self._me.id
_LOG.info(f"started userge bot: {self._me.username}")
_LOG.info(f"started bot: {self._me.username}")
else:
RawClient.USER_ID = self._me.id
_LOG.info(f"started userge: {self._me.first_name}")
_LOG.info(f"started user: {self._me.first_name}")

def __eq__(self, o: object) -> bool:
return isinstance(o, _AbstractUserge) and self.id == o.id
Expand Down Expand Up @@ -279,22 +279,22 @@ async def start(self) -> None:
await _wait_for_instance()
await _set_running(True)

await self._load_plugins()
await self.manager.start()

_LOG.info("Starting ...")

await super().start()

if self._bot is not None:
await self._bot.start()

async def stop(self) -> None: # pylint: disable=arguments-differ
""" stop client and bot """
await self.manager.stop()
await self._load_plugins()
await self.manager.start()

async def stop(self, **_) -> None:
""" stop client and bot """
_LOG.info("Stopping ...")

await self.manager.stop()

if self._bot is not None:
await self._bot.stop()

Expand Down Expand Up @@ -328,10 +328,9 @@ def _handle(num, _) -> None:
_LOG.info(f"Idling Userge - {mode}")
self.loop.run_until_complete(idle_event.wait())

if self.is_initialized:
with suppress(RuntimeError):
self.loop.run_until_complete(self.stop())
self.loop.run_until_complete(self.manager.exit())
with suppress(RuntimeError):
self.loop.run_until_complete(self.stop())
self.loop.run_until_complete(self.manager.exit())

to_cancel = asyncio.all_tasks(self.loop)
for t in to_cancel:
Expand Down
2 changes: 2 additions & 0 deletions userge/core/methods/decorators/raw_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ def _build_decorator(self,
def decorator(func: _PYROFUNC) -> _PYROFUNC:
async def template(r_c: Union['_client.Userge', '_client.UsergeBot'],
r_m: RawMessage) -> None:
await self.manager.wait()

if system.Dynamic.DISABLED_ALL and r_m.chat.id != config.LOG_CHANNEL_ID:
raise StopPropagation
if r_m.chat and r_m.chat.id in system.DISABLED_CHATS:
Expand Down
14 changes: 14 additions & 0 deletions userge/core/types/new/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

__all__ = ['Manager']

import asyncio
from typing import Union, List, Dict, Optional

from userge import config
Expand All @@ -23,6 +24,7 @@ class Manager:
""" manager for userge """
def __init__(self, client: '_client.Userge') -> None:
self._client = client
self._event = asyncio.Event()
self.plugins: Dict[str, Plugin] = {}

@property
Expand Down Expand Up @@ -209,6 +211,7 @@ async def _update(self) -> None:
await plg.update()

async def init(self) -> None:
self._event.clear()
await _init_unloaded()

for plg in self.plugins.values():
Expand All @@ -218,14 +221,25 @@ async def init(self) -> None:

flt.load()

async def wait(self) -> None:
await self._event.wait()

async def start(self) -> None:
self._event.clear()

for plg in self.plugins.values():
await plg.start()

self._event.set()

async def stop(self) -> None:
self._event.clear()

for plg in self.plugins.values():
await plg.stop()

self._event.set()

def clear(self) -> None:
for plg in self.plugins.values():
plg.clear()
Expand Down

0 comments on commit 6b73137

Please sign in to comment.