minor typehint fixes, make optional arg into optional kwarg, add additional TODOs, add basic attachment filtering

This commit is contained in:
Xevion
2021-02-15 05:22:22 -06:00
parent d99be7db9d
commit c234487302
2 changed files with 33 additions and 26 deletions

View File

@@ -5,6 +5,7 @@ from typing import ContextManager, List, Optional
import discord
from discord.ext import commands
# noinspection PyProtectedMember
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, sessionmaker
@@ -87,11 +88,11 @@ class ContestBot(commands.Bot):
if period is not None and period.active:
period.deactivate()
async def add_voting_reactions(self, channel: discord.TextChannel, submissions: Optional[List[Submission]]) -> None:
async def add_voting_reactions(self, channel: discord.TextChannel, submissions: Optional[List[Submission]] = None) -> None:
"""Adds reactions to all valid submissions in the given channel."""
if submissions is None:
with self.get_session() as session:
period: Guild = session.query(Guild).get(channel.guild.id).current_period
period: Period = session.query(Guild).get(channel.guild.id).current_period
if period is None:
logger.error('No valid submissions - current period is not set for the Guild this channel belongs to.')
return
@@ -99,7 +100,7 @@ class ContestBot(commands.Bot):
submissions = period.submissions
if len(submissions) == 0:
logger.warning('Attempted to add voting reactions to submissions, but none were given.')
logger.warning('Attempted to add voting reactions to submissions, but none were given or could be found.')
return
else:
for submission in submissions:

View File

@@ -19,6 +19,9 @@ expected_react_deletions: List[Tuple[int, int]] = []
# TODO: Add command error handling to all commands
# TODO: Use embeds in all bot responses
# TODO: Look into migrating from literals to i18n-ish representation of all messages & formatting
# TODO: Contest names
# TODO: Refactor Period into Contest (major)
class ContestCog(commands.Cog):
def __init__(self, bot: ContestBot):
@@ -125,8 +128,7 @@ class ContestCog(commands.Cog):
overwrite.send_messages = False
overwrite.add_reactions = False
response = 'Period stopped. Reactions and submissions disabled. Advance again to start a new period.'
# TODO: Fetch all submissions related to this period
# TODO: Create new period for Guild at
# TODO: Fetch all submissions related to this period and show a embed
period.advance_state()
@@ -184,8 +186,6 @@ class ContestCog(commands.Cog):
if channel.id == guild.submission_channel:
attachments = message.attachments
# TODO: Do attachment filtering between videos/files/audio etc.
# Ensure that the submission contains at least one attachment
if len(attachments) == 0:
await message.delete(delay=1)
@@ -198,6 +198,12 @@ class ContestCog(commands.Cog):
warning = await channel.send(
f':no_entry_sign: {message.author.mention} Each submission must contain exactly one image.')
await warning.delete(delay=5)
else:
attachment = attachments[0]
if attachment.is_spoiler():
await channel.send('Attachment must not make use of a spoiler.')
elif attachment.width is None:
await channel.send('Attachment must be a image or video.')
else:
last_submission: Submission = session.query(Submission).filter_by(period=guild.current_period,
user=message.author.id).first()
@@ -215,8 +221,8 @@ class ContestCog(commands.Cog):
session.delete(last_submission)
# Add the new submission row
session.add(Submission(id=message.id, user=message.author.id,
period=guild.current_period, timestamp=message.created_at))
session.add(Submission(id=message.id, user=message.author.id, period=guild.current_period,
timestamp=message.created_at))
logger.info(f'New submission created ({message.id}).')
@commands.Cog.listener()