From 80542e1794b4a6223aa4e5cfc7c809a492c1c6b1 Mon Sep 17 00:00:00 2001 From: Infernum11 <81740112+Infernum1@users.noreply.github.com> Date: Sun, 14 Jul 2024 00:14:44 +0530 Subject: [PATCH 1/6] add voting reminders --- bot/cogs/core/voting.py | 95 +++++++++++++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 17 deletions(-) diff --git a/bot/cogs/core/voting.py b/bot/cogs/core/voting.py index c9284639..4208072a 100644 --- a/bot/cogs/core/voting.py +++ b/bot/cogs/core/voting.py @@ -4,13 +4,76 @@ import arrow import discord from discord.ext import commands +from discord.utils import format_dt + +from bot.utils.ctx import CustomContext from bot.cogs.core.database import Database from bot.cogs.core.quests import Quests +from bot.utils.misc import get_user_and_lang_from_loc from bot.villager_bot import VillagerBotCluster from common.models.topgg_vote import TopggVote +class VoteReminderView(discord.ui.View): + def __init__( + self, + *, + bot: VillagerBotCluster, + loc: CustomContext | commands.Context | discord.User, + user_id: int, + timeout: float = 300.0, + ): + super().__init__(timeout=timeout) + + self._bot = bot + self._loc = loc + self._user_id = user_id + + self.message: discord.Message | None = None + + @property + def _db(self) -> Database: + return typing.cast(Database, self._bot.get_cog("Database")) + + async def interaction_check(self, interaction: discord.Interaction) -> bool: + return interaction.user.id == self._user_id + + @discord.ui.button(label="Add Vote Reminder", style=discord.ButtonStyle.gray) + async def btn_add_vote_reminder( + self, interaction: discord.Interaction, button: discord.ui.Button + ): + await interaction.response.defer(thinking=False) + _, lang = get_user_and_lang_from_loc(self._bot.l, self._loc) + + at = arrow.utcnow().shift(hours=12) + await self._db.add_reminder( + interaction.user.id, + interaction.channel.id, + interaction.message.id, + f"[Vote]({self._bot.d.topgg + '/vote'}) for Villager Bot", + at.datetime, + ) + + await self._bot.send_embed( + interaction.user, + lang.useful.remind.remind.format( + self._bot.d.emojis.yes, + format_dt(at.datetime, style="R"), + ), + ) + for child in self.children: + child.disabled = True + + await self.message.edit(view=self) + + async def on_timeout(self) -> None: + for child in self.children: + child.disabled = True + + await self.message.edit(view=self) + + class Voting(commands.Cog): def __init__(self, bot: VillagerBotCluster): self.bot = bot @@ -37,7 +100,8 @@ async def vote(self, *, user_id: int, site: str, is_weekend: bool, is_test: bool self.logger.info(f"{user_id} voted on {site}") self.bot.session_votes += 1 - user = self.bot.get_user(user_id) + user = await self.bot.fetch_user(user_id) + embed = discord.Embed(color=self.bot.embed_color) if user is None: with suppress(discord.HTTPException): @@ -57,7 +121,7 @@ async def vote(self, *, user_id: int, site: str, is_weekend: bool, is_test: bool last_vote = db_user.last_vote or 0 vote_streak = (db_user.vote_streak or 0) + 1 - # make sure last vote wasn't within 12 hours + # # make sure last vote wasn't within 12 hours if arrow.get(last_vote) > arrow.utcnow().shift(hours=-12): return @@ -90,31 +154,28 @@ async def vote(self, *, user_id: int, site: str, is_weekend: bool, is_test: bool else: raise NotImplementedError(f"No case for site {site}") + view = VoteReminderView(bot=self.bot, loc=user, user_id=user.id) if vote_streak is None: await self.db.balance_add(user_id, emeralds) - await self.bot.send_embed( - user, + embed.description = ( f"Thanks for voting! You've received **{emeralds}**{self.d.emojis.emerald}!", - ignore_exceptions=True, ) + message = await user.send(embed=embed, view=view) + elif vote_streak % 16 == 0: barrels = min(int(vote_streak // 32 + 1), 8) await self.db.add_item(user.id, "Barrel", 1024, barrels) - await self.bot.send_embed( - user, - f"Thanks for voting! You've received {barrels}x **Barrel**!", - ignore_exceptions=True, - ) + embed.description = f"Thanks for voting! You've received {barrels}x **Barrel**!" + message = await user.send(embed=embed, view=view) + else: await self.db.balance_add(user_id, emeralds) - await self.bot.send_embed( - user, - ( - f"Thanks for voting! You've received **{emeralds}**{self.d.emojis.emerald}! " - f"(Vote streak is now {vote_streak})" - ), - ignore_exceptions=True, + embed.description = ( + f"Thanks for voting! You've received **{emeralds}**{self.d.emojis.emerald}! " + f"(Vote streak is now {vote_streak})" ) + message = await user.send(embed=embed, view=view) + view.message = message @commands.Cog.listener() async def on_topgg_vote(self, vote: TopggVote): From 5ec4d4d88e3d58fca4705a51a5cc756e886661c9 Mon Sep 17 00:00:00 2001 From: Infernum11 <81740112+Infernum1@users.noreply.github.com> Date: Sun, 14 Jul 2024 00:24:36 +0530 Subject: [PATCH 2/6] misc improvements --- bot/cogs/core/voting.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/bot/cogs/core/voting.py b/bot/cogs/core/voting.py index 4208072a..90933e9b 100644 --- a/bot/cogs/core/voting.py +++ b/bot/cogs/core/voting.py @@ -20,14 +20,14 @@ def __init__( self, *, bot: VillagerBotCluster, - loc: CustomContext | commands.Context | discord.User, + user: discord.User, user_id: int, timeout: float = 300.0, ): super().__init__(timeout=timeout) self._bot = bot - self._loc = loc + self._user = user self._user_id = user_id self.message: discord.Message | None = None @@ -44,14 +44,14 @@ async def btn_add_vote_reminder( self, interaction: discord.Interaction, button: discord.ui.Button ): await interaction.response.defer(thinking=False) - _, lang = get_user_and_lang_from_loc(self._bot.l, self._loc) + _, lang = get_user_and_lang_from_loc(self._bot.l, self._user) at = arrow.utcnow().shift(hours=12) await self._db.add_reminder( interaction.user.id, interaction.channel.id, interaction.message.id, - f"[Vote]({self._bot.d.topgg + '/vote'}) for Villager Bot", + f"[Vote]({self._bot.d.topgg}/vote) for Villager Bot", at.datetime, ) @@ -62,10 +62,8 @@ async def btn_add_vote_reminder( format_dt(at.datetime, style="R"), ), ) - for child in self.children: - child.disabled = True - - await self.message.edit(view=self) + + await self.on_timeout() async def on_timeout(self) -> None: for child in self.children: @@ -121,7 +119,7 @@ async def vote(self, *, user_id: int, site: str, is_weekend: bool, is_test: bool last_vote = db_user.last_vote or 0 vote_streak = (db_user.vote_streak or 0) + 1 - # # make sure last vote wasn't within 12 hours + # make sure last vote wasn't within 12 hours if arrow.get(last_vote) > arrow.utcnow().shift(hours=-12): return From a5fdee95bbb6fb9d439d88c8493315be2ba28d52 Mon Sep 17 00:00:00 2001 From: Infernum11 <81740112+Infernum1@users.noreply.github.com> Date: Sun, 14 Jul 2024 00:25:02 +0530 Subject: [PATCH 3/6] format and lint --- bot/cogs/core/voting.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bot/cogs/core/voting.py b/bot/cogs/core/voting.py index 90933e9b..abe89e01 100644 --- a/bot/cogs/core/voting.py +++ b/bot/cogs/core/voting.py @@ -7,7 +7,6 @@ from discord.utils import format_dt -from bot.utils.ctx import CustomContext from bot.cogs.core.database import Database from bot.cogs.core.quests import Quests from bot.utils.misc import get_user_and_lang_from_loc @@ -62,7 +61,7 @@ async def btn_add_vote_reminder( format_dt(at.datetime, style="R"), ), ) - + await self.on_timeout() async def on_timeout(self) -> None: From 693517e6ea2961dbc4bfaa286308b3a2d6ddf63e Mon Sep 17 00:00:00 2001 From: Infernum11 <81740112+Infernum1@users.noreply.github.com> Date: Sun, 14 Jul 2024 00:30:44 +0530 Subject: [PATCH 4/6] fixes --- bot/cogs/core/voting.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/bot/cogs/core/voting.py b/bot/cogs/core/voting.py index abe89e01..6f2bf1c5 100644 --- a/bot/cogs/core/voting.py +++ b/bot/cogs/core/voting.py @@ -20,14 +20,12 @@ def __init__( *, bot: VillagerBotCluster, user: discord.User, - user_id: int, timeout: float = 300.0, ): super().__init__(timeout=timeout) self._bot = bot self._user = user - self._user_id = user_id self.message: discord.Message | None = None @@ -36,7 +34,7 @@ def _db(self) -> Database: return typing.cast(Database, self._bot.get_cog("Database")) async def interaction_check(self, interaction: discord.Interaction) -> bool: - return interaction.user.id == self._user_id + return interaction.user.id == self._user.id @discord.ui.button(label="Add Vote Reminder", style=discord.ButtonStyle.gray) async def btn_add_vote_reminder( @@ -151,7 +149,7 @@ async def vote(self, *, user_id: int, site: str, is_weekend: bool, is_test: bool else: raise NotImplementedError(f"No case for site {site}") - view = VoteReminderView(bot=self.bot, loc=user, user_id=user.id) + view = VoteReminderView(bot=self.bot, user=user) if vote_streak is None: await self.db.balance_add(user_id, emeralds) embed.description = ( From 32fa3909cdc2b1146c6e54433391ccd946055daa Mon Sep 17 00:00:00 2001 From: Infernum11 <81740112+Infernum1@users.noreply.github.com> Date: Sun, 14 Jul 2024 00:36:54 +0530 Subject: [PATCH 5/6] explicitly set language to 'en' --- bot/cogs/core/voting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot/cogs/core/voting.py b/bot/cogs/core/voting.py index 6f2bf1c5..c0653447 100644 --- a/bot/cogs/core/voting.py +++ b/bot/cogs/core/voting.py @@ -41,7 +41,7 @@ async def btn_add_vote_reminder( self, interaction: discord.Interaction, button: discord.ui.Button ): await interaction.response.defer(thinking=False) - _, lang = get_user_and_lang_from_loc(self._bot.l, self._user) + lang = self._bot.l['en'] at = arrow.utcnow().shift(hours=12) await self._db.add_reminder( From 7796625f42c241ea5c9493b51370ec8a0d61f000 Mon Sep 17 00:00:00 2001 From: Infernum11 <81740112+Infernum1@users.noreply.github.com> Date: Sun, 14 Jul 2024 00:38:18 +0530 Subject: [PATCH 6/6] format and lint --- bot/cogs/core/voting.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bot/cogs/core/voting.py b/bot/cogs/core/voting.py index c0653447..b0a6faad 100644 --- a/bot/cogs/core/voting.py +++ b/bot/cogs/core/voting.py @@ -9,7 +9,6 @@ from bot.cogs.core.database import Database from bot.cogs.core.quests import Quests -from bot.utils.misc import get_user_and_lang_from_loc from bot.villager_bot import VillagerBotCluster from common.models.topgg_vote import TopggVote @@ -41,7 +40,7 @@ async def btn_add_vote_reminder( self, interaction: discord.Interaction, button: discord.ui.Button ): await interaction.response.defer(thinking=False) - lang = self._bot.l['en'] + lang = self._bot.l["en"] at = arrow.utcnow().shift(hours=12) await self._db.add_reminder(