Compare commits

...

8 Commits
v1.1 ... v1.5

Author SHA1 Message Date
c71a7555f9 Updated to 1.5 2023-06-26 13:29:47 +02:00
cb755faa9a Added scopes_placeholders 2023-06-26 13:29:26 +02:00
1859d0532c Updated to 1.4 2023-06-26 13:06:55 +02:00
ebce8e0141 Fixed workers and max_concurrent_transmissions 2023-06-26 13:06:23 +02:00
9af6d5cb7c Integrated the scheduler 2023-06-26 12:45:39 +02:00
7f054a6d93 Updated to 1.3 2023-06-26 12:31:52 +02:00
d6e3ecb564 Updated to 1.2 2023-06-26 12:26:13 +02:00
e93e73bca9 Added missing import 2023-06-26 12:25:55 +02:00
3 changed files with 32 additions and 14 deletions

View File

@@ -1,7 +1,9 @@
__name__ = "libbot" __name__ = "libbot"
__version__ = "1.1" __version__ = "1.5"
__license__ = "GPL3" __license__ = "GPL3"
__author__ = "Profitroll" __author__ = "Profitroll"
from .__main__ import * from .__main__ import *
from . import sync from . import sync
from . import i18n
from . import pyrogram

View File

@@ -1,12 +1,14 @@
import logging import logging
from datetime import datetime, timedelta from datetime import datetime, timedelta
from os import getpid from os import cpu_count, getpid
from pathlib import Path from pathlib import Path
from time import time from time import time
from typing import List, Union from typing import Dict, List, Union
try: try:
import pyrogram import pyrogram
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.schedulers.background import BackgroundScheduler
from pyrogram.client import Client from pyrogram.client import Client
from pyrogram.errors import BadRequest from pyrogram.errors import BadRequest
from pyrogram.handlers.message_handler import MessageHandler from pyrogram.handlers.message_handler import MessageHandler
@@ -40,7 +42,9 @@ logger = logging.getLogger(__name__)
class PyroClient(Client): 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: with open("config.json", "r", encoding="utf-8") as f:
self.config: dict = loads(f.read()) self.config: dict = loads(f.read())
super().__init__( super().__init__(
@@ -50,12 +54,16 @@ class PyroClient(Client):
bot_token=self.config["bot"]["bot_token"], bot_token=self.config["bot"]["bot_token"],
# Workers should be commented when using convopyro, otherwise # Workers should be commented when using convopyro, otherwise
# handlers land in another event loop and you won't see them # 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"]), plugins=dict(root="plugins", exclude=self.config["disabled_plugins"]),
sleep_threshold=120, sleep_threshold=120,
max_concurrent_transmissions=self.config["bot"][ max_concurrent_transmissions=self.config["bot"][
"max_concurrent_transmissions" "max_concurrent_transmissions"
], ]
if "max_concurrent_transmissions" in self.config["bot"]
else 1,
) )
self.owner: int = self.config["bot"]["owner"] self.owner: int = self.config["bot"]["owner"]
self.commands: List[PyroCommand] = [] self.commands: List[PyroCommand] = []
@@ -70,7 +78,11 @@ class PyroClient(Client):
self.in_all_locales = self.bot_locale.in_all_locales self.in_all_locales = self.bot_locale.in_all_locales
self.in_every_locale = self.bot_locale.in_every_locale 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() await super().start()
self.start_time = time() self.start_time = time()
@@ -89,14 +101,17 @@ class PyroClient(Client):
text=f"Bot started PID `{getpid()}`", text=f"Bot started PID `{getpid()}`",
) )
scheduler.add_job( if self.scheduler is None:
return
self.scheduler.add_job(
self.register_commands, self.register_commands,
trigger="date", trigger="date",
run_date=datetime.now() + timedelta(seconds=5), run_date=datetime.now() + timedelta(seconds=5),
kwargs={"command_sets": await self.collect_commands()}, kwargs={"command_sets": await self.collect_commands()},
) )
scheduler.start() self.scheduler.start()
except BadRequest: except BadRequest:
logger.warning("Unable to send message to report chat.") logger.warning("Unable to send message to report chat.")
@@ -151,8 +166,9 @@ class PyroClient(Client):
scope_dict = loads(scope) scope_dict = loads(scope)
# Replace "owner" in the bot scope with owner's id # Replace "owner" in the bot scope with owner's id
if "chat_id" in scope_dict and scope_dict["chat_id"] == "owner": for placeholder, chat_id in self.scopes_placeholders.items():
scope_dict["chat_id"] = self.owner 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 # Create object with the same name and args from the dict
try: try:

View File

@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project] [project]
name = "libbot" name = "libbot"
version = "1.1" version = "1.5"
authors = [{ name = "Profitroll" }] authors = [{ name = "Profitroll" }]
description = "Universal bot library with functions needed for basic Discord/Telegram bot development." description = "Universal bot library with functions needed for basic Discord/Telegram bot development."
readme = "README.md" readme = "README.md"
@@ -25,8 +25,8 @@ classifiers = [
dependencies = ["aiofiles~=23.1.0"] dependencies = ["aiofiles~=23.1.0"]
[project.optional-dependencies] [project.optional-dependencies]
pycord = ["py-cord>=2.0.0"] pycord = ["py-cord~=2.4.1"]
pyrogram = ["pyrogram>=2.0.0"] pyrogram = ["pyrogram~=2.0.106", "apscheduler~=3.10.1"]
speed = ["ujson~=5.8.0"] speed = ["ujson~=5.8.0"]
[project.urls] [project.urls]