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
@@ -57,7 +58,7 @@ class ContestBot(commands.Bot):
_guild: Guild = session.query(Guild).get(guild.id)
if _guild is None:
logger.warning(
f'Guild {guild.name} ({guild.id}) was not inside database on ready. Bot was disconnected or did not add it properly...')
f'Guild {guild.name} ({guild.id}) was not inside database on ready. Bot was disconnected or did not add it properly...')
session.add(Guild(id=guild.id))
async def on_guild_join(self, guild: discord.Guild) -> None:
@@ -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)
@@ -199,25 +199,31 @@ class ContestCog(commands.Cog):
f':no_entry_sign: {message.author.mention} Each submission must contain exactly one image.')
await warning.delete(delay=5)
else:
last_submission: Submission = session.query(Submission).filter_by(period=guild.current_period,
user=message.author.id).first()
if last_submission is not None:
# delete last submission
submission_msg = await channel.fetch_message(last_submission.id)
if submission_msg is None:
logger.error(f'Unexpected: submission message {last_submission.id} could not be found.')
else:
expected_msg_deletions.append(submission_msg.id)
await submission_msg.delete()
logger.info(f'Old submission deleted. {last_submission.id} (Old) -> {message.id} (New)')
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()
if last_submission is not None:
# delete last submission
submission_msg = await channel.fetch_message(last_submission.id)
if submission_msg is None:
logger.error(f'Unexpected: submission message {last_submission.id} could not be found.')
else:
expected_msg_deletions.append(submission_msg.id)
await submission_msg.delete()
logger.info(f'Old submission deleted. {last_submission.id} (Old) -> {message.id} (New)')
# Delete the old submission row
session.delete(last_submission)
# Delete the old submission row
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))
logger.info(f'New submission created ({message.id}).')
# Add the new submission row
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()
async def on_raw_message_delete(self, payload: discord.RawMessageDeleteEvent) -> None:
@@ -336,7 +342,7 @@ class ContestCog(commands.Cog):
submission: Submission = session.query(Submission).get(payload.message_id)
if submission is None:
logger.warning(
f'Witnessed all upvote reactions removed from message {payload.message_id}, but no Submission found in database.')
f'Witnessed all upvote reactions removed from message {payload.message_id}, but no Submission found in database.')
else:
submission.votes = 0