From 57074ea2ce6fde3c990ae083fa4754804928cd67 Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Thu, 24 Jun 2021 16:14:19 -0600 Subject: [PATCH] Cog framework instead of dumb way --- jarvis/__init__.py | 47 ++++++++++++++++++++++++++ jarvis/cogs/util.py | 28 ++++++++++++++++ {src => jarvis}/config.py | 0 jarvis/utils/__init__.py | 25 ++++++++++++++ run.py | 5 +++ src/jarvis.py | 70 --------------------------------------- 6 files changed, 105 insertions(+), 70 deletions(-) create mode 100644 jarvis/__init__.py create mode 100644 jarvis/cogs/util.py rename {src => jarvis}/config.py (100%) create mode 100644 jarvis/utils/__init__.py create mode 100644 run.py delete mode 100644 src/jarvis.py diff --git a/jarvis/__init__.py b/jarvis/__init__.py new file mode 100644 index 0000000..10290fc --- /dev/null +++ b/jarvis/__init__.py @@ -0,0 +1,47 @@ +from discord import Intents +from discord.ext import commands +from discord.utils import find +from psutil import Process +from asyncio import sleep + +from jarvis.config import get_config +from jarvis import utils + +intents = Intents.default() +intents.members = True + +jarvis = commands.Bot(command_prefix=utils.get_prefix, intents=intents) +jarvis_self = Process() + + +@jarvis.event +async def on_ready(): + print("Logged in as {0.user}".format(jarvis)) + print("Connected to {} guild(s)".format(len(jarvis.guilds))) + + +@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 sleep(1) + await general.send("Importing all preferences from home interface...") + await sleep(5) + await general.send("Systems are now fully operational") + + +def run(): + 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) diff --git a/jarvis/cogs/util.py b/jarvis/cogs/util.py new file mode 100644 index 0000000..e8d18af --- /dev/null +++ b/jarvis/cogs/util.py @@ -0,0 +1,28 @@ +from jarvis import jarvis_self +from jarvis.utils import convert_bytesize +from discord import Embed, Color +from discord.ext import commands + + +class UtilCog(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.command(name="status", help="Retrieve J.A.R.V.I.S. status") + async def _status(self, ctx): + title = "J.A.R.V.I.S. Status" + description = "All systems online" + color = Color.from_rgb(152, 204, 218) + embed = Embed(title=title, description=description, color=color) + with jarvis_self.oneshot(): + embed.add_field(name="CPU Usage", value=jarvis_self.cpu_percent()) + embed.add_field( + name="RAM Usage", + value=convert_bytesize(jarvis_self.memory_info().rss), + ) + embed.add_field(name="PID", value=jarvis_self.pid) + await ctx.send(embed=embed) + + +def setup(bot): + bot.add_cog(UtilCog(bot)) diff --git a/src/config.py b/jarvis/config.py similarity index 100% rename from src/config.py rename to jarvis/config.py diff --git a/jarvis/utils/__init__.py b/jarvis/utils/__init__.py new file mode 100644 index 0000000..8e6fdf7 --- /dev/null +++ b/jarvis/utils/__init__.py @@ -0,0 +1,25 @@ +from discord.ext import commands +from pkgutil import iter_modules +import jarvis.cogs + + +def convert_bytesize(bytes: int) -> str: + bytes = float(bytes) + sizes = ["B", "KB", "MB", "GB"] + size = 0 + while bytes >= 1024 and size < len(sizes) - 1: + bytes = bytes / 1024 + size += 1 + return f"{bytes:0.2f}{sizes[size]}" + + +def get_prefix(bot, message): + prefixes = ["$", ">", "?", "!"] + if not message.guild: + return "?" + + return commands.when_mentioned_or(*prefixes)(bot, message) + + +def get_extensions(path=jarvis.cogs.__path__) -> list: + return ["jarvis.cogs.{}".format(x.name) for x in iter_modules(path)] diff --git a/run.py b/run.py new file mode 100644 index 0000000..37cd17e --- /dev/null +++ b/run.py @@ -0,0 +1,5 @@ +#!/bin/python3 +import jarvis + +if __name__ == "__main__": + jarvis.run() diff --git a/src/jarvis.py b/src/jarvis.py deleted file mode 100644 index 841eff8..0000000 --- a/src/jarvis.py +++ /dev/null @@ -1,70 +0,0 @@ -from discord import Embed, Color, Intents -from discord.ext import commands -from discord.utils import find -from psutil import Process -from asyncio import sleep -from config import get_config - -intents = Intents.default() -intents.members = True - -jarvis = commands.Bot(command_prefix=">", intents=intents) -jarvis_self = Process() - - -def convert_bytesize(bytes: int) -> str: - bytes = float(bytes) - sizes = ["B", "KB", "MB", "GB"] - size = 0 - while bytes >= 1024 and size < len(sizes) - 1: - bytes = bytes / 1024 - size += 1 - return f"{bytes:0.2f}{sizes[size]}" - - -@jarvis.event -async def on_ready(): - print("Logged in as {0.user}".format(jarvis)) - print("Connected to {} guild(s)".format(len(jarvis.guilds))) - - -@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 sleep(1) - await general.send("Importing all preferences from home interface...") - await sleep(5) - await general.send("Systems are now fully operational") - - -@jarvis.command(name="status", help="Retrieve J.A.R.V.I.S. status") -async def _status(ctx): - title = "J.A.R.V.I.S. Status" - description = "All systems online" - color = Color.from_rgb(152, 204, 218) - embed = Embed(title=title, description=description, color=color) - with jarvis_self.oneshot(): - embed.add_field(name="CPU Usage", value=jarvis_self.cpu_percent()) - embed.add_field( - name="RAM Usage", - value=convert_bytesize(jarvis_self.memory_info().rss), - ) - embed.add_field(name="PID", value=jarvis_self.pid) - await ctx.send(embed=embed) - - -if __name__ == "__main__": - config = get_config() - print( - "https://discord.com/api/oauth2/authorize?client_id={}&permissions=8&scope=bot".format( - config.client_id - ) - ) - jarvis.run(config.token)