diff --git a/jarvis/cogs/admin/kick.py b/jarvis/cogs/admin/kick.py index eb09b2a..3bb7cfb 100644 --- a/jarvis/cogs/admin/kick.py +++ b/jarvis/cogs/admin/kick.py @@ -8,8 +8,8 @@ from dis_snek.models.snek.application_commands import ( slash_option, ) from dis_snek.models.snek.command import check +from jarvis_core.db.models import Kick -from jarvis.db.models import Kick from jarvis.utils import build_embed from jarvis.utils.permissions import admin_or_permissions @@ -65,10 +65,11 @@ class KickCog(Scale): embed.set_thumbnail(url=user.display_avatar.url) embed.set_footer(text=f"{user.username}#{user.discriminator} | {user.id}") - _ = Kick( + k = Kick( user=user.id, reason=reason, admin=ctx.author.id, guild=ctx.guild.id, - ).save() + ) + await k.commit() await ctx.send(embed=embed) diff --git a/jarvis/cogs/admin/mute.py b/jarvis/cogs/admin/mute.py index 9733e6e..13aa948 100644 --- a/jarvis/cogs/admin/mute.py +++ b/jarvis/cogs/admin/mute.py @@ -74,14 +74,15 @@ class MuteCog(Scale): return await user.timeout(communication_disabled_until=duration, reason=reason) - _ = Mute( + m = Mute( user=user.id, reason=reason, admin=ctx.author.id, guild=ctx.guild.id, duration=duration, active=True, - ).save() + ) + await m.commit() embed = build_embed( title="User Muted", diff --git a/jarvis/cogs/admin/purge.py b/jarvis/cogs/admin/purge.py index 4a893fb..a5a73c6 100644 --- a/jarvis/cogs/admin/purge.py +++ b/jarvis/cogs/admin/purge.py @@ -37,12 +37,13 @@ class PurgeCog(Scale): async for message in ctx.channel.history(limit=amount + 1): messages.append(message) await ctx.channel.delete_messages(messages, reason=f"Purge by {ctx.author.username}") - _ = Purge( + p = Purge( channel=ctx.channel.id, guild=ctx.guild.id, admin=ctx.author.id, count=amount, - ).save() + ) + await p.commit() @slash_command( name="autopurge", sub_cmd_name="add", sub_cmd_description="Automatically purge messages" @@ -78,12 +79,13 @@ class PurgeCog(Scale): await ctx.send("Autopurge already exists.", ephemeral=True) return - _ = Autopurge( + p = Autopurge( guild=ctx.guild.id, channel=channel.id, admin=ctx.author.id, delay=delay, - ).save() + ) + await p.commit() await ctx.send(f"Autopurge set up on {channel.mention}, delay is {delay} seconds") @@ -98,11 +100,11 @@ class PurgeCog(Scale): ) @check(admin_or_permissions(Permissions.MANAGE_MESSAGES)) async def _autopurge_remove(self, ctx: InteractionContext, channel: GuildText) -> None: - autopurge = Autopurge.objects(guild=ctx.guild.id, channel=channel.id) + autopurge = await Autopurge.find_one(q(guild=ctx.guild.id, channel=channel.id)) if not autopurge: await ctx.send("Autopurge does not exist.", ephemeral=True) return - autopurge.delete() + await autopurge.delete() await ctx.send(f"Autopurge removed from {channel.mention}.") @slash_command( @@ -126,12 +128,12 @@ class PurgeCog(Scale): async def _autopurge_update( self, ctx: InteractionContext, channel: GuildText, delay: int ) -> None: - autopurge = Autopurge.objects(guild=ctx.guild.id, channel=channel.id) + autopurge = await Autopurge.find_one(q(guild=ctx.guild.id, channel=channel.id)) if not autopurge: await ctx.send("Autopurge does not exist.", ephemeral=True) return autopurge.delay = delay - autopurge.save() + await autopurge.commit() await ctx.send(f"Autopurge delay updated to {delay} seconds on {channel.mention}.") diff --git a/jarvis/cogs/remindme.py b/jarvis/cogs/remindme.py index 9639485..e7a90b9 100644 --- a/jarvis/cogs/remindme.py +++ b/jarvis/cogs/remindme.py @@ -6,6 +6,7 @@ from typing import List from bson import ObjectId from dis_snek import InteractionContext, Snake +from dis_snek.client.utils.misc_utils import get from dis_snek.models.discord.channel import GuildChannel from dis_snek.models.discord.components import ActionRow, Select, SelectOption from dis_snek.models.discord.embed import Embed, EmbedField @@ -15,8 +16,9 @@ from dis_snek.models.snek.application_commands import ( slash_command, slash_option, ) +from jarvis_core.db import q +from jarvis_core.db.models import Reminder -from jarvis.db.models import Reminder from jarvis.utils import build_embed from jarvis.utils.cachecog import CacheCog @@ -94,7 +96,7 @@ class RemindmeCog(CacheCog): await ctx.send("At least one time period is required", ephemeral=True) return - reminders = Reminder.objects(user=ctx.author.id, active=True).count() + reminders = len(await Reminder.find(q(user=ctx.author.id, active=True))) if reminders >= 5: await ctx.send( "You already have 5 (or more) active reminders. " @@ -105,7 +107,7 @@ class RemindmeCog(CacheCog): remind_at = datetime.now() + timedelta(**delta) - _ = Reminder( + r = Reminder( user=ctx.author_id, channel=ctx.channel.id, guild=ctx.guild.id, @@ -113,7 +115,9 @@ class RemindmeCog(CacheCog): remind_at=remind_at, private=private, active=True, - ).save() + ) + + await r.commit() embed = build_embed( title="Reminder Set", @@ -183,7 +187,7 @@ class RemindmeCog(CacheCog): ephemeral=True, ) return - reminders = Reminder.objects(user=ctx.author.id, active=True) + reminders = await Reminder.find(q(user=ctx.author.id, active=True)) if not reminders: await ctx.send("You have no reminders set.", ephemeral=True) return @@ -194,7 +198,7 @@ class RemindmeCog(CacheCog): @slash_command(name="reminders", sub_cmd_name="delete", sub_cmd_description="Delete a reminder") async def _delete(self, ctx: InteractionContext) -> None: - reminders = Reminder.objects(user=ctx.author.id, active=True) + reminders = await Reminder.find(q(user=ctx.author.id, active=True)) if not reminders: await ctx.send("You have no reminders set", ephemeral=True) return @@ -230,15 +234,10 @@ class RemindmeCog(CacheCog): messages=message, timeout=60 * 5, ) - for to_delete in context.context.values: - _ = Reminder.objects(user=ctx.author.id, id=ObjectId(to_delete)).delete() - - for row in components: - for component in row.components: - component.disabled = True fields = [] - for reminder in filter(lambda x: str(x.id) in context.context.values, reminders): + for to_delete in context.context.values: + reminder = get(reminders, user=ctx.author.id, id=ObjectId(to_delete)) if reminder.private and isinstance(ctx.channel, GuildChannel): fields.append( EmbedField( @@ -255,6 +254,12 @@ class RemindmeCog(CacheCog): inline=False, ) ) + await reminder.delete() + + for row in components: + for component in row.components: + component.disabled = True + embed = build_embed( title="Deleted Reminder(s)", description="", diff --git a/jarvis/config.py b/jarvis/config.py index c2b9983..e8d65bd 100644 --- a/jarvis/config.py +++ b/jarvis/config.py @@ -1,6 +1,7 @@ """Load the config for J.A.R.V.I.S.""" import os +from jarvis_core.config import Config as CConfig from pymongo import MongoClient from yaml import load @@ -10,6 +11,19 @@ except ImportError: from yaml import Loader +class JarvisConfig(CConfig): + REQUIRED = ["token", "client_id", "mongo", "urls"] + OPTIONAL = { + "sync": False, + "log_level": "WARNING", + "scales": None, + "events": True, + "gitlab_token": None, + "max_messages": 1000, + "twitter": None, + } + + class Config(object): """Config singleton object for J.A.R.V.I.S.""" diff --git a/jarvis/tasks/reminder.py b/jarvis/tasks/reminder.py index 98385ab..eba3a95 100644 --- a/jarvis/tasks/reminder.py +++ b/jarvis/tasks/reminder.py @@ -1,8 +1,8 @@ """J.A.R.V.I.S. reminder background task handler.""" from datetime import datetime, timedelta -from dis_snek.ext.tasks.task import Task -from dis_snek.ext.tasks.triggers import IntervalTrigger +from dis_snek.models.snek.tasks.task import Task +from dis_snek.models.snek.tasks.triggers import IntervalTrigger from jarvis_core.db import q from jarvis_core.db.models import Reminder diff --git a/jarvis/tasks/twitter.py b/jarvis/tasks/twitter.py index ff85def..20a8403 100644 --- a/jarvis/tasks/twitter.py +++ b/jarvis/tasks/twitter.py @@ -3,8 +3,8 @@ import logging from datetime import datetime, timedelta import tweepy -from dis_snek.ext.tasks.task import Task -from dis_snek.ext.tasks.triggers import IntervalTrigger +from dis_snek.models.snek.tasks.task import Task +from dis_snek.models.snek.tasks.triggers import IntervalTrigger from jarvis_core.db import q from jarvis_core.db.models import Twitter @@ -41,7 +41,7 @@ async def tweets() -> None: channel_id = twitter.channel tweets = sorted(tweets, key=lambda x: x.id) if guild_id not in guild_cache: - guild_cache[guild_id] = await jarvis.jarvis.get_guild(guild_id) + guild_cache[guild_id] = await jarvis.jarvis.fetch_guild(guild_id) guild = guild_cache[twitter.guild] if channel_id not in channel_cache: channel_cache[channel_id] = await guild.fetch_channel(channel_id) diff --git a/jarvis/tasks/unban.py b/jarvis/tasks/unban.py index 70c2b60..55a8409 100644 --- a/jarvis/tasks/unban.py +++ b/jarvis/tasks/unban.py @@ -1,8 +1,8 @@ """J.A.R.V.I.S. unban background task handler.""" from datetime import datetime, timedelta -from dis_snek.ext.tasks.task import Task -from dis_snek.ext.tasks.triggers import IntervalTrigger +from dis_snek.models.snek.tasks.task import Task +from dis_snek.models.snek.tasks.triggers import IntervalTrigger from jarvis_core.db import q from jarvis_core.db.models import Ban, Unban @@ -16,8 +16,8 @@ async def _unban() -> None: bans = Ban.find(q(type="temp", active=True)) async for ban in bans: if ban.created_at + timedelta(hours=ban.duration) < datetime.now() + timedelta(minutes=10): - guild = await jarvis.jarvis.get_guild(ban.guild) - user = await jarvis.jarvis.get_user(ban.user) + guild = await jarvis.jarvis.fetch_guild(ban.guild) + user = await jarvis.jarvis.fetch_user(ban.user) if user: await guild.unban(user=user, reason="Ban expired") ban.update(q(active=False)) diff --git a/jarvis/tasks/unlock.py b/jarvis/tasks/unlock.py index 2d8793f..22f6f15 100644 --- a/jarvis/tasks/unlock.py +++ b/jarvis/tasks/unlock.py @@ -2,8 +2,8 @@ from asyncio import to_thread from datetime import datetime, timedelta -from dis_snek.ext.tasks.task import Task -from dis_snek.ext.tasks.triggers import IntervalTrigger +from dis_snek.models.snek.tasks.task import Task +from dis_snek.models.snek.tasks.triggers import IntervalTrigger import jarvis from jarvis.db.models import Lock @@ -17,8 +17,8 @@ async def _unlock() -> None: if False: for lock in locks: if lock.created_at + timedelta(minutes=lock.duration) < datetime.utcnow(): - guild = await jarvis.jarvis.get_guild(lock.guild) - channel = await jarvis.jarvis.get_guild(lock.channel) + guild = await jarvis.jarvis.fetch_guild(lock.guild) + channel = await guild.fetch_channel(lock.channel) if channel: roles = await guild.fetch_roles() for role in roles: diff --git a/jarvis/tasks/unwarn.py b/jarvis/tasks/unwarn.py index c3afdac..84a07db 100644 --- a/jarvis/tasks/unwarn.py +++ b/jarvis/tasks/unwarn.py @@ -1,8 +1,8 @@ """J.A.R.V.I.S. unwarn background task handler.""" from datetime import datetime, timedelta -from dis_snek.ext.tasks.task import Task -from dis_snek.ext.tasks.triggers import IntervalTrigger +from dis_snek.models.snek.tasks.task import Task +from dis_snek.models.snek.tasks.triggers import IntervalTrigger from jarvis_core.db import q from jarvis_core.db.models import Warning