def team_from_message(command): newteam = games.team() roster = command.split("\n", 1)[1].split("\n") newteam.name = roster[0] #first line is team name newteam.slogan = roster[1] #second line is slogan for rosternum in range(2, len(roster) - 1): if roster[rosternum] != "": if len(roster[rosternum]) > 70: raise CommandError( f"{roster[rosternum]} is too long, chief. 70 or less.") newteam.add_lineup( games.player(ono.get_stats(roster[rosternum].rstrip()))) if len(roster[len(roster) - 1]) > 70: raise CommandError( f"{roster[len(roster)-1]} is too long, chief. 70 or less.") newteam.set_pitcher( games.player(ono.get_stats( roster[len(roster) - 1].rstrip()))) #last line is pitcher name if len(newteam.name) > 30: raise CommandError( "Team names have to be less than 30 characters! Try again.") elif len(newteam.slogan) > 100: raise CommandError( "We've given you 100 characters for the slogan. Discord puts limits on us and thus, we put limits on you. C'est la vie." ) return newteam
def team_from_collection(newteam_json): # verify collection against our own restrictions if len(newteam_json["fullName"]) > 30: raise CommandError( "Team names have to be less than 30 characters! Try again.") if len(newteam_json["slogan"]) > 100: raise CommandError( "We've given you 100 characters for the slogan. Discord puts limits on us and thus, we put limits on you. C'est la vie." ) if len(newteam_json["lineup"]) > 20: raise CommandError( "20 players in the lineup, maximum. We're being really generous here." ) if not len(newteam_json["rotation"]) == 1: raise CommandError("One and only one pitcher per team, thanks.") for player in newteam_json["lineup"] + newteam_json["rotation"]: if len(player["name"]) > 70: raise CommandError( f"{player['name']} is too long, chief. 70 or less.") #actually build the team newteam = games.team() newteam.name = newteam_json["fullName"] newteam.slogan = newteam_json["slogan"] for player in newteam_json["lineup"]: newteam.add_lineup(games.player(json.dumps(player))) newteam.set_pitcher(games.player(json.dumps(newteam_json["rotation"][0]))) return newteam
async def on_reaction_add(reaction, user): if reaction.message in setupmessages.keys(): game = setupmessages[reaction.message] try: if str(reaction.emoji) == "🔼" and not user == client.user: new_player = games.player(ono.get_stats(db.get_user_player(user)["name"])) game.teams["away"].add_lineup(new_player) await reaction.message.channel.send(f"{new_player} {new_player.star_string('batting_stars')} takes spot #{len(game.teams['away'].lineup)} on the away lineup.") elif str(reaction.emoji) == "🔽" and not user == client.user: new_player = games.player(ono.get_stats(db.get_user_player(user)["name"])) game.teams["home"].add_lineup(new_player) await reaction.message.channel.send(f"{new_player} {new_player.star_string('batting_stars')} takes spot #{len(game.teams['home'].lineup)} on the home lineup.") except: await reaction.message.channel.send(f"{user.display_name}, we can't find your idol. Maybe you don't have one yet?")
def draft_player(self, handle, player_name): """ `handle` is the participant's discord handle. """ if self._active_participant.handle != handle: raise ValueError( f'{self._active_participant.handle} is drafting, not you') player_name = player_name.strip() player = self._players.get(player_name) if not player: # might be some whitespace shenanigans for name, stats in self._players.items(): if name.replace('\xa0', ' ').strip().lower() == player_name.lower(): player = stats break else: # still not found raise ValueError(f'Player `{player_name}` not in draft list') del self._players[player['name']] if len(self._players) <= REFRESH_DRAFT_SIZE: self.refresh_players() if self._round < DRAFT_ROUNDS: self._active_participant.team.add_lineup( games.player(json.dumps(player))) elif self._round == DRAFT_ROUNDS: self._active_participant.team.add_pitcher( games.player(json.dumps(player))) self.advance_draft() if self._active_participant == BOOKMARK: self.advance_draft() return player
async def save_team_batch(message, command): newteam = games.team() #try: roster = command.split("\n", 1)[1].split("\n") newteam.name = roster[0] #first line is team name newteam.slogan = roster[1] #second line is slogan for rosternum in range(2, len(roster) - 1): if roster[rosternum] != "": if len(roster[rosternum]) > 70: await channel.send( f"{roster[rosternum]} is too long, chief. 70 or less.") return newteam.add_lineup( games.player(ono.get_stats(roster[rosternum].rstrip()))) if len(roster[len(roster) - 1]) > 70: await channel.send( f"{roster[len(roster)-1]} is too long, chief. 70 or less.") return newteam.set_pitcher( games.player(ono.get_stats( roster[len(roster) - 1].rstrip()))) #last line is pitcher name if len(newteam.name) > 30: await message.channel.send( "Team names have to be less than 30 characters! Try again.") return elif len(newteam.slogan) > 100: await message.channel.send( "We've given you 100 characters for the slogan. Discord puts limits on us and thus, we put limits on you. C'est la vie." ) return await message.channel.send(embed=build_team_embed(newteam)) checkmsg = await message.channel.send("Does this look good to you, boss?") await checkmsg.add_reaction("👍") await checkmsg.add_reaction("👎") def react_check(react, user): return user == message.author and react.message == checkmsg try: react, user = await client.wait_for('reaction_add', timeout=20.0, check=react_check) if react.emoji == "👍": await message.channel.send("You got it, chief. Saving now.") games.save_team(newteam, message.author.id) await message.channel.send( "Saved! Thank you for flying Air Matteo. We hope you had a pleasant data entry." ) return elif react.emoji == "👎": await message.channel.send( "Message received. Pumping brakes, turning this car around. Try again, chief." ) return except asyncio.TimeoutError: await message.channel.send( "Look, I don't have all day. 20 seconds is long enough, right? Try again." ) return
async def setup_game(channel, owner, newgame): newgame.owner = owner await channel.send( f"Game sucessfully created!\nStart any commands for this game with `{newgame.name}` so I know who's talking about what." ) await asyncio.sleep(1) await channel.send("Who's pitching for the away team?") def input(msg): return msg.content.startswith( newgame.name ) and msg.channel == channel #if author or willing participant and in correct channel while newgame.teams["home"].pitcher == None: def nameinput(msg): return msg.content.startswith( newgame.name ) and msg.channel == channel #if author or willing participant and in correct channel while newgame.teams["away"].pitcher == None: try: namemsg = await client.wait_for('message', check=input) new_pitcher_name = discord.utils.escape_mentions( namemsg.content.split(f"{newgame.name} ")[1]) if len(new_pitcher_name) > 70: await channel.send( "That player name is too long, chief. 70 or less.") else: new_pitcher = games.player(ono.get_stats(new_pitcher_name)) newgame.teams["away"].set_pitcher(new_pitcher) await channel.send( f"{new_pitcher} {new_pitcher.star_string('pitching_stars')}, pitching for the away team!\nNow, the home team's pitcher. Same dance, folks." ) except NameError: await channel.send("Uh.") try: namemsg = await client.wait_for('message', check=input) new_pitcher_name = discord.utils.escape_mentions( namemsg.content.split(f"{newgame.name} ")[1]) if len(new_pitcher_name) > 70: await channel.send( "That player name is too long, chief. 70 or less.") else: new_pitcher = games.player(ono.get_stats(new_pitcher_name)) newgame.teams["home"].set_pitcher(new_pitcher) await channel.send( f"And {new_pitcher} {new_pitcher.star_string('pitching_stars')}, pitching for the home team." ) except: await channel.send("Uh.") #pitchers assigned! team_join_message = await channel.send( f"""Now, the lineups! I need somewhere between 1 and 12 batters. Cloning helps a lot with this sort of thing. React to this message with 🔼 to have your idol join the away team, or 🔽 to have them join the home team. You can also enter names like you did for the pitchers, with a slight difference: `away [name]` or `home [name]` instead of just the name. Creator, type `{newgame.name} done` to finalize lineups.""") await team_join_message.add_reaction("🔼") await team_join_message.add_reaction("🔽") setupmessages[team_join_message] = newgame #emoji_task = asyncio.create_task(watch_for_reacts(team_join_message, ready, newgame)) #msg_task = asyncio.create_task(watch_for_messages(channel, ready, newgame)) #await asyncio.gather( # watch_for_reacts(team_join_message, newgame), # watch_for_messages(channel, newgame) # ) def messagecheck(msg): return (msg.content.startswith(newgame.name) ) and msg.channel == channel and msg.author != client.user while not newgame.ready: try: msg = await client.wait_for('message', timeout=120.0, check=messagecheck) except asyncio.TimeoutError: await channel.send( "Game timed out. 120 seconds between players is a bit much, see?" ) del setupmessages[team_join_message] del newgame return new_player = None if msg.author == newgame.owner and msg.content == f"{newgame.name} done": if newgame.teams['home'].finalize( ) and newgame.teams['away'].finalize(): newgame.ready = True break else: side = None if msg.content.split(f"{newgame.name} ")[1].split(" ", 1)[0] == "home": side = "home" elif msg.content.split(f"{newgame.name} ")[1].split( " ", 1)[0] == "away": side = "away" if side is not None: new_player_name = discord.utils.escape_mentions( msg.content.split(f"{newgame.name} ")[1].split(" ", 1)[1]) if len(new_player_name) > 70: await channel.send( "That player name is too long, chief. 70 or less.") else: new_player = games.player(ono.get_stats(new_player_name)) try: if new_player is not None: newgame.teams[side].add_lineup(new_player) await channel.send( f"{new_player} {new_player.star_string('batting_stars')} takes spot #{len(newgame.teams[side].lineup)} on the {side} lineup." ) except: True del setupmessages[team_join_message] #cleanup! await channel.send("Name the away team, creator.") def ownercheck(msg): return msg.author == newgame.owner while newgame.teams["home"].name == None: while newgame.teams["away"].name == None: newname = await client.wait_for('message', check=ownercheck) if len(newname.content) < 30: newgame.teams['away'].name = newname.content await channel.send( f"Stepping onto the field, the visitors: {newname.content}!\nFinally, the home team, and we can begin." ) else: await channel.send( "Hey, keep these to 30 characters or less please. Discord messages have to stay short." ) newname = await client.wait_for('message', check=ownercheck) if len(newname.content) < 30: newgame.teams['home'].name = newname.content await channel.send( f"Next on the diamond, your home team: {newname.content}!") else: await channel.send( "Hey, keep these to 30 characters or less please. Discord messages have to stay short." ) await asyncio.sleep(3) await channel.send( f"**{newgame.teams['away'].name} at {newgame.teams['home'].name}**") game_task = asyncio.create_task(watch_game(channel, newgame)) await game_task