-
Notifications
You must be signed in to change notification settings - Fork 0
Rock paper Scissors with bot command Elias Cueto #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EliasCueto45
wants to merge
2
commits into
main
Choose a base branch
from
onboarding-elias
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
84 changes: 84 additions & 0 deletions
84
src/capy_app/frontend/cogs/onboarding/elias_onboarding_cog.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import discord | ||
| import logging | ||
| from discord.ext import commands | ||
| from discord import app_commands | ||
|
|
||
| from frontend import config_colors as colors | ||
| from config import settings | ||
| import random | ||
|
|
||
| class EliasCog(commands.Cog): | ||
| def __init__(self, bot: commands.Bot): | ||
| self.bot = bot | ||
| self.logger = logging.getLogger( | ||
| f"discord.cog.{self.__class__.__name__.lower()}" | ||
| ) | ||
| self.status_emojis = { | ||
| "🪨": "Rock", | ||
| "🧻": "Paper", | ||
| "✂️": "Scissors", | ||
| } | ||
|
|
||
| @app_commands.guilds(discord.Object(id=settings.DEBUG_GUILD_ID)) | ||
| @app_commands.command(name="rps", description="Plays Rock Paper Scissors with the bot!", ) | ||
| async def rps(self, interaction: discord.Interaction): | ||
|
|
||
| message = f"React to play!" | ||
| self.logger.info(message) | ||
| embed = discord.Embed( | ||
| title="Rock Paper Scissors", | ||
| description=message, | ||
| color=colors.GUILD, | ||
| ) | ||
| await interaction.response.send_message(embed=embed) | ||
| msg = await interaction.original_response() | ||
| for emoji in self.status_emojis.keys(): | ||
| await msg.add_reaction(emoji) | ||
| self.bot.rps_message_id = msg.id | ||
| self.bot.rps_user_id = interaction.user.id | ||
|
|
||
| def determine_winner(self, user_choice: str, bot_choice: str) -> str: | ||
| if user_choice == bot_choice: | ||
| return "tied" | ||
| wins_against = { | ||
| "Rock": "Scissors", | ||
| "Scissors": "Paper", | ||
| "Paper": "Rock", | ||
| } | ||
| if wins_against[user_choice] == bot_choice: | ||
| return "won ⭐" | ||
| else: | ||
| return "lost ❌" | ||
|
|
||
| @commands.Cog.listener() | ||
| async def on_reaction_add(self, reaction: discord.Reaction, user: discord.User): | ||
| if user.bot: | ||
| return | ||
|
|
||
| if not hasattr(self.bot, "rps_message_id") or reaction.message.id != self.bot.rps_message_id: | ||
| return | ||
|
|
||
| if user.id != self.bot.rps_user_id: | ||
| return | ||
|
|
||
| choice = self.status_emojis.get(str(reaction.emoji)) | ||
| if not choice: | ||
| return | ||
| # Delete reaction version | ||
| await reaction.message.clear_reactions() | ||
|
|
||
| #Delete message version | ||
| #wait reaction.message.delete() | ||
|
|
||
| # Choose bot's random move | ||
| import random | ||
| bot_choice = random.choice(["Rock", "Paper", "Scissors"]) | ||
| result = self.determine_winner(choice, bot_choice) | ||
|
|
||
| await reaction.message.channel.send( | ||
| f"{user.mention} chose **{choice}**, I chose **{bot_choice}**. You **{result}**!" | ||
| ) | ||
|
|
||
|
|
||
| async def setup(bot: commands.Bot): | ||
| await bot.add_cog(EliasCog(bot)) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider refactoring the command into a standalone function without a Cog, custom logger, or unused mappings to simplify the implementation.
Steps to apply:
EliasCogclass andsetupfunction entirely.commands/rps.py) with the above code.commands/rps.py(e.g. viabot.tree.copy_global_to(guild=...)or your existing command loader).status_emojisdict and custom logger import.This keeps the slash-command, the embed, and the reactions, with far less indirection.