-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
235 lines (205 loc) · 9.95 KB
/
utils.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
228
229
230
231
232
233
234
235
import discord
import asyncio
ACCENT_COLOR = 0x5271FF
class SimonSaysGame:
def __init__(
self,
simon: discord.Member,
guild: discord.Guild,
role: discord.Role,
channel: discord.TextChannel,
):
self.simon = simon
self.guild = guild
self.role = role
self.channel = channel
self.started = False
self.winner = None
self._elim_all = False
self._simon_nick = "simon"
self._talked = set()
self._to_say = None
self._to_not_say = None
@property
def player_count(self):
return len(self.role.members)
@property
def players_string(self):
return ', '.join(m.mention for m in self.role.members)
@property
def simon_says_line(self):
return f"{self._simon_nick} says"
async def start(self):
self.started = True
em = discord.Embed(
title="Simon Says Game is Starting!",
description=f"In this game you must listen to the Simon ({self.simon.mention}) and follow his orders. If you fail to do so you'll be eliminated! The last person remaining wins!",
color=ACCENT_COLOR,
)
await self.channel.send(embed=em)
await self.channel.send(
f"Players ({self.player_count}): {self.players_string}"
)
async def eliminate(self, member: discord.Member, ctx=None, reason=None):
if self.role not in member.roles:
return
await member.remove_roles(self.role)
send = ctx.respond if ctx else self.channel.send
await send(
f"{member.mention} has been eliminated. {'Reason: '+reason if reason else 'How sad'}. {self.player_count} players remain."
)
await self.check_winner()
async def mass_eliminate(self, members: list[discord.Member], reason=None):
if self.winner:
return
embed = discord.Embed(title="Eliminating people...", description=f"Eliminating {len(members)} contenstant{'' if len(members)==1 else 's'}.", color=ACCENT_COLOR)
if reason:
embed.add_field(name="Reason", value=reason)
ping = ", ".join(m.mention for m in members) or "** **"
for m in members:
await m.remove_roles(self.role)
await self.channel.send(
ping, embed=embed
)
await self.check_winner()
async def handle_message(self, msg: discord.Message, bot: discord.Bot):
msg.content = msg.content.lower()
if msg.author == self.simon:
if msg.content.startswith(self.simon_says_line):
match msg.content.split()[2:]:
case [self._simon_nick, "is", "now", new_nick, *_]:
self._simon_nick = new_nick
await msg.add_reaction("✅")
case ["talk", *_] | ["afk", "check", *_]:
self._talked = set()
await asyncio.sleep(12)
await self.mass_eliminate([m for m in self.role.members if m not in self._talked], reason="AFK | Didn't talk")
case ["shut"] | ["don't", "talk"]:
self._elim_all = True
await asyncio.sleep(12)
self._elim_all = False
case ["change", "your", "status", "to", status, *_]:
await asyncio.sleep(12)
await self.mass_eliminate([m for m in self.role.members if str(m.status) != status.lower()], reason="Didn't change their status")
case ["change", "your", "nickname" | "name" | "nick", "to", *nick]:
nick = " ".join(nick)
await asyncio.sleep(16)
await self.mass_eliminate([m for m in self.role.members if str(m.display_name).lower() != nick], reason="Didn't change their name")
case ["say", *words]:
self._to_say = " ".join(words)
self._talked = set()
await asyncio.sleep(12)
await self.mass_eliminate([m for m in self.role.members if m not in self._talked], reason="AFK | Didn't talk")
self._to_say = None
case ["what", "is", *_] | ["what's", *_]:
self._talked = set()
await asyncio.sleep(16)
await self.mass_eliminate([m for m in self.role.members if m not in self._talked], reason="AFK | Didn't answer")
if "below" in msg.content:
try:
m = await bot.wait_for(
"message",
check=lambda ms: ms.channel == self.channel,
timeout=15,
)
except asyncio.TimeoutError:
pass
if "lose" in msg.content:
await self.eliminate(m.author)
else:
await self.channel.send(
f"{m.author.mention} was the person below."
)
else:
words = msg.content.split()
if len(words) > 1 and "say" in words[1]:
words = words[2:]
match words:
case ["talk", *_] | ["afk", "check", *_]:
self._elim_all = True
await asyncio.sleep(12)
self._elim_all = False
case ["shut"] | ["don't", "talk"]:
self._talked = set()
await asyncio.sleep(12)
await self.mass_eliminate([m for m in self.role.members if m not in self._talked], reason="AFK | Didn't talk")
case ["change", "your", "status", "to", status, *_]:
await asyncio.sleep(12)
await self.mass_eliminate([m for m in self.role.members if str(m.status) == status.lower()], reason="Changed their status")
case ["change", "your", "nickname" | "name", "to", nick, *_]:
await asyncio.sleep(12)
await self.mass_eliminate([m for m in self.role.members if str(m.display_name).lower() == nick], reason="Changed their nickname")
case ["say", *words]:
self._to_not_say = " ".join(words)
await asyncio.sleep(12)
self._to_not_say = None
if "below" in msg.content:
try:
m = await bot.wait_for(
"message",
check=lambda ms: ms.channel == self.channel,
timeout=15,
)
except asyncio.TimeoutError:
pass
if "win" in msg.content:
await self.eliminate(m.author)
else:
await self.channel.send(
f"{m.author.mention} was the person below."
)
elif self.role in msg.author.roles:
self._talked.add(msg.author)
if self._elim_all:
await self.eliminate(msg.author)
if self._to_not_say and msg.content == self._to_not_say:
await self.eliminate(msg.author)
if self._to_say and msg.content != self._to_say:
await self.eliminate(msg.author)
async def check_winner(self):
if self.winner:
return
if self.player_count == 1:
self.winner = self.role.members[0]
await self.channel.send(
f"We have a winner! {self.winner.mention} has won the game! GG! Thanks for playing!"
)
await self.winner.remove_roles(self.role)
elif self.player_count == 0:
self.winner = True
await self.channel.send(
f"Everyone lost, no winner!"
)
class StartView(discord.ui.View):
def __init__(self, game: SimonSaysGame):
self.game = game
super().__init__()
@discord.ui.button(label="Join", style=discord.ButtonStyle.blurple, emoji="🙋♂️")
async def join_callback(self, button, interaction: discord.Interaction):
if interaction.user == self.game.simon:
return await interaction.response.send_message(
"You are the simon! You cannot join the game.", ephemeral=True
)
elif self.game.role in interaction.user.roles:
return await interaction.response.send_message(
"You are already a participant!", ephemeral=True
)
await interaction.user.add_roles(self.game.role)
await interaction.response.send_message(
"You are now a participant!", ephemeral=True
)
em = interaction.message.embeds[0]
em.clear_fields()
em.add_field(name="Player Count", value=str(self.game.player_count))
em.add_field(name="Players", value=self.game.players_string)
await interaction.message.edit(embed=em)
@discord.ui.button(label="Start game!", style=discord.ButtonStyle.green, emoji="✨")
async def start_callback(self, button, interaction: discord.Interaction):
if interaction.user == self.game.simon:
await interaction.response.send_message("Starting game!", ephemeral=True)
self.stop()
await self.game.start()
else:
await interaction.response.send_message(
"You can't start the game, you aren't the simon!", ephemeral=True
)