Untitled
unknown
python
a month ago
4.4 kB
2
Indexable
Never
class Pokecatch(View): def __init__(self, user_id): self.user_id = user_id self.clicked = 0 self.retries = 0 self.catched = False self.choosen = None self.item_name = None self.emoji = None self.max_tries = pyrandom.randint(2, 5) def create_embed(self, title, description, emoji): return Message( content=emoji, title=title, description=description, color=0x248046, ) async def _check_user_permission(self, interaction): if interaction.user.id != self.user_id: await defer(interaction, action="create", ephemeral=True) respond_interaction(interaction, "❌ Vous ne pouvez pas cliquer sur ce bouton.") return False return True @button(label="Capturer", style="success") async def catch_button(self, interaction, button): if not await self._check_user_permission(interaction): return await defer(interaction, action="update") if not self.catched: self.clicked += 1 if self.clicked > self.max_tries: embed = self.create_embed( "Oh non...", f"**{self.item_name}** s'est échappé après **{self.clicked}** essais... 😔", "" ) respond_interaction(interaction, message=embed, components=None) self.stop() else: capture_chance = min(self.choosen['weight'] * (0.1 * self.clicked), 1.0) if pyrandom.random() < capture_chance: self.catched = True self.emoji = "<:pokeball:1284566873853136986>" embed = self.create_embed( "Félicitations !", f"Tu as capturé **{self.item_name}** ! ✨", self.emoji ) items_db = SlashMongo("inventory", "items") await items_db.update_one( {"item_numb": str(self.choosen["item_numb"]), "user_id": str(self.user_id)}, {"$inc": {"count": 1}}, upsert=True ) respond_interaction(interaction, message=embed, components=None) self.stop() else: embed = self.create_embed( "❌ Mince !", f"Ça a échoué, continuez ! Essais N° **{self.clicked}**", self.emoji ) respond_interaction(interaction, message=embed, components=self) @button(label="Relancer", style="primary") async def retry_button(self, interaction, button): if not await self._check_user_permission(interaction): return await defer(interaction, action="update") if self.retries < 3: self.retries += 1 self.clicked = 0 self.catched = False await self.start_view(interaction) else: embed = self.create_embed( "❌ Limite atteinte", "Vous avez atteint le nombre maximum de relances.", self.emoji ) respond_interaction(interaction, message=embed, components=None) self.stop() async def start_view(self, interaction): emojis_db = SlashMongo("inventory", "emojis") all_emojis = await emojis_db.find({}) weights = list(map(lambda item: item['weight'], all_emojis)) self.choosen = pyrandom.choices(all_emojis, weights)[0] self.item_name = self.choosen['emoji_name'] self.emoji = f"<a:{self.item_name}:{self.choosen['emoji_id']}>" remaining_retries = 3 - self.retries embed = self.create_embed( f"Un **{self.item_name}** sauvage apparait...", f"Relances restantes: **{remaining_retries}**", self.emoji ) respond_interaction( interaction, message=embed, components=self, attachments=None ) view = Pokecatch(user.id) await view.start_view(interaction) exit_code = await view.wait(scope=scope) if exit_code == "timeout": set_error("⏰ Timeout, vous avez été déconnecté.")
Leave a Comment