Implement Submission.update and get/fetch message helper functions

Added and implemented helper functions for getting and fetching messages
given a payload's integer channel and message IDs.
Implemented Submission.votes list modifications and Submission.update in
 code (tested with real bot)
 Committed constants.py containing ReactionMarker used in models.py
This commit is contained in:
Xevion
2021-02-17 05:18:25 -06:00
parent 5c70a78798
commit 7427763b57
3 changed files with 25 additions and 25 deletions

View File

@@ -106,3 +106,11 @@ class ContestBot(commands.Bot):
for submission in submissions:
message: discord.PartialMessage = channel.get_partial_message(submission.id)
await message.add_reaction(self.get_emoji(constants.Emoji.UPVOTE))
def get_message(self, channel_id: int, message_id: int) -> discord.PartialMessage:
channel: discord.TextChannel = self.get_channel(channel_id)
return channel.get_partial_message(message_id)
async def fetch_message(self, channel_id: int, message_id: int) -> discord.Message:
channel: discord.TextChannel = self.get_channel(channel_id)
return await channel.fetch_message(message_id)

View File

@@ -279,11 +279,7 @@ class ContestCog(commands.Cog):
if submission is None:
logger.warning(f'Upvote reaction added to message {payload.message_id}, but no Submission found in database.')
else:
submission.increment()
# Make sure our reaction exists, verify vote count
self_reacted = await submission.verify(await message.fetch(), self.bot.user)
if not self_reacted:
await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE))
await submission.update(self.bot, message=await message.fetch())
else:
# Remove the emoji since it's not supposed to be there anyways.
# If permissions were setup correctly, only moderators or admins should be able to trigger this.
@@ -309,16 +305,8 @@ class ContestCog(commands.Cog):
if submission is None:
logger.warning(f'Upvote reaction removed from message {payload.message_id}, but no Submission found in database.')
else:
submission.decrement()
# Get the actual number of votes from the message
channel: discord.TextChannel = self.bot.get_channel(payload.channel_id)
message: discord.Message = await channel.fetch_message(payload.message_id)
self_reacted = await submission.verify(await message.fetch(), self.bot.user)
# Make sure our reaction exists
if not self_reacted:
await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE))
message = await self.bot.fetch_message(payload.channel_id, payload.message_id)
await submission.update(self.bot, message=message)
@commands.Cog.listener()
async def on_raw_reaction_clear(self, payload: discord.RawReactionActionEvent) -> None:
@@ -330,10 +318,8 @@ class ContestCog(commands.Cog):
if submission is None:
logger.warning(f'Witnessed reactions removed from message {payload.message_id}, but no Submission found in database.')
else:
submission.votes = 0
# TODO: Add helper function for getting partial/full messages easily given a payload or channel id and message id
channel: discord.TextChannel = self.bot.get_channel(payload.channel_id)
message: discord.PartialMessage = channel.get_partial_message(payload.message_id)
submission.votes = []
message = self.bot.get_message(payload.channel_id, payload.message_id)
await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE))
@commands.Cog.listener()
@@ -345,13 +331,11 @@ class ContestCog(commands.Cog):
if helpers.is_upvote(payload.emoji):
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.')
logger.warning(f'Witnessed all upvote reactions removed from message {payload.message_id},'
f' but no Submission found in database.')
else:
submission.votes = 0
channel: discord.TextChannel = self.bot.get_channel(payload.channel_id)
message: discord.PartialMessage = channel.get_partial_message(payload.message_id)
submission.votes = []
message = self.bot.get_message(payload.channel_id, payload.message_id)
await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE))

View File

@@ -2,6 +2,8 @@ import logging
import os
# Path Constants
from collections import namedtuple
BASE_DIR = os.path.dirname(os.path.abspath(os.path.join(__file__, '..')))
TOKEN = os.path.join(BASE_DIR, 'token.dat')
DATABASE = os.path.join(BASE_DIR, 'database.db')
@@ -10,7 +12,13 @@ DATABASE_URI = f'sqlite:///{DATABASE}'
# Other constants
LOGGING_LEVEL = logging.DEBUG
# Emote references
class Emoji(object):
"""A constants class storing the IDs of various Emojis used by the bot."""
UPVOTE = 810310002220859393
DOWNVOTE = 810310019840213002
# Named Tuples
ReactionMarker = namedtuple("ReactionMarker", ["message", "user", "emoji"], defaults=[Emoji.UPVOTE])