actually add pokemap and interaction failed fix for guessaddon
This commit is contained in:
parent
5c275de591
commit
48d3ac0be2
2 changed files with 147 additions and 28 deletions
|
@ -12,12 +12,31 @@ if TYPE_CHECKING:
|
||||||
|
|
||||||
class GuessAddonGame(ui.View):
|
class GuessAddonGame(ui.View):
|
||||||
|
|
||||||
def __init__(self, bot: Lina):
|
def __init__(self, bot: Lina, author_id: int):
|
||||||
super().__init__()
|
super().__init__(timeout=15)
|
||||||
|
|
||||||
self.addonChoices = []
|
self.addonChoices = []
|
||||||
self.bot: Lina = bot
|
self.bot: Lina = bot
|
||||||
|
self.author_id = author_id
|
||||||
self.gotCorrectAnswer = False
|
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):
|
async def initGame(self):
|
||||||
self.correctAddon = random.choice(
|
self.correctAddon = random.choice(
|
||||||
|
@ -61,39 +80,53 @@ class GuessAddonGame(ui.View):
|
||||||
self.children[c].label = self.addonChoices[c][1]
|
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)):
|
for i in range(len(self.children)):
|
||||||
if self.addonChoices[i][2] is True:
|
if self.addonChoices[i][2] is True:
|
||||||
self.children[i].style = discord.ButtonStyle.green
|
self.children[i].style = discord.ButtonStyle.green
|
||||||
self.children[i].disabled = True
|
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()
|
self.stop()
|
||||||
|
|
||||||
@ui.button(style=discord.ButtonStyle.gray)
|
@ui.button(style=discord.ButtonStyle.gray)
|
||||||
async def choice1(self, interaction: discord.Interaction, button: ui.Button):
|
async def choice1(self, interaction: discord.Interaction, button: ui.Button):
|
||||||
if self.addonChoices[0][2] is True:
|
if self.addonChoices[0][2] is True:
|
||||||
self.gotCorrectAnswer = True
|
self.gotCorrectAnswer = True
|
||||||
self.endGame()
|
await self.endGame(interaction)
|
||||||
else:
|
else:
|
||||||
self.choice1.style = discord.ButtonStyle.red
|
self.choice1.style = discord.ButtonStyle.red
|
||||||
self.endGame()
|
await self.endGame(interaction)
|
||||||
|
|
||||||
@ui.button(style=discord.ButtonStyle.gray)
|
@ui.button(style=discord.ButtonStyle.gray)
|
||||||
async def choice2(self, interaction: discord.Interaction, button: ui.Button):
|
async def choice2(self, interaction: discord.Interaction, button: ui.Button):
|
||||||
if self.addonChoices[1][2] is True:
|
if self.addonChoices[1][2] is True:
|
||||||
self.gotCorrectAnswer = True
|
self.gotCorrectAnswer = True
|
||||||
self.endGame()
|
await self.endGame(interaction)
|
||||||
else:
|
else:
|
||||||
self.choice2.style = discord.ButtonStyle.red
|
self.choice2.style = discord.ButtonStyle.red
|
||||||
self.endGame()
|
await self.endGame(interaction)
|
||||||
|
|
||||||
@ui.button(style=discord.ButtonStyle.gray)
|
@ui.button(style=discord.ButtonStyle.gray)
|
||||||
async def choice3(self, interaction: discord.Interaction, button: ui.Button):
|
async def choice3(self, interaction: discord.Interaction, button: ui.Button):
|
||||||
if self.addonChoices[2][2] is True:
|
if self.addonChoices[2][2] is True:
|
||||||
self.gotCorrectAnswer = True
|
self.gotCorrectAnswer = True
|
||||||
self.endGame()
|
await self.endGame(interaction)
|
||||||
else:
|
else:
|
||||||
self.choice2.style = discord.ButtonStyle.red
|
self.choice3.style = discord.ButtonStyle.red
|
||||||
self.endGame()
|
await self.endGame(interaction)
|
||||||
|
|
||||||
|
|
||||||
class Games(commands.Cog):
|
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.")
|
@app_commands.command(name="guessaddon", description="Guess what addon it is based on the image.")
|
||||||
async def guessaddon(self, interaction: discord.Interaction):
|
async def guessaddon(self, interaction: discord.Interaction):
|
||||||
game = GuessAddonGame(self.bot)
|
game = GuessAddonGame(self.bot, interaction.user.id)
|
||||||
await game.initGame()
|
await game.initGame()
|
||||||
|
|
||||||
await interaction.response.send_message(embed=discord.Embed(
|
await interaction.response.send_message(embed=discord.Embed(
|
||||||
title = "Guess what the addon is",
|
title="Guess what the addon is",
|
||||||
color = self.bot.accent_color,
|
color=self.bot.accent_color,
|
||||||
).set_image(url=game.correctAddonImage), view=game)
|
).set_image(url=game.correctAddonImage)
|
||||||
|
.set_footer(text="You have 15 seconds to answer."), view=game)
|
||||||
await game.wait()
|
await game.wait()
|
||||||
|
game.interaction = interaction
|
||||||
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)
|
|
||||||
|
|
||||||
async def setup(bot: Lina):
|
async def setup(bot: Lina):
|
||||||
await bot.add_cog(Games(bot))
|
await bot.add_cog(Games(bot))
|
||||||
|
|
97
cogs/pokemap.py
Normal file
97
cogs/pokemap.py
Normal file
|
@ -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))
|
Loading…
Reference in a new issue