jarvis-bot/jarvis/__init__.py
2021-06-25 12:28:50 -06:00

76 lines
2.4 KiB
Python

from pathlib import Path
from discord import Intents
from discord.ext import commands
from discord.utils import find
from psutil import Process
import asyncio
from jarvis.config import get_config
from jarvis import utils
from jarvis import logo
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)
jarvis_self = Process()
__version__ = "0.1.0"
@jarvis.event
async def on_ready():
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
@jarvis.event
async def on_guild_join(guild):
general = find(lambda x: x.name == "general", guild.channels)
if general and general.permissions_for(guild.me).send_messages:
await general.send(
"Allow me to introduce myself. I am J.A.R.V.I.S., a virtual "
+ "artificial intelligence, and I'm here to assist you with a "
+ "variety of tasks as best I can, "
+ "24 hours a day, seven days a week."
)
await asyncio.sleep(1)
await general.send("Importing all preferences from home interface...")
await asyncio.sleep(5)
await general.send("Systems are now fully operational")
def run(ctx=None):
global restart_ctx
if ctx:
restart_ctx = ctx
for extension in utils.get_extensions():
jarvis.load_extension(extension)
config = get_config()
print(
" https://discord.com/api/oauth2/authorize?client_id="
+ "{}&permissions=8&scope=bot".format(config.client_id)
)
jarvis.run(config.token, bot=True, reconnect=True)
if restart_ctx:
return restart_ctx