Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
c71a7555f9
|
|||
cb755faa9a
|
|||
1859d0532c
|
|||
ebce8e0141
|
|||
9af6d5cb7c
|
|||
7f054a6d93
|
@@ -1,5 +1,5 @@
|
||||
__name__ = "libbot"
|
||||
__version__ = "1.2"
|
||||
__version__ = "1.5"
|
||||
__license__ = "GPL3"
|
||||
__author__ = "Profitroll"
|
||||
|
||||
|
@@ -1,12 +1,14 @@
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
from os import getpid
|
||||
from os import cpu_count, getpid
|
||||
from pathlib import Path
|
||||
from time import time
|
||||
from typing import List, Union
|
||||
from typing import Dict, List, Union
|
||||
|
||||
try:
|
||||
import pyrogram
|
||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from pyrogram.client import Client
|
||||
from pyrogram.errors import BadRequest
|
||||
from pyrogram.handlers.message_handler import MessageHandler
|
||||
@@ -40,7 +42,9 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PyroClient(Client):
|
||||
def __init__(self):
|
||||
def __init__(
|
||||
self, scheduler: Union[AsyncIOScheduler, BackgroundScheduler, None] = None
|
||||
):
|
||||
with open("config.json", "r", encoding="utf-8") as f:
|
||||
self.config: dict = loads(f.read())
|
||||
super().__init__(
|
||||
@@ -50,12 +54,16 @@ class PyroClient(Client):
|
||||
bot_token=self.config["bot"]["bot_token"],
|
||||
# Workers should be commented when using convopyro, otherwise
|
||||
# handlers land in another event loop and you won't see them
|
||||
workers=self.config["bot"]["workers"],
|
||||
workers=self.config["bot"]["workers"]
|
||||
if "workers" in self.config["bot"]
|
||||
else min(32, cpu_count() + 4),
|
||||
plugins=dict(root="plugins", exclude=self.config["disabled_plugins"]),
|
||||
sleep_threshold=120,
|
||||
max_concurrent_transmissions=self.config["bot"][
|
||||
"max_concurrent_transmissions"
|
||||
],
|
||||
]
|
||||
if "max_concurrent_transmissions" in self.config["bot"]
|
||||
else 1,
|
||||
)
|
||||
self.owner: int = self.config["bot"]["owner"]
|
||||
self.commands: List[PyroCommand] = []
|
||||
@@ -70,7 +78,11 @@ class PyroClient(Client):
|
||||
self.in_all_locales = self.bot_locale.in_all_locales
|
||||
self.in_every_locale = self.bot_locale.in_every_locale
|
||||
|
||||
async def start(self, scheduler):
|
||||
self.scheduler: Union[AsyncIOScheduler, BackgroundScheduler, None] = scheduler
|
||||
|
||||
self.scopes_placeholders: Dict[str, int] = {"owner": self.owner}
|
||||
|
||||
async def start(self):
|
||||
await super().start()
|
||||
|
||||
self.start_time = time()
|
||||
@@ -89,14 +101,17 @@ class PyroClient(Client):
|
||||
text=f"Bot started PID `{getpid()}`",
|
||||
)
|
||||
|
||||
scheduler.add_job(
|
||||
if self.scheduler is None:
|
||||
return
|
||||
|
||||
self.scheduler.add_job(
|
||||
self.register_commands,
|
||||
trigger="date",
|
||||
run_date=datetime.now() + timedelta(seconds=5),
|
||||
kwargs={"command_sets": await self.collect_commands()},
|
||||
)
|
||||
|
||||
scheduler.start()
|
||||
self.scheduler.start()
|
||||
except BadRequest:
|
||||
logger.warning("Unable to send message to report chat.")
|
||||
|
||||
@@ -151,8 +166,9 @@ class PyroClient(Client):
|
||||
scope_dict = loads(scope)
|
||||
|
||||
# Replace "owner" in the bot scope with owner's id
|
||||
if "chat_id" in scope_dict and scope_dict["chat_id"] == "owner":
|
||||
scope_dict["chat_id"] = self.owner
|
||||
for placeholder, chat_id in self.scopes_placeholders.items():
|
||||
if "chat_id" in scope_dict and scope_dict["chat_id"] == placeholder:
|
||||
scope_dict["chat_id"] = chat_id
|
||||
|
||||
# Create object with the same name and args from the dict
|
||||
try:
|
||||
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "libbot"
|
||||
version = "1.2"
|
||||
version = "1.5"
|
||||
authors = [{ name = "Profitroll" }]
|
||||
description = "Universal bot library with functions needed for basic Discord/Telegram bot development."
|
||||
readme = "README.md"
|
||||
@@ -25,8 +25,8 @@ classifiers = [
|
||||
dependencies = ["aiofiles~=23.1.0"]
|
||||
|
||||
[project.optional-dependencies]
|
||||
pycord = ["py-cord>=2.0.0"]
|
||||
pyrogram = ["pyrogram>=2.0.0"]
|
||||
pycord = ["py-cord~=2.4.1"]
|
||||
pyrogram = ["pyrogram~=2.0.106", "apscheduler~=3.10.1"]
|
||||
speed = ["ujson~=5.8.0"]
|
||||
|
||||
[project.urls]
|
||||
|
Reference in New Issue
Block a user