-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path__main__.py
60 lines (47 loc) · 1.74 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import asyncio
import importlib
import inspect
import pkgutil
import socket
from typing import Iterator, NoReturn
import aiohttp
from discord import AllowedMentions, Intents
from bot import cogs
from bot import settings
from bot.bot import Friendo
def _get_cogs() -> Iterator[str]:
"""
Return an iterator going through each cog.
On each iteration the cog is check for having a setup function raising a more readable error if not found.
"""
def on_error(name: str) -> NoReturn:
raise ImportError(name=name)
for module in pkgutil.walk_packages(cogs.__path__, f"{cogs.__name__}.", onerror=on_error):
if any(name.startswith("_") for name in module.name.split(".")):
continue # Ignore modules/packages with a name starting with _
if module.ispkg:
imported = importlib.import_module(module.name)
if not inspect.isfunction(getattr(imported, "setup", None)):
continue
yield module.name
async def start_bot() -> None:
"""Load in extensions and start running the bot."""
resolver = aiohttp.AsyncResolver()
connector = aiohttp.TCPConnector(
resolver=resolver,
family=socket.AF_INET,
)
async with aiohttp.ClientSession(connector=connector) as session:
bot = Friendo(
command_prefix=settings.COMMAND_PREFIX, help_command=None, intents=Intents.all(),
allowed_mentions=AllowedMentions(everyone=False),
session=session,
connector=connector,
resolver=resolver,
)
for cog in _get_cogs():
await bot.load_extension(cog)
async with bot:
await bot.start(settings.TOKEN)
if __name__ == "__main__":
asyncio.run(start_bot())