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
+5 -4
View File
@@ -5,6 +5,7 @@ from typing import ContextManager, List, Optional
import discord import discord
from discord.ext import commands from discord.ext import commands
# noinspection PyProtectedMember
from sqlalchemy.engine import Engine from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, sessionmaker from sqlalchemy.orm import Session, sessionmaker
@@ -57,7 +58,7 @@ class ContestBot(commands.Bot):
_guild: Guild = session.query(Guild).get(guild.id) _guild: Guild = session.query(Guild).get(guild.id)
if _guild is None: if _guild is None:
logger.warning( 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)) session.add(Guild(id=guild.id))
async def on_guild_join(self, guild: discord.Guild) -> None: 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: if period is not None and period.active:
period.deactivate() 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.""" """Adds reactions to all valid submissions in the given channel."""
if submissions is None: if submissions is None:
with self.get_session() as session: 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: if period is None:
logger.error('No valid submissions - current period is not set for the Guild this channel belongs to.') logger.error('No valid submissions - current period is not set for the Guild this channel belongs to.')
return return
@@ -99,7 +100,7 @@ class ContestBot(commands.Bot):
submissions = period.submissions submissions = period.submissions
if len(submissions) == 0: 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 return
else: else:
for submission in submissions: for submission in submissions:
+28 -22
View File
@@ -19,6 +19,9 @@ expected_react_deletions: List[Tuple[int, int]] = []
# TODO: Add command error handling to all commands # TODO: Add command error handling to all commands
# TODO: Use embeds in all bot responses # TODO: Use embeds in all bot responses
# TODO: Look into migrating from literals to i18n-ish representation of all messages & formatting # 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): class ContestCog(commands.Cog):
def __init__(self, bot: ContestBot): def __init__(self, bot: ContestBot):
@@ -125,8 +128,7 @@ class ContestCog(commands.Cog):
overwrite.send_messages = False overwrite.send_messages = False
overwrite.add_reactions = False overwrite.add_reactions = False
response = 'Period stopped. Reactions and submissions disabled. Advance again to start a new period.' response = 'Period stopped. Reactions and submissions disabled. Advance again to start a new period.'
# TODO: Fetch all submissions related to this period # TODO: Fetch all submissions related to this period and show a embed
# TODO: Create new period for Guild at
period.advance_state() period.advance_state()
@@ -184,8 +186,6 @@ class ContestCog(commands.Cog):
if channel.id == guild.submission_channel: if channel.id == guild.submission_channel:
attachments = message.attachments attachments = message.attachments
# TODO: Do attachment filtering between videos/files/audio etc.
# Ensure that the submission contains at least one attachment # Ensure that the submission contains at least one attachment
if len(attachments) == 0: if len(attachments) == 0:
await message.delete(delay=1) 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.') 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: else:
last_submission: Submission = session.query(Submission).filter_by(period=guild.current_period, attachment = attachments[0]
user=message.author.id).first() if attachment.is_spoiler():
if last_submission is not None: await channel.send('Attachment must not make use of a spoiler.')
# delete last submission elif attachment.width is None:
submission_msg = await channel.fetch_message(last_submission.id) await channel.send('Attachment must be a image or video.')
if submission_msg is None: else:
logger.error(f'Unexpected: submission message {last_submission.id} could not be found.') last_submission: Submission = session.query(Submission).filter_by(period=guild.current_period,
else: user=message.author.id).first()
expected_msg_deletions.append(submission_msg.id) if last_submission is not None:
await submission_msg.delete() # delete last submission
logger.info(f'Old submission deleted. {last_submission.id} (Old) -> {message.id} (New)') 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 # Delete the old submission row
session.delete(last_submission) session.delete(last_submission)
# Add the new submission row # Add the new submission row
session.add(Submission(id=message.id, user=message.author.id, session.add(Submission(id=message.id, user=message.author.id, period=guild.current_period,
period=guild.current_period, timestamp=message.created_at)) timestamp=message.created_at))
logger.info(f'New submission created ({message.id}).') logger.info(f'New submission created ({message.id}).')
@commands.Cog.listener() @commands.Cog.listener()
async def on_raw_message_delete(self, payload: discord.RawMessageDeleteEvent) -> None: 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) submission: Submission = session.query(Submission).get(payload.message_id)
if submission is None: if submission is None:
logger.warning( 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: else:
submission.votes = 0 submission.votes = 0