Use CacheCog where applicable

This commit is contained in:
Zeva Rose 2021-07-25 22:32:56 -06:00
parent 408ef87f43
commit cfa9642fb3
4 changed files with 9 additions and 73 deletions

View file

@ -14,6 +14,7 @@ from discord_slash.model import ButtonStyle
from jarvis.config import get_config
from jarvis.db import DBManager
from jarvis.utils import build_embed
from jarvis.utils.cachecog import CacheCog
from jarvis.utils.field import Field
guild_ids = [578757004059738142, 520021794380447745, 862402786116763668]
@ -25,26 +26,13 @@ invites = re.compile(
)
class CTCCog(commands.Cog):
class CTCCog(CacheCog):
def __init__(self, bot):
self.bot = bot
super().__init__(bot)
mconf = get_config().mongo
self.db = DBManager(mconf).mongo
self._session = aiohttp.ClientSession()
self.url = "https://completethecodetwo.cards/pw"
self.cache = {}
self._expire_interaction.start()
def check_cache(self, ctx: SlashContext, **kwargs):
if not kwargs:
kwargs = {}
return find(
lambda x: x["command"] == ctx.subcommand_name
and x["user"] == ctx.author.id
and x["guild"] == ctx.guild.id
and all(x[k] == v for k, v in kwargs.items()),
self.cache.values(),
)
@cog_ext.cog_subcommand(
base="ctc2",
@ -178,15 +166,6 @@ class CTCCog(commands.Cog):
await paginator.start()
@loop(minutes=1)
async def _expire_interaction(self):
keys = list(self.cache.keys())
for key in keys:
if self.cache[key]["timeout"] <= datetime.utcnow() + timedelta(
minutes=1
):
del self.cache[key]
def setup(bot):
bot.add_cog(CTCCog(bot))

View file

@ -11,32 +11,21 @@ from discord_slash.utils.manage_commands import create_choice, create_option
from jarvis.config import get_config
from jarvis.utils import build_embed
from jarvis.utils.cachecog import CacheCog
from jarvis.utils.field import Field
guild_ids = [862402786116763668]
class GitlabCog(commands.Cog):
class GitlabCog(CacheCog):
def __init__(self, bot):
self.bot = bot
super().__init__(bot)
config = get_config()
self._gitlab = gitlab.Gitlab(
"https://git.zevaryx.com", private_token=config.gitlab_token
)
# J.A.R.V.I.S. GitLab ID is 29
self.project = self._gitlab.projects.get(29)
self.cache = {}
self._expire_interaction.start()
def check_cache(self, ctx: SlashContext, **kwargs):
if not kwargs:
kwargs = {}
return find(
lambda x: x["command"] == ctx.subcommand_name
and x["user"] == ctx.author.id
and all(x[k] == v for k, v in kwargs.items()),
self.cache.values(),
)
@cog_ext.cog_subcommand(
base="gl",
@ -546,15 +535,6 @@ class GitlabCog(commands.Cog):
await paginator.start()
@loop(minutes=1)
async def _expire_interaction(self):
keys = list(self.cache.keys())
for key in keys:
if self.cache[key]["timeout"] <= datetime.utcnow() + timedelta(
minutes=1
):
del self.cache[key]
def setup(bot):
if get_config().gitlab_token:

View file

@ -4,12 +4,10 @@ from datetime import datetime, timedelta
from typing import Optional
from bson import ObjectId
from ButtonPaginator import Paginator
from discord.ext import commands
from discord.ext.tasks import loop
from discord.utils import find
from discord_slash import SlashContext, cog_ext
from discord_slash.model import ButtonStyle
from discord_slash.utils.manage_commands import create_option
from discord_slash.utils.manage_components import (
create_actionrow,
@ -20,6 +18,7 @@ from discord_slash.utils.manage_components import (
from jarvis.db.types import Reminder
from jarvis.utils import build_embed
from jarvis.utils.cachecog import CacheCog
from jarvis.utils.field import Field
valid = re.compile(r"[\w\s\-\\/.!@#$%^*()+=<>,\u0080-\U000E0FFF]*")
@ -29,23 +28,11 @@ invites = re.compile(
)
class RemindmeCog(commands.Cog):
class RemindmeCog(CacheCog):
def __init__(self, bot):
self.bot = bot
self.cache = {}
super().__init__(bot)
self._remind.start()
def check_cache(self, ctx: SlashContext, **kwargs):
if not kwargs:
kwargs = {}
return find(
lambda x: x["command"] == ctx.subcommand_name
and x["user"] == ctx.author.id
and x["guild"] == ctx.guild.id
and all(x[k] == v for k, v in kwargs.items()),
self.cache.values(),
)
@cog_ext.cog_slash(
name="remindme",
description="Set a reminder",
@ -318,15 +305,6 @@ class RemindmeCog(commands.Cog):
component["disabled"] = True
await message.edit(components=components)
@loop(minutes=1)
async def _expire_interaction(self):
keys = list(self.cache.keys())
for key in keys:
if self.cache[key]["timeout"] <= datetime.utcnow() + timedelta(
minutes=1
):
del self.cache[key]
@loop(seconds=15)
async def _remind(self):
reminders = Reminder.get_active(

View file

@ -10,7 +10,6 @@ from jarvis.utils.permissions import admin_or_permissions
class SettingsCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.cache = {}
def update_settings(self, setting, value, guild):
setting = Setting(setting=setting, value=value, guild=guild)