mirror of
https://github.com/Xevion/contest-assistant.git
synced 2025-12-07 05:14:42 -06:00
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:
@@ -106,3 +106,11 @@ class ContestBot(commands.Bot):
|
|||||||
for submission in submissions:
|
for submission in submissions:
|
||||||
message: discord.PartialMessage = channel.get_partial_message(submission.id)
|
message: discord.PartialMessage = channel.get_partial_message(submission.id)
|
||||||
await message.add_reaction(self.get_emoji(constants.Emoji.UPVOTE))
|
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)
|
||||||
|
|||||||
@@ -279,11 +279,7 @@ class ContestCog(commands.Cog):
|
|||||||
if submission is None:
|
if submission is None:
|
||||||
logger.warning(f'Upvote reaction added to message {payload.message_id}, but no Submission found in database.')
|
logger.warning(f'Upvote reaction added to message {payload.message_id}, but no Submission found in database.')
|
||||||
else:
|
else:
|
||||||
submission.increment()
|
await submission.update(self.bot, message=await message.fetch())
|
||||||
# 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))
|
|
||||||
else:
|
else:
|
||||||
# Remove the emoji since it's not supposed to be there anyways.
|
# 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.
|
# 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:
|
if submission is None:
|
||||||
logger.warning(f'Upvote reaction removed from message {payload.message_id}, but no Submission found in database.')
|
logger.warning(f'Upvote reaction removed from message {payload.message_id}, but no Submission found in database.')
|
||||||
else:
|
else:
|
||||||
submission.decrement()
|
message = await self.bot.fetch_message(payload.channel_id, payload.message_id)
|
||||||
|
await submission.update(self.bot, message=message)
|
||||||
# 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))
|
|
||||||
|
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
async def on_raw_reaction_clear(self, payload: discord.RawReactionActionEvent) -> None:
|
async def on_raw_reaction_clear(self, payload: discord.RawReactionActionEvent) -> None:
|
||||||
@@ -330,10 +318,8 @@ class ContestCog(commands.Cog):
|
|||||||
if submission is None:
|
if submission is None:
|
||||||
logger.warning(f'Witnessed reactions removed from message {payload.message_id}, but no Submission found in database.')
|
logger.warning(f'Witnessed reactions removed from message {payload.message_id}, but no Submission found in database.')
|
||||||
else:
|
else:
|
||||||
submission.votes = 0
|
submission.votes = []
|
||||||
# TODO: Add helper function for getting partial/full messages easily given a payload or channel id and message id
|
message = self.bot.get_message(payload.channel_id, payload.message_id)
|
||||||
channel: discord.TextChannel = self.bot.get_channel(payload.channel_id)
|
|
||||||
message: discord.PartialMessage = channel.get_partial_message(payload.message_id)
|
|
||||||
await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE))
|
await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE))
|
||||||
|
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
@@ -345,13 +331,11 @@ class ContestCog(commands.Cog):
|
|||||||
if helpers.is_upvote(payload.emoji):
|
if helpers.is_upvote(payload.emoji):
|
||||||
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},'
|
||||||
f'Witnessed all upvote reactions removed from message {payload.message_id}, but no Submission found in database.')
|
f' but no Submission found in database.')
|
||||||
else:
|
else:
|
||||||
submission.votes = 0
|
submission.votes = []
|
||||||
|
message = self.bot.get_message(payload.channel_id, payload.message_id)
|
||||||
channel: discord.TextChannel = self.bot.get_channel(payload.channel_id)
|
|
||||||
message: discord.PartialMessage = channel.get_partial_message(payload.message_id)
|
|
||||||
await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE))
|
await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import logging
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
# Path Constants
|
# Path Constants
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
BASE_DIR = os.path.dirname(os.path.abspath(os.path.join(__file__, '..')))
|
BASE_DIR = os.path.dirname(os.path.abspath(os.path.join(__file__, '..')))
|
||||||
TOKEN = os.path.join(BASE_DIR, 'token.dat')
|
TOKEN = os.path.join(BASE_DIR, 'token.dat')
|
||||||
DATABASE = os.path.join(BASE_DIR, 'database.db')
|
DATABASE = os.path.join(BASE_DIR, 'database.db')
|
||||||
@@ -10,7 +12,13 @@ DATABASE_URI = f'sqlite:///{DATABASE}'
|
|||||||
# Other constants
|
# Other constants
|
||||||
LOGGING_LEVEL = logging.DEBUG
|
LOGGING_LEVEL = logging.DEBUG
|
||||||
|
|
||||||
|
|
||||||
# Emote references
|
# Emote references
|
||||||
class Emoji(object):
|
class Emoji(object):
|
||||||
|
"""A constants class storing the IDs of various Emojis used by the bot."""
|
||||||
UPVOTE = 810310002220859393
|
UPVOTE = 810310002220859393
|
||||||
DOWNVOTE = 810310019840213002
|
DOWNVOTE = 810310019840213002
|
||||||
|
|
||||||
|
|
||||||
|
# Named Tuples
|
||||||
|
ReactionMarker = namedtuple("ReactionMarker", ["message", "user", "emoji"], defaults=[Emoji.UPVOTE])
|
||||||
|
|||||||
Reference in New Issue
Block a user