Cog framework instead of dumb way
This commit is contained in:
parent
80db71602b
commit
57074ea2ce
6 changed files with 105 additions and 70 deletions
47
jarvis/__init__.py
Normal file
47
jarvis/__init__.py
Normal file
|
@ -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)
|
28
jarvis/cogs/util.py
Normal file
28
jarvis/cogs/util.py
Normal file
|
@ -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))
|
25
jarvis/utils/__init__.py
Normal file
25
jarvis/utils/__init__.py
Normal file
|
@ -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)]
|
5
run.py
Normal file
5
run.py
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
#!/bin/python3
|
||||||
|
import jarvis
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
jarvis.run()
|
|
@ -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)
|
|
Loading…
Add table
Reference in a new issue