async def pass_the_word(ctx, *args): global pass_the_word_game, join_message if len(args) == 0: join_message = await ctx.send(embed=get_embed( "React to this message with any emoji to join the game.\n\nStart the game with `::pass_the_word !`.", discord.Colour.green())) return if len(args) == 1 and args[0] == "!" and join_message != None: try: pass_the_word_game = PassTheWord() await pass_the_word_game.create_from_reactions(ctx, join_message) except AssertionError as e: await ctx.send(embed=get_embed(str(e), discord.Colour.red())) except Exception as e: print(e) await ctx.send(embed=get_embed("Let Lucas know he's f****d up.", discord.Colour.red())) else: try: pass_the_word_game = PassTheWord() await pass_the_word_game.create_from_mentions(ctx, args) except AssertionError as e: await ctx.send(embed=get_embed(str(e), discord.Colour.red())) except Exception as e: print(e) await ctx.send(embed=get_embed("Let Lucas know he's f****d up.", discord.Colour.red()))
async def create(self, ctx, join_message): self = TopTrumps() self.channel = ctx.channel # Get the players players = set() refreshed_join_message = await ctx.channel.fetch_message( join_message.id) for reaction in refreshed_join_message.reactions: [players.add(user) async for user in reaction.users()] self.players = list(players) #if len(self.players) < 2: # raise AssertionError("You need at least two players.") self.challenger = randint(0, len(self.players) - 1) self.alive = [True] * len(self.players) # Load and distribute cards with open('./resources/cards.json') as file: cards = json.load(file)['cards'] shuffle(cards) #cards_pp = floor(len(cards)/len(self.players)) cards_pp = 3 challenger = self.players[self.challenger] embed_next_turn = discord.Embed( colour=discord.Colour.gold()).set_footer( text=f"It's {challenger.name}'s turn.", icon_url=challenger.avatar_url) embed_your_turn = get_embed( "**It's your turn.** Choose a category and read out the scores. Use `::continue <category>` to exchange cards.", colour=discord.Colour.green()) #self.decks = [] for i in range(0, len(self.players)): k = i * cards_pp deck = deque(cards[k:k + cards_pp]) self.decks.append(deck) # Send each player's top card embed_top_card = discord.Embed( description= f"You have {len(deck[i])} cards. This is your top card:", colour=discord.Colour.gold(), ).set_image(url=deck[0]["url"]) await self.players[i].send(embed=embed_top_card) await self.players[i].send(embed=embed_your_turn if i == self.challenger else embed_next_turn) await self.channel.send(embed=get_embed( f"The game is ready. Check your DMs and reply to HamBot there.")) return self
async def continue_game(self, ctx, category): if category.lower() not in [ 'vintage', 'quality', 'cringe', 'salt', 'endurance' ]: await self.channel.send(embed=get_embed( f"Right. **{ctx.author.name}**, what the f**k is '*{category}*'? Engage your brain and make it either:\n *vintage*, *quality*, *cringe*, *salt*, or *endurance*." )) return # Choose the winner top_cards = [ cards[0] for i, cards in enumerate(self.decks) if self.alive[i] ] scores = [top_card[category.lower()] for top_card in top_cards] winner_index = argmax(scores) winning_player = self.players[winner_index] # Add cards to the winner's deck self.decks[winner_index].extend(top_cards) # Remove everyone's top card just_died = [] for i in range(len(self.players)): if not self.alive[i]: return self.decks[i].popleft() if len(self.decks[i]) == 0: self.alive[i] = False just_died.append(self.players[i].name) # Check if there is a winner if self.alive.count(True) == 1: embed_game_over = get_embed( f"The game is over. **{winning_player.name}** is the winner.") [ await player.send(embed=embed_game_over) for player in self.players ] await self.channel.send(embed=embed_game_over) return True # Get the next player self.challenger = winner_index # Message the players embed_end_of_round = discord.Embed( description= f"**{winning_player.name}** won with a *{category.lower()}* score of {scores[winner_index]}.\n\n**{', '.join(just_died)}** has run out of cards and is out.", colour=discord.Colour.gold()).set_image( url=top_cards[winner_index]["url"]) await self.message_players(embed_end_of_round) return False
async def top_trumps(ctx, *args): global trump_game, trump_join_message if len(args) == 0: trump_join_message = await ctx.send(embed=get_embed( "React to this message with any emoji to join the game.\n\nStart the game with `::top_trumps !`.", discord.Colour.green())) return if len(args) == 1 and args[0] == "!" and trump_join_message != None: try: trump_game = await TopTrumps.create(ctx, trump_join_message) except AssertionError as e: await ctx.send(embed=get_embed(str(e), discord.Colour.red())) except Exception as e: print(e) await ctx.send(embed=get_embed("Let Lucas know he's f****d up.", discord.Colour.red()))
async def message_players(self, embed_end_of_round): challenger = self.players[self.challenger] embed_next_turn = discord.Embed( colour=discord.Colour.gold()).set_footer( text=f"It's {challenger.name}'s turn.", icon_url=challenger.avatar_url) embed_your_turn = get_embed( "**It's your turn.** Choose a category and read out the scores. Use `::continue <category>` to exchange cards.", colour=discord.Colour.green()) for i in range(len(self.players)): player = self.players[i] # Message all players, alive or dead, who won the round. await player.send(embed=embed_end_of_round) # Send alive players their new card if not self.alive[i]: return embed_top_card = discord.Embed( description= f":hamster: You have {len(self.decks[i])} cards. This is your top card:", colour=discord.Colour.gold(), ).set_image(url=self.decks[i][0]["url"]) await player.send(embed=embed_top_card) await player.send(embed=embed_your_turn if i == self.challenger else embed_next_turn)
async def random_kick(ctx): members_in_voice = [] for voice_channel in ctx.guild.voice_channels: members_in_voice.extend(voice_channel.members) if len(members_in_voice) > 0: unlucky_member = random.choice(members_in_voice) await ctx.send(embed=get_embed(f"Unlucky **{unlucky_member.name}**.")) await unlucky_member.edit(voice_channel=None)
async def continue_trump(ctx, category): global trump_game if trump_game == None: await ctx.send(embed=get_embed( "Oh dumbass, there's no *Top Trumps* game going on.")) return isGameOver = await trump_game.continue_game(ctx, category) if isGameOver: trump_game = None
async def received_new_word(self, ctx, word): # Ensure the word is from someone in the game if self.game_over: return if not any(player.id == ctx.author.id for player in self.players): await ctx.send(embed=get_embed("You're not apart of the game.")) return current_player = self.players[self.current_player_index] # Ensure the word is from the current player if ctx.author.id != current_player.id: await ctx.send(embed=get_embed("It's not your turn.")) return # End the game if necessary if word == "!": await ctx.message.add_reaction(self.emojis['thumbsup']) await self.channel.send(embed=get_embed( f"Here's the final story:```{' '.join(self.chain)}```")) for player in self.players: await player.send(embed=get_embed( f"Check out the final story in **{self.channel.name}** in **{self.channel.guild.name}**" )) self.game_over = True return # Ensure the word is legitimate if not match(self.message_regex, word): await ctx.message.add_reaction(self.emojis['thumbsdown']) return # Update current player and message them self.chain.append(word) self.update_current_player() await self.message_current_player() await ctx.message.add_reaction(self.emojis['thumbsup'])
async def create_from_reactions(self, ctx, join_message): player_ids = set() new_join_message = await ctx.channel.fetch_message(join_message.id) for reaction in new_join_message.reactions: async for user in reaction.users(): player_ids.add(user.id) await new_join_message.clear_reactions() await new_join_message.edit(embed=get_embed( "React to this message with any emoji to join the game.\n\nStart the game with `::pass_the_word !`.", colour=discord.Colour.from_rgb(47, 49, 54))) if len(player_ids) < 2: raise AssertionError("You need at least two players.") await self.create(ctx, list(player_ids)) return self
async def create(self, ctx, player_ids): self.players = [] for player_id in player_ids: player = discord.utils.find(lambda m: m.id == player_id, ctx.guild.members) self.players.append(player) self.num_players = len(self.players) self.num_candidates = self.num_players - 1 self.chain = [choice(self.starters)] self.current_player_index = randint(0, self.num_players - 1) self.channel = ctx.channel for p in self.players: print(p.name) await self.message_current_player(self) await self.channel.send(embed=get_embed( "Let the chaos ensue. When the chain is long enough, it will be posted here." )) return self
async def message_current_player(self): current_player = self.players[self.current_player_index] current_prompt = ' '.join(self.chain[-2:]) await current_player.send(embed=get_embed( f"It's your turn. What word should come next in the story after:\n```{current_prompt}```\nReply with `::chain <word>` or end the story with `::chain !`." ))