1- from redbot .core import commands # isort:skip
2- from redbot .core .i18n import Translator # isort:skip
3- import discord # isort:skip
4- import typing # isort:skip
5-
61import asyncio
72import datetime
83import random
94import string
5+ import typing
106from collections import Counter
117
8+ import discord
129from prettytable import PrettyTable
10+
11+ from redbot .core import commands
12+ from redbot .core .i18n import Translator
1313from redbot .core .utils .chat_formatting import box , humanize_list
1414
1515_ : Translator = Translator ("AcronymGame" , __file__ )
@@ -21,7 +21,9 @@ def __init__(self, parent: discord.ui.View) -> None:
2121 self ._parent : discord .ui .View = parent
2222 self .answer : discord .ui .TextInput = discord .ui .TextInput (
2323 label = _ ("Answer for {acronym} acronym" ).format (acronym = self ._parent .acronym ),
24- placeholder = _ ("Your full name for {acronym} acronym" ).format (acronym = self ._parent .acronym ),
24+ placeholder = _ ("Your full name for {acronym} acronym" ).format (
25+ acronym = self ._parent .acronym ,
26+ ),
2527 default = None ,
2628 style = discord .TextStyle .short ,
2729 custom_id = "description" ,
@@ -37,26 +39,25 @@ async def on_submit(self, interaction: discord.Interaction) -> None:
3739 if interaction .user in self ._parent .players :
3840 await interaction .response .send_message (
3941 _ ("You have already joined this game, with `{answer}` answer." ).format (
40- answer = self ._parent .players [interaction .user ]
42+ answer = self ._parent .players [interaction .user ],
4143 ),
4244 ephemeral = True ,
4345 )
4446 return
4547 if len (self ._parent .players ) >= 20 :
46- await interaction .response .send_message (
47- _ ("Sorry, maximum 20 players." ), ephemeral = True
48- )
48+ await interaction .response .send_message (_ ("Sorry, maximum 20 players." ), ephemeral = True )
4949 return
5050 if self ._parent ._mode != "join" :
5151 await interaction .response .send_message (
52- _ ("Sorry, the vote has already started." ), ephemeral = True
52+ _ ("Sorry, the vote has already started." ),
53+ ephemeral = True ,
5354 )
5455 return
5556 answer = self .answer .value .strip ()
5657 if len (answer .split (" " )) != len (self ._parent .acronym ):
5758 await interaction .response .send_message (
5859 _ ("Sorry, the number of words in your answer must be {len_words}." ).format (
59- len_words = len (self ._parent .acronym )
60+ len_words = len (self ._parent .acronym ),
6061 ),
6162 ephemeral = True ,
6263 )
@@ -65,7 +66,7 @@ async def on_submit(self, interaction: discord.Interaction) -> None:
6566 if answer .split (" " )[i ][0 ].upper () != letter .upper ():
6667 await interaction .response .send_message (
6768 _ (
68- "Sorry, the initial of each word in your answer must be each letter of the acronym ({acronym})."
69+ "Sorry, the initial of each word in your answer must be each letter of the acronym ({acronym})." ,
6970 ).format (acronym = self ._parent .acronym ),
7071 ephemeral = True ,
7172 )
@@ -77,20 +78,22 @@ async def on_submit(self, interaction: discord.Interaction) -> None:
7778 for num , (__ , answer ) in enumerate (self ._parent .players .items ()):
7879 table .add_row ([num + 1 , answer ])
7980 embed .description = _ (
80- "Join the game by clicking on the button below and entering your acronym. Maximum 20 players.\n "
81+ "Join the game by clicking on the button below and entering your acronym. Maximum 20 players.\n " ,
8182 ) + box (str (table ), lang = "py" )
8283 self ._parent ._message : discord .Message = await self ._parent ._message .edit (embed = embed )
8384 await interaction .response .send_message (
84- _ ("Game joined with `{answer}` answer." ).format (answer = answer ), ephemeral = True
85+ _ ("Game joined with `{answer}` answer." ).format (answer = answer ),
86+ ephemeral = True ,
8587 )
8688
8789
8890class AnswerSelect (discord .ui .Select ):
89- def __init__ (self , parent : discord .ui .View , players : typing . Dict [discord .Member , int ]) -> None :
91+ def __init__ (self , parent : discord .ui .View , players : dict [discord .Member , int ]) -> None :
9092 self ._parent : discord .ui .View = parent
9193 self ._options = [
9294 discord .SelectOption (
93- label = f"{ answer [:97 ]} ..." if len (answer ) > 100 else answer , value = str (num + 1 )
95+ label = f"{ answer [:97 ]} ..." if len (answer ) > 100 else answer ,
96+ value = str (num + 1 ),
9497 )
9598 for num , (_ , answer ) in enumerate (players .items ())
9699 ]
@@ -115,24 +118,29 @@ def __init__(
115118 self .cog : commands .Cog = cog
116119
117120 self .acronym : str = None
118- self .players : typing . Dict [discord .Member , str ] = {}
121+ self .players : dict [discord .Member , str ] = {}
119122 self .votes : Counter [int , int ] = Counter ()
120123
121124 self ._message : discord .Message = None
122- self ._voters : typing . Dict [discord .Member , int ] = {}
125+ self ._voters : dict [discord .Member , int ] = {}
123126 self ._mode : typing .Literal ["join" , "vote" ] = None
124127
125128 async def start (self , ctx : commands .Context ) -> discord .Message :
126129 self .ctx : commands .Context = ctx
127130 self .acronym : str = self .get_acronym ()
128131 embed : discord .Embed = discord .Embed (
129132 title = _ ("Acronym Game" ),
130- description = _ ("Click the button below to **join the party**! Please note that the maximum amount of players is **20**." ),
133+ description = _ (
134+ "Click the button below to **join the party**! Please note that the maximum amount of players is **20**." ,
135+ ),
131136 color = await self .ctx .embed_color (),
132137 )
133138 embed .add_field (name = _ ("Random Acronym:" ), value = self .acronym )
134139 end_time = int ((datetime .datetime .now () + datetime .timedelta (seconds = 120 )).timestamp ())
135- embed .add_field (name = _ ("End time for joining:" ), value = f"<t:{ end_time } :T> (<t:{ end_time } :R>)" )
140+ embed .add_field (
141+ name = _ ("End time for joining:" ),
142+ value = f"<t:{ end_time } :T> (<t:{ end_time } :R>)" ,
143+ )
136144 embed .set_author (
137145 name = _ ("Hosted by {host.display_name}" ).format (host = self .ctx .author ),
138146 icon_url = self .ctx .author .display_avatar ,
@@ -146,9 +154,7 @@ async def start(self, ctx: commands.Context) -> discord.Message:
146154 if len (self .players ) < 3 :
147155 await self .on_timeout ()
148156 self .stop ()
149- raise commands .UserFeedbackCheckFailure (
150- _ ("At least three players are needed to play!" )
151- )
157+ raise commands .UserFeedbackCheckFailure (_ ("At least three players are needed to play!" ))
152158 table = PrettyTable ()
153159 table .field_names = ["#" , "Answer" ]
154160 for num , (__ , answer ) in enumerate (self .players .items ()):
@@ -158,7 +164,7 @@ async def start(self, ctx: commands.Context) -> discord.Message:
158164 color = await self .ctx .embed_color (),
159165 )
160166 embed .description = _ (
161- "Use the dropdown below to vote for the best answer for the random acronym. All guild members can vote.\n "
167+ "Use the dropdown below to vote for the best answer for the random acronym. All guild members can vote.\n " ,
162168 ) + box (str (table ), lang = "py" )
163169 embed .add_field (name = "Random Acronym" , value = self .acronym )
164170 end_time = int ((datetime .datetime .now () + datetime .timedelta (seconds = 60 )).timestamp ())
@@ -195,20 +201,20 @@ async def start(self, ctx: commands.Context) -> discord.Message:
195201 ),
196202 answer ,
197203 self .votes [list (self .players ).index (player ) + 1 ],
198- ]
204+ ],
199205 )
200206 embed : discord .Embed = discord .Embed (
201- title = "Acronym Game" , color = await self .ctx .embed_color ()
202- )
203- embed .description = _ ("Here is the leaderboard for this game:" ) + box (
204- str (table ), lang = "py"
207+ title = "Acronym Game" ,
208+ color = await self .ctx .embed_color (),
205209 )
210+ embed .description = _ ("Here is the leaderboard for this game:" ) + box (str (table ), lang = "py" )
206211 embed .add_field (name = "Random Acronym" , value = self .acronym )
207212 self ._message : discord .Message = await self ._message .edit (embed = embed , view = self )
208213 await self ._message .reply (
209214 _ ("Winner{s}: {winners}!" ).format (
210- winners = humanize_list (winners ), s = "s" if len (winners ) > 1 else ""
211- )
215+ winners = humanize_list (winners ),
216+ s = "s" if len (winners ) > 1 else "" ,
217+ ),
212218 )
213219 return self ._message
214220
@@ -228,32 +234,39 @@ def get_acronym(self) -> str:
228234 return "" .join (
229235 [
230236 random .choice (string .ascii_uppercase ) for __ in range (4 )
231- ] # range(random.choice(range(4, 5)))
237+ ], # range(random.choice(range(4, 5)))
232238 )
233239
234240 @discord .ui .button (label = "Join Game" , emoji = "🎮" , style = discord .ButtonStyle .success )
235241 async def join_button (
236- self , interaction : discord .Interaction , button : discord .ui .Button
242+ self ,
243+ interaction : discord .Interaction ,
244+ button : discord .ui .Button ,
237245 ) -> None :
238246 modal = JoinGameModal (self )
239247 await interaction .response .send_modal (modal )
240248
241249 async def _callback (
242- self , interaction : discord .Interaction , option : discord .SelectOption
250+ self ,
251+ interaction : discord .Interaction ,
252+ option : discord .SelectOption ,
243253 ) -> None :
244254 if interaction .user in self ._voters :
245255 await interaction .response .send_message (
246- _ ("You have already voted in this game." ), ephemeral = True
256+ _ ("You have already voted in this game." ),
257+ ephemeral = True ,
247258 )
248259 return
249260 value = int (option .value )
250261 if interaction .user == list (self .players )[value - 1 ]:
251262 await interaction .response .send_message (
252- _ ("You can't vote for yourself." ), ephemeral = True
263+ _ ("You can't vote for yourself." ),
264+ ephemeral = True ,
253265 )
254266 return
255267 self ._voters [interaction .user ] = value
256268 self .votes [value ] += 1
257269 await interaction .response .send_message (
258- _ ("Vote given for answer {num}." ).format (num = value ), ephemeral = True
270+ _ ("Vote given for answer {num}." ).format (num = value ),
271+ ephemeral = True ,
259272 )
0 commit comments