Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update __main__.py #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 14 additions & 30 deletions DaisyX/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import asyncio
import os
from importlib import import_module

from aiogram import executor
Expand All @@ -25,14 +22,15 @@
from DaisyX.modules import ALL_MODULES, LOADED_MODULES, MOD_HELP
from DaisyX.utils.logger import log


if get_bool_key("DEBUG_MODE"):
log.debug("Enabling logging middleware.")
dp.middleware.setup(LoggingMiddleware())

LOAD = get_list_key("LOAD")
DONT_LOAD = get_list_key("DONT_LOAD")

if get_bool_key("LOAD_MODULES"):
if get_bool_key('LOAD_MODULES'):
if len(LOAD) > 0:
modules = LOAD
else:
Expand All @@ -42,13 +40,13 @@

log.info("Modules to load: %s", str(modules))
for module_name in modules:
# Load pm_menu at last
if module_name == "pm_menu":
# Load pm_menu at last
if module_name == 'pm_menu':
continue
log.debug(f"Importing <d><n>{module_name}</></>")
imported_module = import_module("DaisyX.modules." + module_name)
if hasattr(imported_module, "__help__"):
if hasattr(imported_module, "__mod_name__"):
if hasattr(imported_module, '__help__'):
if hasattr(imported_module, '__mod_name__'):
MOD_HELP[imported_module.__mod_name__] = imported_module.__help__
else:
MOD_HELP[imported_module.__name__] = imported_module.__help__
Expand All @@ -61,17 +59,21 @@

import_module("DaisyX.modules.pm_menu")
# Import misc stuff

import_module("DaisyX.utils.exit_gracefully")
if not get_bool_key("DEBUG_MODE"):
if not get_bool_key('DEBUG_MODE'):
import_module("DaisyX.utils.sentry")


async def before_srv_task(loop):
for module in [m for m in LOADED_MODULES if hasattr(m, "__before_serving__")]:
log.debug("Before serving: " + module.__name__)
for module in [m for m in LOADED_MODULES if hasattr(m, '__before_serving__')]:
log.debug('Before serving: ' + module.__name__)
loop.create_task(module.__before_serving__(loop))


import_module("DaisyX.utils.db_structure_migrator")


async def start(_):
log.debug("Starting before serving task for all modules...")
loop.create_task(before_srv_task(loop))
Expand All @@ -81,25 +83,7 @@ async def start(_):
await asyncio.sleep(2)


async def start_webhooks(_):
url = os.getenv("WEBHOOK_URL") + f"/{TOKEN}"
await bot.set_webhook(url)
return await start(_)


log.info("Starting loop..")
log.info("Aiogram: Using polling method")

if os.getenv("WEBHOOKS", False):
port = os.getenv("WEBHOOKS_PORT", 8080)
executor.start_webhook(dp, f"/{TOKEN}", on_startup=start_webhooks, port=port)
else:
executor.start_polling(
dp,
loop=loop,
on_startup=start,
timeout=15,
relax=0.1,
fast=True,
skip_updates=True,
)
executor.start_polling(dp, loop=loop, on_startup=start)