Compare commits
26 Commits
ae28461f3b
...
v3.0.0
Author | SHA1 | Date | |
---|---|---|---|
5ba763246b | |||
c756c6b1dc
|
|||
9e9c90fce1
|
|||
91cc03f921 | |||
a0538625e2 | |||
f0ffdf096d | |||
e7555d3de1
|
|||
17f15aca5b
|
|||
73c3a1ff13 | |||
bfa3d4f6e9 | |||
3d10cbcb2f | |||
88f77e8494 | |||
f5f62f20cc | |||
45743bdb7e | |||
8e1f746309 | |||
c4df7c6106 | |||
5103333920
|
|||
df09a21aee | |||
1800ff4dc3 | |||
9557b1759b | |||
a650039bc9 | |||
a6a1f1cacc | |||
c8d5a81a2c | |||
c23326cd10 | |||
e55916501d | |||
4acd61c5c9 |
@@ -3,6 +3,9 @@
|
|||||||
"extends": [
|
"extends": [
|
||||||
"config:base"
|
"config:base"
|
||||||
],
|
],
|
||||||
|
"baseBranches": [
|
||||||
|
"dev"
|
||||||
|
],
|
||||||
"pip_requirements": {
|
"pip_requirements": {
|
||||||
"fileMatch": [
|
"fileMatch": [
|
||||||
"requirements/.*\\.txt$"
|
"requirements/.*\\.txt$"
|
||||||
|
@@ -1,11 +1,11 @@
|
|||||||
black==23.10.1
|
black==23.12.1
|
||||||
build==1.0.3
|
build==1.0.3
|
||||||
isort==5.12.0
|
isort==5.13.2
|
||||||
mypy==1.6.1
|
mypy==1.8.0
|
||||||
pylint==3.0.2
|
pylint==3.0.3
|
||||||
pytest-asyncio==0.21.1
|
pytest-asyncio==0.23.3
|
||||||
pytest-cov==4.1.0
|
pytest-cov==4.1.0
|
||||||
pytest==7.4.3
|
pytest==7.4.4
|
||||||
tox==4.11.3
|
tox==4.11.4
|
||||||
types-aiofiles==23.2.0.0
|
types-aiofiles==23.2.0.0
|
||||||
types-ujson==5.8.0.1
|
types-ujson==5.9.0.0
|
@@ -1 +1,2 @@
|
|||||||
|
apscheduler~=3.10.4
|
||||||
py-cord~=2.4.1
|
py-cord~=2.4.1
|
@@ -1,2 +1,2 @@
|
|||||||
apscheduler~=3.10.1
|
apscheduler~=3.10.4
|
||||||
pyrogram~=2.0.106
|
pyrofork~=2.3.16.post1
|
@@ -1 +1 @@
|
|||||||
ujson~=5.8.0
|
ujson~=5.9.0
|
@@ -1,6 +1,6 @@
|
|||||||
__version__ = "2.0.1"
|
__version__ = "3.0.0"
|
||||||
__license__ = "GPL3"
|
__license__ = "GPL3"
|
||||||
__author__ = "Profitroll"
|
__author__ = "Profitroll"
|
||||||
|
|
||||||
from . import i18n, pyrogram, sync
|
from . import i18n, pycord, pyrogram, sync
|
||||||
from .__main__ import *
|
from .__main__ import *
|
||||||
|
1
src/libbot/pycord/classes/__init__.py
Normal file
1
src/libbot/pycord/classes/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from .bot import PycordBot
|
57
src/libbot/pycord/classes/bot.py
Normal file
57
src/libbot/pycord/classes/bot.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any, Dict, Union
|
||||||
|
|
||||||
|
try:
|
||||||
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||||
|
from apscheduler.schedulers.background import BackgroundScheduler
|
||||||
|
from discord import Bot
|
||||||
|
except ImportError as exc:
|
||||||
|
raise ImportError(
|
||||||
|
"You need to install libbot[pycord] in order to use this class."
|
||||||
|
) from exc
|
||||||
|
|
||||||
|
try:
|
||||||
|
from ujson import loads
|
||||||
|
except ImportError:
|
||||||
|
from json import loads
|
||||||
|
|
||||||
|
from libbot.i18n import BotLocale
|
||||||
|
from libbot.i18n.sync import _
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class PycordBot(Bot):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
config: Union[Dict[str, Any], None] = None,
|
||||||
|
config_path: Union[str, Path] = Path("config.json"),
|
||||||
|
locales_root: Union[str, Path, None] = None,
|
||||||
|
scheduler: Union[AsyncIOScheduler, BackgroundScheduler, None] = None,
|
||||||
|
**kwargs,
|
||||||
|
):
|
||||||
|
if config is None:
|
||||||
|
with open(config_path, "r", encoding="utf-8") as f:
|
||||||
|
self.config: dict = loads(f.read())
|
||||||
|
else:
|
||||||
|
self.config = config
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
debug_guilds=self.config["bot"]["debug_guilds"],
|
||||||
|
owner_ids=self.config["bot"]["owners"],
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.bot_locale: BotLocale = BotLocale(
|
||||||
|
default_locale=self.config["locale"],
|
||||||
|
locales_root=(Path("locale") if locales_root is None else locales_root),
|
||||||
|
)
|
||||||
|
self.default_locale: str = self.bot_locale.default
|
||||||
|
self.locales: dict = self.bot_locale.locales
|
||||||
|
|
||||||
|
self._ = self.bot_locale._
|
||||||
|
self.in_all_locales = self.bot_locale.in_all_locales
|
||||||
|
self.in_every_locale = self.bot_locale.in_every_locale
|
||||||
|
|
||||||
|
self.scheduler: Union[AsyncIOScheduler, BackgroundScheduler, None] = scheduler
|
@@ -103,7 +103,7 @@ class PyroClient(Client):
|
|||||||
|
|
||||||
self.bot_locale: BotLocale = BotLocale(
|
self.bot_locale: BotLocale = BotLocale(
|
||||||
default_locale=self.config["locale"],
|
default_locale=self.config["locale"],
|
||||||
locales_root=(Path("locale") if locales_root is None else locales_root)
|
locales_root=(Path("locale") if locales_root is None else locales_root),
|
||||||
)
|
)
|
||||||
self.default_locale: str = self.bot_locale.default
|
self.default_locale: str = self.bot_locale.default
|
||||||
self.locales: dict = self.bot_locale.locales
|
self.locales: dict = self.bot_locale.locales
|
||||||
@@ -129,6 +129,53 @@ class PyroClient(Client):
|
|||||||
getpid(),
|
getpid(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Register default bot's info
|
||||||
|
try:
|
||||||
|
await self.set_bot_info(
|
||||||
|
name=self._("name", "bot"),
|
||||||
|
about=self._("about", "bot"),
|
||||||
|
description=self._("description", "bot"),
|
||||||
|
lang_code="",
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"Bot's info for the default locale %s has been updated",
|
||||||
|
self.default_locale,
|
||||||
|
)
|
||||||
|
except KeyError:
|
||||||
|
logger.warning(
|
||||||
|
"Default locale %s has incorrect keys or values in bot section",
|
||||||
|
self.default_locale,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Register bot's info for each available locale
|
||||||
|
for locale_code in self.locales:
|
||||||
|
locale = self.locales[locale_code]
|
||||||
|
|
||||||
|
if "metadata" not in locale or ("codes" not in locale["metadata"]):
|
||||||
|
logger.warning(
|
||||||
|
"Locale %s is missing metadata or metadata.codes key", locale_code
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
for code in locale["metadata"]["codes"]:
|
||||||
|
try:
|
||||||
|
await self.set_bot_info(
|
||||||
|
name=locale["bot"]["name"],
|
||||||
|
about=locale["bot"]["about"],
|
||||||
|
description=locale["bot"]["description"],
|
||||||
|
lang_code=code,
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"Bot's info for the locale %s has been updated",
|
||||||
|
self.default_locale,
|
||||||
|
)
|
||||||
|
except KeyError:
|
||||||
|
logger.warning(
|
||||||
|
"Locale %s has incorrect keys or values in bot section",
|
||||||
|
locale_code,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Send a message to the bot's reports chat about the startup
|
||||||
try:
|
try:
|
||||||
await self.send_message(
|
await self.send_message(
|
||||||
chat_id=self.owner
|
chat_id=self.owner
|
||||||
@@ -136,10 +183,13 @@ class PyroClient(Client):
|
|||||||
else self.config["reports"]["chat_id"],
|
else self.config["reports"]["chat_id"],
|
||||||
text=f"Bot started PID `{getpid()}`",
|
text=f"Bot started PID `{getpid()}`",
|
||||||
)
|
)
|
||||||
|
except BadRequest:
|
||||||
|
logger.warning("Unable to send message to report chat.")
|
||||||
|
|
||||||
if self.scheduler is None:
|
if self.scheduler is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Schedule the task to register all commands
|
||||||
if register_commands:
|
if register_commands:
|
||||||
self.scheduler.add_job(
|
self.scheduler.add_job(
|
||||||
self.register_commands,
|
self.register_commands,
|
||||||
@@ -149,8 +199,6 @@ class PyroClient(Client):
|
|||||||
)
|
)
|
||||||
|
|
||||||
self.scheduler.start()
|
self.scheduler.start()
|
||||||
except BadRequest:
|
|
||||||
logger.warning("Unable to send message to report chat.")
|
|
||||||
|
|
||||||
async def stop(self, exit_completely: bool = True) -> None:
|
async def stop(self, exit_completely: bool = True) -> None:
|
||||||
try:
|
try:
|
||||||
@@ -170,16 +218,16 @@ class PyroClient(Client):
|
|||||||
if exit_completely:
|
if exit_completely:
|
||||||
try:
|
try:
|
||||||
exit()
|
exit()
|
||||||
except SystemExit as exp:
|
except SystemExit as exc:
|
||||||
raise SystemExit(
|
raise SystemExit(
|
||||||
"Bot has been shut down, this is not an application error!"
|
"Bot has been shut down, this is not an application error!"
|
||||||
) from exp
|
) from exc
|
||||||
|
|
||||||
async def collect_commands(self) -> Union[List[CommandSet], None]:
|
async def collect_commands(self) -> Union[List[CommandSet], None]:
|
||||||
"""Gather list of the bot's commands
|
"""Gather list of the bot's commands
|
||||||
|
|
||||||
### Returns:
|
### Returns:
|
||||||
* `List[CommandSet]`: List of the commands' sets
|
* `List[CommandSet]`: List of the commands' sets.
|
||||||
"""
|
"""
|
||||||
command_sets = None
|
command_sets = None
|
||||||
|
|
||||||
|
@@ -1,4 +1,16 @@
|
|||||||
{
|
{
|
||||||
|
"metadata": {
|
||||||
|
"flag": "🇬🇧",
|
||||||
|
"name": "English",
|
||||||
|
"codes": [
|
||||||
|
"en"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"bot": {
|
||||||
|
"name": "",
|
||||||
|
"about": "",
|
||||||
|
"description": ""
|
||||||
|
},
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
"messages": {
|
"messages": {
|
||||||
"example": "okay"
|
"example": "okay"
|
||||||
|
@@ -1,4 +1,16 @@
|
|||||||
{
|
{
|
||||||
|
"metadata": {
|
||||||
|
"flag": "🇺🇦",
|
||||||
|
"name": "Українська",
|
||||||
|
"codes": [
|
||||||
|
"uk"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"bot": {
|
||||||
|
"name": "",
|
||||||
|
"about": "",
|
||||||
|
"description": ""
|
||||||
|
},
|
||||||
"foo": "бар",
|
"foo": "бар",
|
||||||
"messages": {
|
"messages": {
|
||||||
"example": "окей"
|
"example": "окей"
|
||||||
|
Reference in New Issue
Block a user