mirror of
https://github.com/Xevion/contest-assistant.git
synced 2025-12-07 20:06:41 -06:00
complete submission tracking in database, old submission auto deletion
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
import logging
|
||||
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from discord.ext.commands import Context
|
||||
|
||||
from contest import checks
|
||||
from contest import checks, constants
|
||||
from contest.bot import ContestBot
|
||||
|
||||
logger = logging.getLogger(__file__)
|
||||
logger.setLevel(constants.LOGGING_LEVEL)
|
||||
|
||||
expected_deletions = []
|
||||
|
||||
|
||||
class ContestCog(commands.Cog):
|
||||
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
|
||||
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
|
||||
if len(attachments) == 0:
|
||||
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.')
|
||||
await warning.delete(delay=5)
|
||||
elif len(attachments) > 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.')
|
||||
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()
|
||||
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent) -> None:
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import logging
|
||||
import os
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
import aiosqlite
|
||||
|
||||
@@ -43,6 +45,15 @@ class ContestDatabase(object):
|
||||
prefix TEXT DEFAULT '$',
|
||||
submission INTEGER NULLABLE)''')
|
||||
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:
|
||||
await cur.close()
|
||||
|
||||
@@ -93,3 +104,21 @@ class ContestDatabase(object):
|
||||
"""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.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()
|
||||
|
||||
Reference in New Issue
Block a user