-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrps.py
227 lines (206 loc) · 14 KB
/
rps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import discord
from discord import app_commands
from discord.ext import commands
import random
import asyncio
class Player1Buttons(discord.ui.View):
def __init__(self, *, timeout=180):
super().__init__(timeout=timeout)
@discord.ui.button(label="Rock", style=discord.ButtonStyle.blurple, emoji="🪨") # or .primary
async def p1_rock(self, interaction:discord.Interaction, button:discord.ui.Button):
if interaction.user != player1:
await interaction.response.send_message("> This is not your turn/game!", ephemeral = True)
else:
global player1_choice
player1_choice = "rock 🪨"
if enemy==None or enemy.id==855437723166703616:
comp_choice = random.choice(rpsGame)
if comp_choice == 'rock 🪨':
await interaction.response.send_message(f'Well, that was weird. We tied.\n>>> Your choice: **{player1_choice}**\nMy choice: **{comp_choice}**', view=playAgain())
elif comp_choice == 'paper 🧻':
await interaction.response.send_message(f'Nice try, but I won this time!!\n>>> Your choice: **{player1_choice}**\nMy choice: **{comp_choice}**', view=playAgain())
elif comp_choice == 'scissors ✂️':
await interaction.response.send_message(f"Aw, you beat me. It won't happen again!\n>>> Your choice: **{player1_choice}**\nMy choice: **{comp_choice}**", view=playAgain())
for child in self.children:
child.disabled=True
await interaction.message.edit(view=self)
else:
#player 2 msg
view = Player2Buttons()
embed = discord.Embed(description=f"Waiting for **{enemy.name}** choice..\nRock, paper, or scissors? Choose wisely...")
await interaction.message.edit(content=f"{enemy.mention}'s turn", embed = embed, view=view) #main message
await interaction.response.defer()
@discord.ui.button(label="Paper", style=discord.ButtonStyle.blurple, emoji="🧻") # or .secondary/.grey
async def p1_paper(self, interaction:discord.Interaction, button:discord.ui.Button):
if interaction.user != player1:
await interaction.response.send_message("> This is not your turn/game!", ephemeral = True)
else:
global player1_choice
player1_choice = "paper 🧻"
if enemy==None or enemy.id==855437723166703616:
comp_choice = random.choice(rpsGame)
if comp_choice == 'rock 🪨':
await interaction.response.send_message(f'Aw man, you actually managed to beat me.\n>>> Your choice: **{player1_choice}**\nMy choice: **{comp_choice}**', view=playAgain())
elif comp_choice == 'paper 🧻':
await interaction.response.send_message(f'Oh, wacky. We just tied. I call a rematch!!\n>>> Your choice: **{player1_choice}**\nMy choice: **{comp_choice}**', view=playAgain())
elif comp_choice == 'scissors ✂️':
await interaction.response.send_message(f"I WON!!!\n>>> Your choice: **{player1_choice}**\nMy choice: **{comp_choice}**", view=playAgain())
for child in self.children:
child.disabled=True
await interaction.message.edit(view=self)
else:
#player 2 msg
view = Player2Buttons()
embed = discord.Embed(description=f"Waiting for **{enemy.name}** choice..\nRock, paper, or scissors? Choose wisely...")
await interaction.message.edit(content=f"{enemy.mention}'s turn", embed = embed, view=view) #main message
await interaction.response.defer()
@discord.ui.button(label="Scissors", style=discord.ButtonStyle.blurple, emoji="✂️") # or .success
async def p1_scissors(self, interaction:discord.Interaction, button:discord.ui.Button):
if interaction.user != player1:
await interaction.response.send_message("> This is not your turn/game!", ephemeral = True)
else:
global player1_choice
player1_choice = "scissors ✂️"
if enemy==None or enemy.id==855437723166703616:
comp_choice = random.choice(rpsGame)
if comp_choice == 'rock 🪨':
await interaction.response.send_message(f'HAHA!! I JUST CRUSHED YOU!! I rock!!\n>>> Your choice: **{player1_choice}**\nMy choice: **{comp_choice}**', view=playAgain())
elif comp_choice == 'paper 🧻':
await interaction.response.send_message(f'Bruh. >: |\n>>> Your choice: **{player1_choice}**\nMy choice: **{comp_choice}**', view=playAgain())
elif comp_choice == 'scissors ✂️':
await interaction.response.send_message(f"Oh well, we tied.\n>>> Your choice: **{player1_choice}**\nMy choice: **{comp_choice}**", view=playAgain())
for child in self.children:
child.disabled=True
await interaction.message.edit(view=self)
else:
#player 2 msg
view = Player2Buttons()
embed = discord.Embed(description=f"Waiting for **{enemy.name}** choice..\nRock, paper, or scissors? Choose wisely...")
await interaction.message.edit(content=f"{enemy.mention}'s turn", embed = embed, view=view) #main message
await interaction.response.defer()
class Player2Buttons(discord.ui.View):
def __init__(self, *, timeout=180):
super().__init__(timeout=timeout)
@discord.ui.button(label="Rock", style=discord.ButtonStyle.blurple, emoji="🪨") # or .primary
async def p2_rock(self, interaction:discord.Interaction, button:discord.ui.Button):
if interaction.user != enemy:
await interaction.response.send_message("> This is not your turn/game!", ephemeral = True)
else:
global player2_choice
player2_choice = "rock 🪨"
if player1_choice == 'rock 🪨':
await interaction.response.send_message(f'Well, that was weird. Both of you tied.\n>>> {player1.mention} choice: **{player1_choice}**\n{enemy.mention} choice: **{player2_choice}**', view=playAgain())
elif player1_choice == 'paper 🧻':
await interaction.response.send_message(f'The pen beats the sword? More like the paper beats the rock!!\n>>> {player1.mention} choice: **{player1_choice}**\n{enemy.mention} choice: **{player2_choice}**', view=playAgain())
elif player1_choice == 'scissors ✂️':
await interaction.response.send_message(f'HAHA!! **{enemy.name}** JUST CRUSHED **{player1.name}**!!\n>>> {player1.mention} choice: **{player1_choice}**\n{enemy.mention} choice: **{player2_choice}**', view=playAgain())
for child in self.children:
child.disabled=True
await interaction.message.edit(view=self)
@discord.ui.button(label="Paper", style=discord.ButtonStyle.blurple, emoji="🧻") # or .secondary/.grey
async def p2_paper(self, interaction:discord.Interaction, button:discord.ui.Button):
if interaction.user != enemy:
await interaction.response.send_message("> This is not your turn/game!", ephemeral = True)
else:
global player2_choice
player2_choice = "paper"
if player1_choice == 'rock 🪨':
await interaction.response.send_message(f'Nice try **{player1.name}**, but **{enemy.name}** won this time!!\n>>> {player1.mention} choice: **{player1_choice}**\n{enemy.mention} choice: **{player2_choice}**', view=playAgain())
elif player1_choice == 'paper 🧻':
await interaction.response.send_message(f'Oh, wacky. you just tied. I call a rematch!!\n>>> {player1.mention} choice: **{player1_choice}**\n{enemy.mention} choice: **{player2_choice}**', view=playAgain())
elif player1_choice == 'scissors ✂️':
await interaction.response.send_message(f'Bruh. >: |\n>>> **{player1.name}** choice: **{player1_choice}**\n{enemy.mention} choice: **{player2_choice}**', view=playAgain())
for child in self.children:
child.disabled=True
await interaction.message.edit(view=self)
@discord.ui.button(label="Scissors", style=discord.ButtonStyle.blurple, emoji="✂️") # or .success
async def p2_scissors(self, interaction:discord.Interaction, button:discord.ui.Button):
if interaction.user != enemy:
await interaction.response.send_message("> This is not your turn/game!", ephemeral = True)
else:
global player2_choice
player2_choice = "scissors ✂️"
if player1_choice == 'rock 🪨':
await interaction.response.send_message(f"Aw, **{player1.name}** beat **{enemy.name}**. Hard luck **{enemy.name}**!\n>>> {player1.mention} choice: **{player1_choice}**\n{enemy.mention} choice: **{player2_choice}**", view=playAgain())
elif player1_choice == 'paper 🧻':
await interaction.response.send_message(f"Aw man, **{enemy.name}** actually managed to beat **{player1.name}**.\n>>> {player1.mention} choice: **{player1_choice}**\n{enemy.mention} choice: **{player2_choice}**", view=playAgain())
elif player1_choice == 'scissors ✂️':
await interaction.response.send_message(f"Oh well, you tied.\n>>> {player1.mention} choice: **{player1_choice}**\n{enemy.mention} choice: **{player2_choice}**", view=playAgain())
for child in self.children:
child.disabled=True
await interaction.message.edit(view=self)
class playAgain(discord.ui.View):
def __init__(self,*, timeout=180):
super().__init__(timeout=timeout)
@discord.ui.button(label="Play Again",style=discord.ButtonStyle.green) # or .primary
async def play_again(self, interaction:discord.Interaction, button:discord.ui.Button):
if interaction.user != player1:
await interaction.response.send_message("> Only player 1 can click the button", ephemeral = True)
else:
if enemy==None or enemy.id==855437723166703616:
view = Player1Buttons()
embed = discord.Embed(description="Rock, paper, or scissors? Choose wisely...")
await interaction.response.send_message(embed = embed, view=view) #main message
elif enemy.bot:
return await interaction.response.send_message("> You can play with me or with another member. Not another bot!", ephemeral=True)
else:
#player 1 msg
view = Player1Buttons()
embed = discord.Embed(description=f"Waiting for **{player1.name}** choice..\nRock, paper, or scissors? Choose wisely...")
await interaction.response.send_message(f"{player1.mention}'s turn", embed = embed, view=view) #main message
for child in self.children:
child.disabled=True
await interaction.message.edit(view=self)
class RPS(commands.Cog):
GAME_TIMEOUT_THRESHOLD = 180
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
#wakin~
@commands.Cog.listener()
async def on_ready(self):
print("RPS is online.")
#rps command
@commands.hybrid_command(name = "rps", with_app_command = True, description = "Play Rock Paper Scissors.")
@app_commands.describe(player2 = "Player to challenge (default is the bot).")
@commands.cooldown(1, 10, commands.BucketType.user)
async def rps(self, ctx, player2: discord.Member=None):
global enemy
global player1
global rpsGame
player1 = ctx.author
enemy = player2
rpsGame = ['rock 🪨', 'paper 🧻', 'scissors ✂️']
if player2 == player1:
return await ctx.send(">>> You can not play with yourself!\nYou can play with me if you feel that lonely...", ephemeral=True)
elif player2 == None or player2 == self.bot.user:
view = Player1Buttons()
embed = discord.Embed(description="Rock, paper, or scissors? Choose wisely...")
await ctx.send(embed = embed, view=view) #main message
#wait for buttons timeout
#idk if this works
try:
await self.bot.wait_for('interaction', check=lambda interaction: interaction.data["component_type"] == 2 and "custom_id" in interaction.data.keys(), timeout=self.GAME_TIMEOUT_THRESHOLD)
except asyncio.TimeoutError:
await ctx.reply(f"> {player1.mention} ran out of time and lost.")
elif player2.bot:
return await ctx.send("> You can play with me or with another member. Not another bot!", ephemeral=True)
else:
#player 1 msg
view = Player1Buttons()
embed = discord.Embed(description=f"Waiting for **{player1.name}** choice..\nRock, paper, or scissors? Choose wisely...")
await ctx.send(f"{player1.mention}'s turn", embed = embed, view=view) #main message
#wait for buttons timeout
#idk if this actually works
try:
await self.bot.wait_for('interaction', check=lambda interaction: interaction.data["component_type"] == 2 and "custom_id" in interaction.data.keys(), timeout=self.GAME_TIMEOUT_THRESHOLD)
except asyncio.TimeoutError:
await ctx.reply(f"> {player1.mention} ran out of time and lost.")
@rps.error
async def rpsl_error(self, ctx, error):
if isinstance(error, commands.CommandOnCooldown):
cool_error = discord.Embed(title=f"Slow it down bro!",
description=f"> Try again in {error.retry_after:.2f}s.",
colour=discord.Colour.light_grey())
await ctx.reply(embed=cool_error)
async def setup(bot: commands.Bot) -> None:
await bot.add_cog(RPS(bot))