Password generator, lots of cleanup, more dynamic db configs

This commit is contained in:
Zeva Rose 2021-07-31 00:03:03 -06:00
parent e6055e58b8
commit 7b6c399f6f
16 changed files with 127 additions and 34 deletions

View file

@ -3,12 +3,22 @@
client_id: 123456789012345678 client_id: 123456789012345678
logo: alligator2 logo: alligator2
mongo: mongo:
username: user connect:
password: pass username: user
host: localhost password: pass
port: 27017 host: localhost
port: 27017
database: database
urls: urls:
url_name: url url_name: url
url_name2: url2 url_name2: url2
max_messages: 1000 max_messages: 1000
gitlab_token: null gitlab_token: null
cogs:
- list
- of
- enabled
- cogs
- all
- if
- empty

View file

@ -4,13 +4,13 @@ from pathlib import Path
from discord import Intents from discord import Intents
from discord.ext import commands from discord.ext import commands
from discord.utils import find from discord.utils import find
from discord_slash import SlashCommand
from psutil import Process
from jarvis import logo, tasks, utils from jarvis import logo, tasks, utils
from jarvis.config import get_config from jarvis.config import get_config
from jarvis.db import DBManager from jarvis.db import DBManager
from jarvis.events import guild, member, message from jarvis.events import guild, member, message
from psutil import Process
from discord_slash import SlashCommand
if asyncio.get_event_loop().is_closed(): if asyncio.get_event_loop().is_closed():
asyncio.set_event_loop(asyncio.new_event_loop()) asyncio.set_event_loop(asyncio.new_event_loop())
@ -25,11 +25,11 @@ jarvis = commands.Bot(
) )
slash = SlashCommand(jarvis, sync_commands=True, sync_on_cog_reload=True) slash = SlashCommand(jarvis, sync_commands=True, sync_on_cog_reload=True)
jarvis_self = Process() jarvis_self = Process()
__version__ = "1.8.1" __version__ = "1.9.0"
jconfig = get_config()
db = DBManager(get_config().mongo).mongo db = DBManager(jconfig.mongo["connect"]).mongo
jarvis_db = db.jarvis jarvis_db = db[jconfig.mongo["database"]]
@jarvis.event @jarvis.event

View file

@ -15,7 +15,10 @@ from jarvis.utils.permissions import admin_or_permissions
class LockdownCog(CacheCog): class LockdownCog(CacheCog):
def __init__(self, bot: commands.Bot): def __init__(self, bot: commands.Bot):
super().__init__(bot) super().__init__(bot)
self.db = DBManager(get_config().mongo).mongo.jarvis config = get_config()
self.db = DBManager(config.mongo["connect"]).mongo[
config.mongo["database"]
]
@cog_ext.cog_subcommand( @cog_ext.cog_subcommand(
base="lockdown", base="lockdown",

View file

@ -30,7 +30,7 @@ class CTCCog(CacheCog):
def __init__(self, bot): def __init__(self, bot):
super().__init__(bot) super().__init__(bot)
mconf = get_config().mongo mconf = get_config().mongo
self.db = DBManager(mconf).mongo self.db = DBManager(mconf["connect"]).mongo
self._session = aiohttp.ClientSession() self._session = aiohttp.ClientSession()
self.url = "https://completethecodetwo.cards/pw" self.url = "https://completethecodetwo.cards/pw"

View file

@ -28,7 +28,8 @@ class ErrorHandlerCog(commands.Cog):
return return
elif isinstance(error, commands.errors.CommandOnCooldown): elif isinstance(error, commands.errors.CommandOnCooldown):
await ctx.send( await ctx.send(
"Command on cooldown. Please wait before trying again", "Command on cooldown. "
+ f"Please wait {error.retry_after:0.2f}s before trying again",
hidden=True, hidden=True,
) )
else: else:

View file

@ -8,6 +8,7 @@ from discord.ext import commands
from discord_slash import cog_ext from discord_slash import cog_ext
import jarvis import jarvis
from jarvis.config import get_config
from jarvis.db import DBManager from jarvis.db import DBManager
from jarvis.utils import build_embed from jarvis.utils import build_embed
from jarvis.utils.field import Field from jarvis.utils.field import Field
@ -22,8 +23,10 @@ class JokeCog(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
config = jarvis.config.get_config() config = get_config()
self.db = DBManager(config.mongo).mongo.jarvis self.db = DBManager(config.mongo["connect"]).mongo[
config.mongo["database"]
]
# TODO: Make this a command group with subcommands # TODO: Make this a command group with subcommands
async def _joke(self, ctx, id: str = None): async def _joke(self, ctx, id: str = None):

View file

@ -13,7 +13,7 @@ class ModlogCommandCog(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_slash_command(self, ctx: SlashContext): async def on_slash_command(self, ctx: SlashContext):
if not isinstance(ctx.channel, DMChannel): if not isinstance(ctx.channel, DMChannel) and ctx.name not in ["pw"]:
modlog = Setting.get(guild=ctx.guild.id, setting="modlog") modlog = Setting.get(guild=ctx.guild.id, setting="modlog")
if modlog: if modlog:
channel = ctx.guild.get_channel(modlog.value) channel = ctx.guild.get_channel(modlog.value)

View file

@ -2,12 +2,12 @@ from datetime import datetime, timedelta
import discord import discord
from discord.utils import find from discord.utils import find
from jarvis.utils import build_embed from jarvis.utils import build_embed
from jarvis.utils.field import Field from jarvis.utils.field import Field
def modlog_embed( def modlog_embed(
self,
member: discord.Member, member: discord.Member,
admin: discord.Member, admin: discord.Member,
log: discord.AuditLogEntry, log: discord.AuditLogEntry,

View file

@ -35,7 +35,11 @@ class OwnerCog(commands.Cog):
or ctx.message.author.id in self.admins or ctx.message.author.id in self.admins
): ):
try: try:
if "jarvis.cogs." not in cog:
cog = "jarvis.cogs." + cog.split(".")[-1]
self.bot.load_extension(cog) self.bot.load_extension(cog)
except commands.errors.ExtensionAlreadyLoaded:
await ctx.send(f"Cog `{cog}` already loaded")
except Exception as e: except Exception as e:
await ctx.send( await ctx.send(
f"Failed to load new cog `{cog}`: {type(e).name} - {e}" f"Failed to load new cog `{cog}`: {type(e).name} - {e}"
@ -48,7 +52,7 @@ class OwnerCog(commands.Cog):
@commands.command(name="unload", hidden=True) @commands.command(name="unload", hidden=True)
@user_is_bot_admin() @user_is_bot_admin()
async def _unload_cog(self, ctx, *, cog: str): async def _unload_cog(self, ctx, *, cog: str):
if cog == "jarvis.cogs.owner": if cog in ["jarvis.cogs.owner", "owner"]:
await ctx.send("Cannot unload `owner` cog") await ctx.send("Cannot unload `owner` cog")
return return
info = await self.bot.application_info() info = await self.bot.application_info()
@ -57,7 +61,11 @@ class OwnerCog(commands.Cog):
or ctx.message.author.id in self.admins or ctx.message.author.id in self.admins
): ):
try: try:
if "jarvis.cogs." not in cog:
cog = "jarvis.cogs." + cog.split(".")[-1]
self.bot.unload_extension(cog) self.bot.unload_extension(cog)
except commands.errors.ExtensionNotLoaded:
await ctx.send(f"Cog `{cog}` not loaded")
except Exception as e: except Exception as e:
await ctx.send( await ctx.send(
f"Failed to unload cog `{cog}` {type(e).__name__} - {e}" f"Failed to unload cog `{cog}` {type(e).__name__} - {e}"
@ -70,7 +78,7 @@ class OwnerCog(commands.Cog):
@commands.command(name="reload", hidden=True) @commands.command(name="reload", hidden=True)
@user_is_bot_admin() @user_is_bot_admin()
async def _cog_reload(self, ctx, *, cog: str): async def _cog_reload(self, ctx, *, cog: str):
if cog == "jarvis.cogs.owner": if cog in ["jarvis.cogs.owner", "owner"]:
await ctx.send("Cannot reload `owner` cog") await ctx.send("Cannot reload `owner` cog")
return return
info = await self.bot.application_info() info = await self.bot.application_info()
@ -79,8 +87,13 @@ class OwnerCog(commands.Cog):
or ctx.message.author.id in self.admins or ctx.message.author.id in self.admins
): ):
try: try:
if "jarvis.cogs." not in cog:
cog = "jarvis.cogs." + cog.split(".")[-1]
try:
self.bot.load_extension(cog)
except commands.errors.ExtensionNotLoaded:
pass
self.bot.unload_extension(cog) self.bot.unload_extension(cog)
self.bot.load_extension(cog)
except Exception as e: except Exception as e:
await ctx.send( await ctx.send(
f"Failed to reload cog `{cog}` {type(e).__name__} - {e}" f"Failed to reload cog `{cog}` {type(e).__name__} - {e}"

View file

@ -1,14 +1,17 @@
import re import re
import secrets
import string
from io import BytesIO from io import BytesIO
from discord import File, Guild, Role, User from discord import File, Guild, Role, User
from discord.ext import commands from discord.ext import commands
from discord_slash import SlashContext, cog_ext from discord_slash import SlashContext, cog_ext
from discord_slash.utils.manage_commands import create_option from discord_slash.utils.manage_commands import create_choice, create_option
from PIL import Image, ImageDraw from PIL import Image, ImageDraw
import jarvis import jarvis
from jarvis import config, jarvis_self, logo from jarvis import jarvis_self, logo
from jarvis.config import get_config
from jarvis.data.robotcamo import emotes, names from jarvis.data.robotcamo import emotes, names
from jarvis.utils import build_embed, convert_bytesize, get_repo_hash from jarvis.utils import build_embed, convert_bytesize, get_repo_hash
from jarvis.utils.field import Field from jarvis.utils.field import Field
@ -23,7 +26,7 @@ class UtilCog(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
self.config = config.get_config() self.config = get_config()
@cog_ext.cog_slash( @cog_ext.cog_slash(
name="status", name="status",
@ -267,6 +270,57 @@ class UtilCog(commands.Cog):
await ctx.send(embed=embed) await ctx.send(embed=embed)
@cog_ext.cog_subcommand(
base="pw",
name="gen",
base_desc="Password utilites",
description="Generate a secure password",
guild_ids=[862402786116763668],
options=[
create_option(
name="length",
description="Password length (default 32)",
option_type=4,
required=False,
),
create_option(
name="chars",
description="Characters to include (default last option)",
option_type=4,
required=False,
choices=[
create_choice(name="A-Za-z", value=0),
create_choice(name="A-Fa-f0-9", value=1),
create_choice(name="A-Za-z0-9", value=2),
create_choice(name="A-Za-z0-9!@#$%^&*", value=3),
],
),
],
)
@commands.cooldown(1, 15, type=commands.BucketType.user)
async def _pw_gen(
self, ctx: SlashContext, length: int = 32, chars: int = 3
):
if length > 256:
await ctx.send(
"Please limit password to 256 characters", hidden=True
)
return
choices = [
string.ascii_letters,
string.hexdigits,
string.ascii_letters + string.digits,
string.ascii_letters + string.digits + "!@#$%^&*",
]
pw = "".join(secrets.choice(choices[chars]) for i in range(length))
await ctx.send(
f"Generated password:\n`{pw}`\n\n"
+ '**WARNING: Once you press "Dismiss Message", '
+ "*the password is lost forever***",
hidden=True,
)
def setup(bot): def setup(bot):
bot.add_cog(UtilCog(bot)) bot.add_cog(UtilCog(bot))

View file

@ -24,6 +24,7 @@ class Config(object):
logo: str, logo: str,
mongo: dict, mongo: dict,
urls: dict, urls: dict,
cogs: list = None,
gitlab_token: str = None, gitlab_token: str = None,
max_messages: int = 1000, max_messages: int = 1000,
): ):
@ -32,9 +33,10 @@ class Config(object):
self.logo = logo self.logo = logo
self.mongo = mongo self.mongo = mongo
self.urls = urls self.urls = urls
self.cogs = cogs
self.max_messages = max_messages self.max_messages = max_messages
self.gitlab_token = gitlab_token self.gitlab_token = gitlab_token
db = DBManager(config=mongo).mongo.jarvis.config db = DBManager(config=mongo["connect"]).mongo[mongo["database"]].config
db_config = db.find() db_config = db.find()
for item in db_config: for item in db_config:
setattr(self, item["key"], item["value"]) setattr(self, item["key"], item["value"])

View file

@ -4,9 +4,10 @@ from datetime import datetime
from typing import Any, Optional from typing import Any, Optional
from bson import ObjectId from bson import ObjectId
from pymongo import ASCENDING, DESCENDING
from jarvis.config import get_config from jarvis.config import get_config
from jarvis.db import DBManager from jarvis.db import DBManager
from pymongo import ASCENDING, DESCENDING
logger = logging.getLogger("mongodb") logger = logging.getLogger("mongodb")
@ -35,7 +36,9 @@ coll_lookup = {
"Warning": "warns", "Warning": "warns",
} }
db_instance = DBManager(get_config().mongo).mongo.jarvis m_config = get_config().mongo
db_instance = DBManager(m_config["connect"]).mongo[m_config["database"]]
################# #################

View file

@ -13,8 +13,9 @@ class GuildEventHandler(object):
async def on_guild_join(self, guild): async def on_guild_join(self, guild):
general = find(lambda x: x.name == "general", guild.channels) general = find(lambda x: x.name == "general", guild.channels)
if general and general.permissions_for(guild.me).send_messages: if general and general.permissions_for(guild.me).send_messages:
user = self.bot.user
await general.send( await general.send(
"Allow me to introduce myself. I am J.A.R.V.I.S., a virtual " f"Allow me to introduce myself. I am {user.mention}, a virtual "
+ "artificial intelligence, and I'm here to assist you with a " + "artificial intelligence, and I'm here to assist you with a "
+ "variety of tasks as best I can, " + "variety of tasks as best I can, "
+ "24 hours a day, seven days a week." + "24 hours a day, seven days a week."

View file

@ -6,8 +6,8 @@ from discord import Color, Embed
from discord.ext import commands from discord.ext import commands
import jarvis.cogs import jarvis.cogs
import jarvis.config
import jarvis.db import jarvis.db
from jarvis.config import get_config
__all__ = ["field", "db", "cachecog", "permissions"] __all__ = ["field", "db", "cachecog", "permissions"]
@ -40,7 +40,9 @@ def get_prefix(bot, message):
def get_extensions(path=jarvis.cogs.__path__) -> list: def get_extensions(path=jarvis.cogs.__path__) -> list:
return ["jarvis.cogs.{}".format(x.name) for x in iter_modules(path)] config = get_config()
vals = config.cogs or [x.name for x in iter_modules(path)]
return ["jarvis.cogs.{}".format(x) for x in vals]
def parse_color_hex(hex: str) -> Color: def parse_color_hex(hex: str) -> Color:

View file

@ -1,12 +1,12 @@
from discord.ext import commands from discord.ext import commands
import jarvis from jarvis.config import get_config
def user_is_bot_admin(): def user_is_bot_admin():
def predicate(ctx): def predicate(ctx):
if getattr(jarvis.config.get_config(), "admins", None): if getattr(get_config(), "admins", None):
return ctx.author.id in jarvis.config.get_config().admins return ctx.author.id in get_config().admins
else: else:
return False return False

3
run.py
View file

@ -7,6 +7,7 @@ from time import sleep
import git import git
import jarvis import jarvis
from jarvis.config import get_config
def run(): def run():
@ -93,7 +94,7 @@ Command List:
if __name__ == "__main__": if __name__ == "__main__":
freeze_support() freeze_support()
config = jarvis.config.get_config() config = get_config()
pid_file = Value("i", 0) pid_file = Value("i", 0)
jarvis_process = Process(target=run, name="jarvis") jarvis_process = Process(target=run, name="jarvis")
logo = jarvis.logo.get_logo(config.logo) logo = jarvis.logo.get_logo(config.logo)