Add sync command, clean up db sessions

pull/115/head
stijndcl 2022-06-21 20:30:11 +02:00
parent 868cd392c3
commit 5c2c62c6c4
4 changed files with 34 additions and 5 deletions

View File

@ -0,0 +1,27 @@
from typing import Optional
import discord
from discord.ext import commands
from didier import Didier
class Owner(commands.Cog):
"""Cog for owner-only commands"""
client: Didier
def __init__(self, client: Didier):
self.client = client
@commands.command(name="Sync")
@commands.is_owner()
async def sync(self, ctx: commands.Context, guild: Optional[discord.Guild] = None):
"""Sync all application-commands in Discord"""
await self.client.tree.sync(guild=guild)
await ctx.message.add_reaction("🔄")
async def setup(client: Didier):
"""Load the cog"""
await client.add_cog(Owner(client))

View File

@ -29,8 +29,9 @@ class Tasks(commands.Cog):
if settings.UFORA_RSS_TOKEN is None or settings.UFORA_ANNOUNCEMENTS_CHANNEL is None: if settings.UFORA_RSS_TOKEN is None or settings.UFORA_ANNOUNCEMENTS_CHANNEL is None:
return return
async with self.client.db_session as session:
announcements_channel = self.client.get_channel(settings.UFORA_ANNOUNCEMENTS_CHANNEL) announcements_channel = self.client.get_channel(settings.UFORA_ANNOUNCEMENTS_CHANNEL)
announcements = await fetch_ufora_announcements(self.client.db_session) announcements = await fetch_ufora_announcements(session)
for announcement in announcements: for announcement in announcements:
await announcements_channel.send(embed=announcement.to_embed()) await announcements_channel.send(embed=announcement.to_embed())
@ -48,7 +49,8 @@ class Tasks(commands.Cog):
@tasks.loop(hours=24) @tasks.loop(hours=24)
async def remove_old_ufora_announcements(self): async def remove_old_ufora_announcements(self):
"""Remove all announcements that are over 1 week old, once per day""" """Remove all announcements that are over 1 week old, once per day"""
await remove_old_announcements(self.client.db_session) async with self.client.db_session as session:
await remove_old_announcements(session)
@remove_old_ufora_announcements.before_loop @remove_old_ufora_announcements.before_loop
async def _before_remove_old_ufora_announcements(self): async def _before_remove_old_ufora_announcements(self):