From 48d3ac0be2ac2c338bc1a71055fe31fe7bb471f1 Mon Sep 17 00:00:00 2001 From: kita Date: Fri, 1 Mar 2024 16:59:29 +0500 Subject: [PATCH] actually add pokemap and interaction failed fix for guessaddon --- cogs/games.py | 78 +++++++++++++++++++++++++-------------- cogs/pokemap.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+), 28 deletions(-) create mode 100644 cogs/pokemap.py diff --git a/cogs/games.py b/cogs/games.py index 7f73674..a40cd6b 100644 --- a/cogs/games.py +++ b/cogs/games.py @@ -12,12 +12,31 @@ if TYPE_CHECKING: class GuessAddonGame(ui.View): - def __init__(self, bot: Lina): - super().__init__() + def __init__(self, bot: Lina, author_id: int): + super().__init__(timeout=15) self.addonChoices = [] self.bot: Lina = bot + self.author_id = author_id self.gotCorrectAnswer = False + self.interaction = None + + async def interaction_check(self, interaction: discord.Interaction): + if interaction.user.id != self.author_id: + await interaction.response.send_message("no u", ephemeral=True) + return False + return True + + async def on_timeout(self): + for child in self.children: + child.disabled = True + + return await self.interaction.edit_original_response(embed=discord.Embed( + title="Times Up!", + description="You didn't answer on time!", + color=self.bot.accent_color + ), view=self) + async def initGame(self): self.correctAddon = random.choice( @@ -61,39 +80,53 @@ class GuessAddonGame(ui.View): self.children[c].label = self.addonChoices[c][1] - def endGame(self): + async def endGame(self, interaction: discord.Interaction): for i in range(len(self.children)): if self.addonChoices[i][2] is True: self.children[i].style = discord.ButtonStyle.green self.children[i].disabled = True + + if self.gotCorrectAnswer: + await interaction.response.edit_message(embed=discord.Embed( + title="Great Job!", + description="You got the correct answer!", + color=self.bot.accent_color + ), view=self) + else: + await interaction.response.edit_message(embed=discord.Embed( + title="Wrong answer!", + description="Better luck next time!", + color=self.bot.accent_color + ), view=self) + self.stop() @ui.button(style=discord.ButtonStyle.gray) async def choice1(self, interaction: discord.Interaction, button: ui.Button): if self.addonChoices[0][2] is True: self.gotCorrectAnswer = True - self.endGame() + await self.endGame(interaction) else: self.choice1.style = discord.ButtonStyle.red - self.endGame() + await self.endGame(interaction) @ui.button(style=discord.ButtonStyle.gray) async def choice2(self, interaction: discord.Interaction, button: ui.Button): if self.addonChoices[1][2] is True: self.gotCorrectAnswer = True - self.endGame() + await self.endGame(interaction) else: self.choice2.style = discord.ButtonStyle.red - self.endGame() + await self.endGame(interaction) @ui.button(style=discord.ButtonStyle.gray) async def choice3(self, interaction: discord.Interaction, button: ui.Button): if self.addonChoices[2][2] is True: self.gotCorrectAnswer = True - self.endGame() + await self.endGame(interaction) else: - self.choice2.style = discord.ButtonStyle.red - self.endGame() + self.choice3.style = discord.ButtonStyle.red + await self.endGame(interaction) class Games(commands.Cog): @@ -103,27 +136,16 @@ class Games(commands.Cog): @app_commands.command(name="guessaddon", description="Guess what addon it is based on the image.") async def guessaddon(self, interaction: discord.Interaction): - game = GuessAddonGame(self.bot) + game = GuessAddonGame(self.bot, interaction.user.id) await game.initGame() await interaction.response.send_message(embed=discord.Embed( - title = "Guess what the addon is", - color = self.bot.accent_color, - ).set_image(url=game.correctAddonImage), view=game) + title="Guess what the addon is", + color=self.bot.accent_color, + ).set_image(url=game.correctAddonImage) + .set_footer(text="You have 15 seconds to answer."), view=game) await game.wait() - - if game.gotCorrectAnswer: - await interaction.edit_original_response(embed=discord.Embed( - title="Great Job!", - description="You got the correct answer!", - color=self.bot.accent_color - ), view=game) - else: - await interaction.edit_original_response(embed=discord.Embed( - title="Wrong answer!", - description="Better luck next time!", - color=self.bot.accent_color - ), view=game) - + game.interaction = interaction + async def setup(bot: Lina): await bot.add_cog(Games(bot)) diff --git a/cogs/pokemap.py b/cogs/pokemap.py new file mode 100644 index 0000000..2f3279c --- /dev/null +++ b/cogs/pokemap.py @@ -0,0 +1,97 @@ +from __future__ import annotations + +import discord +from discord import app_commands +from discord.ext import commands + +from utils import formatting + +import datetime +import logging +import random +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from bot import Lina + +log = logging.getLogger("lina.cogs.pokemap") + +class PokeMap(commands.Cog): + + def __init__(self, bot: Lina): + self.bot: Lina = bot + + @app_commands.command(name="pokemap") + async def pokemap(self, interaction: discord.Interaction): + + try: + + cooldown = await self.bot.pool.fetchrow( + """ + SELECT cooldown FROM lina_discord_pokemap + WHERE id = $1 + """, + interaction.user.id + ) + + if cooldown: + + if cooldown["cooldown"].timestamp() > \ + datetime.datetime.now().timestamp(): + + return await interaction.response.send_message(embed=discord.Embed( + description="You need to wait **{time}** before catching another pokemap.".format( + time=formatting.humanize_timedelta( + timedelta=cooldown["cooldown"] - datetime.datetime.now() + + )), + color=self.bot.accent_color + )) + + addon = random.choice( + await self.bot.pool.fetch( + "SELECT id FROM lina_discord_addons;") + )["id"] + addonInfo = await self.bot.pool.fetchrow( + """ + SELECT id, name, image FROM lina_discord_addons + WHERE id = $1 + """, addon + ) + + await self.bot.pool.execute( + """ + INSERT INTO lina_discord_pokemap + (id, maps, cooldown) + VALUES ($1, $2::text[], current_timestamp + '2h' ::interval) + ON CONFLICT (id) DO UPDATE SET + maps = array_append(lina_discord_pokemap.maps, $3), + cooldown = current_timestamp + '2h' ::interval + """, + interaction.user.id, {addonInfo["id"]}, + addonInfo["id"] + ) + except Exception: + log.exception("Could not set pokemap for user %s", interaction.user.id) + return await interaction.response.send_message(embed=discord.Embed( + title="Error", + description="A database error has occurred. Please contact the developer.", + color=self.bot.accent_color + ), ephemeral=True) + + await interaction.response.send_message(embed=discord.Embed( + description=( + "{user}, you've caught a **{addonName}**!\n\n" + "`/installaddon {addonId}`" + ).format( + user=interaction.user.mention, + addonName=addonInfo["name"], + addonId=addonInfo["id"] + ), + color=self.bot.accent_color + ).set_thumbnail(url=addonInfo["image"]) + ) + + +async def setup(bot: Lina): + await bot.add_cog(PokeMap(bot))