complete submission tracking in database, old submission auto deletion

This commit is contained in:
Xevion
2021-02-08 00:49:23 -06:00
parent b2dc0fdc91
commit 28c425ba9e
2 changed files with 55 additions and 4 deletions

View File

@@ -1,10 +1,17 @@
import logging
import discord import discord
from discord.ext import commands from discord.ext import commands
from discord.ext.commands import Context from discord.ext.commands import Context
from contest import checks from contest import checks, constants
from contest.bot import ContestBot from contest.bot import ContestBot
logger = logging.getLogger(__file__)
logger.setLevel(constants.LOGGING_LEVEL)
expected_deletions = []
class ContestCog(commands.Cog): class ContestCog(commands.Cog):
def __init__(self, bot: ContestBot): def __init__(self, bot: ContestBot):
@@ -43,18 +50,33 @@ class ContestCog(commands.Cog):
if message.author == self.bot.user or message.author.bot or not message.guild: return if message.author == self.bot.user or message.author.bot or not message.guild: return
cur_submission = await self.bot.db.get_submission_channel(message.guild.id) cur_submission = await self.bot.db.get_submission_channel(message.guild.id)
if message.channel.id == cur_submission: channel: discord.TextChannel = message.channel
if channel.id == cur_submission:
attachments = message.attachments attachments = message.attachments
if len(attachments) == 0: if len(attachments) == 0:
await message.delete(delay=1) await message.delete(delay=1)
warning = await message.channel.send( warning = await channel.send(
f':no_entry_sign: {message.author.mention} Each submission must contain exactly one image.') f':no_entry_sign: {message.author.mention} Each submission must contain exactly one image.')
await warning.delete(delay=5) await warning.delete(delay=5)
elif len(attachments) > 1: elif len(attachments) > 1:
await message.delete(delay=1) await message.delete(delay=1)
warning = await message.channel.send( warning = await channel.send(
f':no_entry_sign: {message.author.mention} Each submission must contain exactly one image.') f':no_entry_sign: {message.author.mention} Each submission must contain exactly one image.')
await warning.delete(delay=5) await warning.delete(delay=5)
else:
last_submission = await self.bot.db.get_submission(message.guild.id, message.author.id)
if last_submission is not None:
# delete last submission
submission_msg = await channel.fetch_message(last_submission)
if submission_msg is None:
logger.error(f'Unexpected: submission message {last_submission} could not be found.')
else:
await submission_msg.delete()
logger.info(f'Old submission deleted. {last_submission} (Old) -> {message.id} (New)')
await self.bot.db.add_submission(message.id, channel.guild.id, message.author.id, message.created_at)
logger.info(f'New submission created ({message.id}).')
@commands.Cog.listener() @commands.Cog.listener()
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent) -> None: async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent) -> None:

View File

@@ -1,6 +1,8 @@
import logging import logging
import os import os
import sqlite3 import sqlite3
from datetime import datetime
from typing import Optional
import aiosqlite import aiosqlite
@@ -43,6 +45,15 @@ class ContestDatabase(object):
prefix TEXT DEFAULT '$', prefix TEXT DEFAULT '$',
submission INTEGER NULLABLE)''') submission INTEGER NULLABLE)''')
logger.info(f"'guild' table created.") logger.info(f"'guild' table created.")
await cur.execute('''SELECT name FROM sqlite_master WHERE type='table' AND name = ?;''', ['submission'])
if await cur.fetchone() is None:
await self.conn.execute('''CREATE TABLE IF NOT EXISTS submission
(id INTEGER PRIMARY KEY,
user INTEGER,
guild INTEGER,
timestamp DATETIME)''')
logger.info(f"'submission' table created.")
finally: finally:
await cur.close() await cur.close()
@@ -93,3 +104,21 @@ class ContestDatabase(object):
"""Removes a guild from the database while completing appropriate teardown actions.""" """Removes a guild from the database while completing appropriate teardown actions."""
await self.conn.execute('''DELETE FROM guild WHERE id = ?''', [guild_id]) await self.conn.execute('''DELETE FROM guild WHERE id = ?''', [guild_id])
await self.conn.commit() await self.conn.commit()
async def get_submission(self, guild_id: int, user_id: int) -> Optional[int]:
cur = await self.conn.cursor()
try:
await cur.execute('''SELECT id FROM submission WHERE guild = ? AND user = ?''', [guild_id, user_id])
row = await cur.fetchone()
if row is None:
return None
return row[0]
finally:
await cur.close()
async def add_submission(self, submission_id: int, guild_id: int, user_id: int, timestamp: int = None) -> None:
await self.conn.execute(
'''INSERT INTO submission (id, user, guild, timestamp) VALUES (?, ?, ?, ?)''',
[submission_id, user_id, guild_id, timestamp or datetime.utcnow().timestamp()]
)
await self.conn.commit()