113 lines
3.3 KiB
Python
113 lines
3.3 KiB
Python
"""Main J.A.R.V.I.S. package."""
|
|
import asyncio
|
|
import logging
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
from discord import Intents
|
|
from discord.ext import commands
|
|
from discord.utils import find
|
|
from discord_slash import SlashCommand
|
|
from mongoengine import connect
|
|
from psutil import Process
|
|
|
|
from jarvis import logo # noqa: F401
|
|
from jarvis import tasks
|
|
from jarvis import utils
|
|
from jarvis.config import get_config
|
|
from jarvis.events import guild
|
|
from jarvis.events import member
|
|
from jarvis.events import message
|
|
|
|
jconfig = get_config()
|
|
|
|
logger = logging.getLogger("discord")
|
|
logger.setLevel(logging.getLevelName(jconfig.log_level))
|
|
file_handler = logging.FileHandler(filename="jarvis.log", encoding="UTF-8", mode="w")
|
|
file_handler.setFormatter(logging.Formatter("[%(asctime)s][%(levelname)s][%(name)s] %(message)s"))
|
|
logger.addHandler(file_handler)
|
|
|
|
if asyncio.get_event_loop().is_closed():
|
|
asyncio.set_event_loop(asyncio.new_event_loop())
|
|
|
|
intents = Intents.default()
|
|
intents.members = True
|
|
restart_ctx = None
|
|
|
|
|
|
jarvis = commands.Bot(
|
|
command_prefix=utils.get_prefix,
|
|
intents=intents,
|
|
help_command=None,
|
|
max_messages=jconfig.max_messages,
|
|
)
|
|
|
|
slash = SlashCommand(jarvis, sync_commands=True, sync_on_cog_reload=True)
|
|
jarvis_self = Process()
|
|
__version__ = "1.10.3"
|
|
|
|
|
|
@jarvis.event
|
|
async def on_ready() -> None:
|
|
"""d.py on_ready override."""
|
|
global restart_ctx
|
|
print(" Logged in as {0.user}".format(jarvis))
|
|
print(" Connected to {} guild(s)".format(len(jarvis.guilds)))
|
|
with jarvis_self.oneshot():
|
|
print(f" Current PID: {jarvis_self.pid}")
|
|
Path(f"jarvis.{jarvis_self.pid}.pid").touch()
|
|
if restart_ctx:
|
|
channel = None
|
|
if "guild" in restart_ctx:
|
|
guild = find(lambda x: x.id == restart_ctx["guild"], jarvis.guilds)
|
|
if guild:
|
|
channel = find(lambda x: x.id == restart_ctx["channel"], guild.channels)
|
|
elif "user" in restart_ctx:
|
|
channel = jarvis.get_user(restart_ctx["user"])
|
|
if channel:
|
|
await channel.send("Core systems restarted and back online.")
|
|
restart_ctx = None
|
|
|
|
|
|
def run(ctx: dict = None) -> Optional[dict]:
|
|
"""Run J.A.R.V.I.S."""
|
|
global restart_ctx
|
|
if ctx:
|
|
restart_ctx = ctx
|
|
connect(
|
|
db="ctc2",
|
|
alias="ctc2",
|
|
authentication_source="admin",
|
|
**jconfig.mongo["connect"],
|
|
)
|
|
connect(
|
|
db=jconfig.mongo["database"],
|
|
alias="main",
|
|
authentication_source="admin",
|
|
**jconfig.mongo["connect"],
|
|
)
|
|
jconfig.get_db_config()
|
|
for extension in utils.get_extensions():
|
|
jarvis.load_extension(extension)
|
|
print(
|
|
" https://discord.com/api/oauth2/authorize?client_id="
|
|
+ "{}&permissions=8&scope=bot%20applications.commands".format(jconfig.client_id) # noqa: W503
|
|
)
|
|
|
|
jarvis.max_messages = jconfig.max_messages
|
|
tasks.init()
|
|
|
|
# Add event listeners
|
|
if jconfig.events:
|
|
_ = [
|
|
guild.GuildEventHandler(jarvis),
|
|
member.MemberEventHandler(jarvis),
|
|
message.MessageEventHandler(jarvis),
|
|
]
|
|
jarvis.run(jconfig.token, bot=True, reconnect=True)
|
|
for cog in jarvis.cogs:
|
|
session = getattr(cog, "_session", None)
|
|
if session:
|
|
session.close()
|
|
if restart_ctx:
|
|
return restart_ctx
|