2021-01-02 08:05:54 +00:00
import discord , json , math , os , roman , games , asyncio , random , main_controller , threading , time , leagues , urllib
2020-12-20 00:08:09 +00:00
import database as db
2020-12-20 01:26:05 +00:00
import onomancer as ono
2020-12-28 08:21:47 +00:00
from flask import Flask
2020-12-20 00:08:09 +00:00
2020-12-27 08:42:09 +00:00
class Command :
def isauthorized ( self , user ) :
return True
2020-12-20 00:08:09 +00:00
2020-12-27 08:42:09 +00:00
async def execute ( self , msg , command ) :
2020-12-22 20:02:28 +00:00
return
2020-12-28 09:09:02 +00:00
class CommandError ( Exception ) :
pass
2020-12-27 08:42:09 +00:00
class IntroduceCommand ( Command ) :
name = " introduce "
template = " "
description = " "
2020-12-22 20:02:28 +00:00
2020-12-27 08:42:09 +00:00
def isauthorized ( self , user ) :
return user . id in config ( ) [ " owners " ]
2020-12-20 01:26:05 +00:00
2020-12-27 08:42:09 +00:00
async def execute ( self , msg , command ) :
text = """ **Your name, favorite team, and pronouns**: Matteo Prestige, CHST, they/them ***only.*** There ' s more than one of us up here, after all.
* * What are you majoring in ( wrong answers only ) * * : Economics .
* * Your favorite and least favorite beverage , without specifying which * * : Vanilla milkshakes , chocolate milkshakes .
* * Favorite non - Mild Low team * * : The Mills . We hope they ' re treating Ren alright.
* * If you were a current blaseball player , who would you be * * : We refuse to answer this question .
* * Your hobbies / interests * * : Minigolf , blaseball , felony insider trading .
Our avatar was graciously provided to us , with permission , by @HetreaSky on Twitter .
"""
await msg . channel . send ( text )
2020-12-20 05:13:23 +00:00
2020-12-27 09:03:41 +00:00
class CountActiveGamesCommand ( Command ) :
name = " countactivegames "
template = " "
description = " "
2020-12-22 20:02:28 +00:00
2020-12-27 09:03:41 +00:00
def isauthorized ( self , user ) :
return user . id in config ( ) [ " owners " ]
2020-12-20 01:26:05 +00:00
2020-12-27 09:03:41 +00:00
async def execute ( self , msg , command ) :
await msg . channel . send ( f " There ' s { len ( gamesarray ) } active games right now, boss. " )
2020-12-20 05:13:23 +00:00
2020-12-27 08:42:09 +00:00
class RomanCommand ( Command ) :
name = " roman "
template = " m;roman [number] "
description = " Converts any natural number less than 4,000,000 into roman numerals. This one is just for fun. "
2020-12-20 05:13:23 +00:00
2020-12-27 08:42:09 +00:00
async def execute ( self , msg , command ) :
2020-12-20 10:28:47 +00:00
try :
2020-12-27 08:42:09 +00:00
await msg . channel . send ( roman . roman_convert ( command ) )
2020-12-20 10:28:47 +00:00
except ValueError :
2020-12-27 08:42:09 +00:00
await msg . channel . send ( f " \" { command } \" isn ' t an integer in Arabic numerals. " )
class IdolizeCommand ( Command ) :
name = " idolize "
template = " m;idolize [name] "
2020-12-28 16:05:19 +00:00
description = " Records any name as your idol, mostly for fun. There ' s a limit of 70 characters. That should be *plenty*. "
2020-12-20 10:28:47 +00:00
2020-12-27 08:42:09 +00:00
async def execute ( self , msg , command ) :
if ( command . startswith ( " meme " ) ) :
2020-12-20 12:25:25 +00:00
meme = True
2020-12-27 08:42:09 +00:00
command = command . split ( " " , 1 ) [ 1 ]
2020-12-20 12:25:25 +00:00
else :
meme = False
2020-12-27 08:42:09 +00:00
player_name = discord . utils . escape_mentions ( command )
2020-12-22 20:15:18 +00:00
if len ( player_name ) > = 70 :
await msg . channel . send ( " That name is too long. Please keep it below 70 characters, for my sake and yours. " )
return
2020-12-23 23:02:55 +00:00
try :
player_json = ono . get_stats ( player_name )
db . designate_player ( msg . author , json . loads ( player_json ) )
if not meme :
await msg . channel . send ( f " { player_name } is now your idol. " )
else :
await msg . channel . send ( f " { player_name } is now { msg . author . display_name } ' s idol. " )
await msg . channel . send ( f " Reply if { player_name } is your idol also. " )
except :
2020-12-27 01:28:36 +00:00
await msg . channel . send ( " Something went wrong. Tell xvi. " )
2020-12-20 12:25:25 +00:00
2020-12-27 08:42:09 +00:00
class ShowIdolCommand ( Command ) :
name = " showidol "
template = " m;showidol "
description = " Displays your idol ' s name and stars in a nice discord embed. "
async def execute ( self , msg , command ) :
2020-12-20 12:25:25 +00:00
try :
player_json = db . get_user_player ( msg . author )
embed = build_star_embed ( player_json )
embed . set_footer ( text = msg . author . display_name )
await msg . channel . send ( embed = embed )
except :
await msg . channel . send ( " We can ' t find your idol. Looked everywhere, too. " )
2020-12-27 08:42:09 +00:00
class ShowPlayerCommand ( Command ) :
name = " showplayer "
template = " m;showplayer [name] "
2020-12-28 16:05:19 +00:00
description = " Displays any name ' s stars in a nice discord embed, there ' s a limit of 70 characters. That should be *plenty*. Note: if you want to lookup a lot of different players you can do it on onomancer instead of spamming this command a bunch and clogging up discord: https://onomancer.sibr.dev/reflect "
2020-12-27 08:42:09 +00:00
async def execute ( self , msg , command ) :
2020-12-24 09:51:38 +00:00
player_name = json . loads ( ono . get_stats ( command . split ( " " , 1 ) [ 1 ] ) )
await msg . channel . send ( embed = build_star_embed ( player_name ) )
2020-12-27 08:42:09 +00:00
class StartGameCommand ( Command ) :
name = " startgame "
template = " m;startgame [away] [home] [innings] "
2020-12-28 16:05:19 +00:00
description = """ Starts a game with premade teams made using saveteam, use this command at the top of a list followed by each of these in a new line (shift+enter in discord, or copy+paste from notepad):
- the away team ' s name.
- the home team ' s name.
- and finally , optionally , the number of innings , which must be greater than 2 and less than 31. if not included it will default to 9. """
2020-12-24 09:51:38 +00:00
2020-12-27 08:42:09 +00:00
async def execute ( self , msg , command ) :
2020-12-31 19:09:50 +00:00
league = None
2020-12-27 05:52:34 +00:00
if config ( ) [ " game_freeze " ] :
2020-12-27 01:02:39 +00:00
await msg . channel . send ( " Patch incoming. We ' re not allowing new games right now. " )
return
2020-12-26 05:17:49 +00:00
2020-12-31 19:09:50 +00:00
if " -l " in command . split ( " \n " ) [ 0 ] :
league = command . split ( " \n " ) [ 0 ] . split ( " -l " ) [ 1 ]
elif " --league " in command . split ( " \n " ) [ 0 ] :
league = command . split ( " \n " ) [ 0 ] . split ( " --league " ) [ 1 ]
2021-01-01 01:19:57 +00:00
innings = None
2020-12-24 10:18:29 +00:00
try :
2020-12-31 19:04:55 +00:00
team_name1 = command . split ( " \n " ) [ 1 ] . strip ( )
team1 = games . get_team ( team_name1 )
if team1 is None :
teams = games . search_team ( team_name1 . lower ( ) )
if len ( teams ) == 1 :
team1 = teams [ 0 ]
team_name2 = command . split ( " \n " ) [ 2 ] . strip ( )
team2 = games . get_team ( team_name2 )
if team2 is None :
teams = games . search_team ( team_name2 . lower ( ) )
if len ( teams ) == 1 :
team2 = teams [ 0 ]
2020-12-24 10:18:29 +00:00
innings = int ( command . split ( " \n " ) [ 3 ] )
except IndexError :
2020-12-27 19:36:25 +00:00
try :
2020-12-31 19:04:55 +00:00
team_name1 = command . split ( " \n " ) [ 1 ] . strip ( )
team1 = games . get_team ( team_name1 )
if team1 is None :
teams = games . search_team ( team_name1 . lower ( ) )
if len ( teams ) == 1 :
team1 = teams [ 0 ]
team_name2 = command . split ( " \n " ) [ 2 ] . strip ( )
team2 = games . get_team ( team_name2 )
if team2 is None :
teams = games . search_team ( team_name2 . lower ( ) )
if len ( teams ) == 1 :
team2 = teams [ 0 ]
2020-12-27 19:36:25 +00:00
except IndexError :
await msg . channel . send ( " We need at least three lines: startgame, away team, and home team are required. Optionally, the number of innings can go at the end, if you want a change of pace. " )
2020-12-27 19:47:32 +00:00
return
2020-12-24 10:18:29 +00:00
except :
2020-12-27 01:02:39 +00:00
await msg . channel . send ( " Something about that command tripped us up. Either we couldn ' t find a team, or you gave us a bad number of innings. " )
2020-12-26 05:27:14 +00:00
return
2020-12-27 19:40:40 +00:00
if innings is not None and innings < 2 :
2020-12-26 05:27:14 +00:00
await msg . channel . send ( " Anything less than 2 innings isn ' t even an outing. Try again. " )
return
2020-12-27 01:27:03 +00:00
2020-12-27 19:40:40 +00:00
elif innings is not None and innings > 30 and msg . author . id not in config ( ) [ " owners " ] :
2020-12-27 01:02:39 +00:00
await msg . channel . send ( " Y ' all can ' t behave, so we ' ve limited games to 30 innings. Ask xvi to start it with more if you really want to. " )
2020-12-27 01:56:10 +00:00
return
2020-12-24 10:18:29 +00:00
if team1 is not None and team2 is not None :
2021-01-03 11:06:51 +00:00
game = games . game ( team1 . finalize ( ) , team2 . finalize ( ) , length = innings )
2020-12-27 03:53:32 +00:00
channel = msg . channel
await msg . delete ( )
2020-12-27 05:52:34 +00:00
2020-12-31 19:09:50 +00:00
game_task = asyncio . create_task ( watch_game ( channel , game , user = msg . author , league = league ) )
2020-12-24 10:18:29 +00:00
await game_task
2020-12-31 19:13:36 +00:00
else :
2020-12-31 19:19:54 +00:00
await msg . channel . send ( " We can ' t find one or both of those teams. Check your staging, chief. " )
2020-12-31 19:13:36 +00:00
return
2020-12-22 06:56:33 +00:00
2020-12-27 08:42:09 +00:00
class SaveTeamCommand ( Command ) :
name = " saveteam "
2021-01-02 06:10:52 +00:00
template = """ m;saveteam
[ name ]
[ slogan ]
[ lineup ]
[ rotation ] """
2020-12-28 16:05:19 +00:00
description = """ Saves a team to the database allowing it to be used for games. Send this command at the top of a list, with entries separated by new lines (shift+enter in discord, or copy+paste from notepad).
- the first line of the list is your team ' s name (cannot contain emoji).
- the second line is your team ' s icon and slogan, this should begin with an emoji followed by a space, followed by a short slogan.
2021-01-02 06:10:52 +00:00
- the third line must be blank .
2020-12-28 16:05:19 +00:00
- the next lines are your batters ' names in the order you want them to appear in your lineup, lineups can contain any number of batters between 1 and 12.
2021-01-02 06:10:52 +00:00
- there must be another blank line between your batters and your pitchers .
- the final lines are your pitchers ' names.
2020-12-28 16:05:19 +00:00
if you did it correctly , you ' ll get a team embed with a prompt to confirm. hit the 👍 and it ' ll be saved . """
2020-12-27 08:42:09 +00:00
async def execute ( self , msg , command ) :
2020-12-28 09:09:02 +00:00
if db . get_team ( command . split ( ' \n ' , 1 ) [ 1 ] . split ( " \n " ) [ 0 ] ) == None :
2020-12-29 21:10:57 +00:00
await msg . channel . send ( f " Fetching players... " )
2020-12-28 09:09:02 +00:00
team = team_from_message ( command )
save_task = asyncio . create_task ( save_team_confirm ( msg , team ) )
2020-12-26 09:46:42 +00:00
await save_task
else :
name = command . split ( ' \n ' , 1 ) [ 1 ] . split ( ' \n ' ) [ 0 ]
await msg . channel . send ( f " { name } already exists. Try a new name, maybe? " )
2020-12-22 06:56:33 +00:00
2020-12-28 09:09:02 +00:00
class ImportCommand ( Command ) :
name = " import "
template = " m;import [onomancer collection URL] "
description = " Imports an onomancer collection as a new team. You can use the new onomancer simsim setting to ensure compatibility. "
async def execute ( self , msg , command ) :
team_raw = ono . get_collection ( command . strip ( ) )
if not team_raw == None :
team_json = json . loads ( team_raw )
if db . get_team ( team_json [ " fullName " ] ) == None :
team = team_from_collection ( team_json )
await asyncio . create_task ( save_team_confirm ( msg , team ) )
else :
await msg . channel . send ( f " { team_json [ ' fullName ' ] } already exists. Try a new name, maybe? " )
else :
await msg . channel . send ( " Something went pear-shaped while we were looking for that collection. You certain it ' s a valid onomancer URL? " )
2020-12-27 08:42:09 +00:00
class ShowTeamCommand ( Command ) :
name = " showteam "
template = " m;showteam [name] "
2020-12-28 16:05:19 +00:00
description = " Shows information about any saved team. "
2020-12-27 08:42:09 +00:00
async def execute ( self , msg , command ) :
2020-12-27 09:32:19 +00:00
team_name = command . strip ( )
team = games . get_team ( team_name )
2020-12-24 09:51:38 +00:00
if team is not None :
await msg . channel . send ( embed = build_team_embed ( team ) )
else :
2020-12-27 09:32:19 +00:00
teams = games . search_team ( team_name . lower ( ) )
if len ( teams ) == 1 :
await msg . channel . send ( embed = build_team_embed ( teams [ 0 ] ) )
else :
await msg . channel . send ( " Can ' t find that team, boss. Typo? " )
2020-12-21 09:46:12 +00:00
2020-12-27 08:42:09 +00:00
class ShowAllTeamsCommand ( Command ) :
name = " showallteams "
template = " m;showallteams "
2020-12-28 16:05:19 +00:00
description = " Shows a paginated list of all teams available for games which can be scrolled through. "
2020-12-27 08:42:09 +00:00
async def execute ( self , msg , command ) :
2020-12-26 09:46:42 +00:00
list_task = asyncio . create_task ( team_pages ( msg , games . get_all_teams ( ) ) )
await list_task
2020-12-27 08:42:09 +00:00
class SearchTeamsCommand ( Command ) :
name = " searchteams "
template = " m;searchteams [searchterm] "
2020-12-28 16:05:19 +00:00
description = " Shows a paginated list of all teams whose names contain the given search term. "
2020-12-27 08:42:09 +00:00
async def execute ( self , msg , command ) :
search_term = command . strip ( )
2020-12-26 10:09:49 +00:00
if len ( search_term ) > 30 :
await msg . channel . send ( " Team names can ' t even be that long, chief. Try something shorter. " )
return
list_task = asyncio . create_task ( team_pages ( msg , games . search_team ( search_term ) , search_term = search_term ) )
2020-12-26 09:46:42 +00:00
await list_task
2020-12-27 08:42:09 +00:00
class CreditCommand ( Command ) :
name = " credit "
template = " m;credit "
description = " Shows artist credit for matteo ' s avatar. "
async def execute ( self , msg , command ) :
2020-12-20 05:17:05 +00:00
await msg . channel . send ( " Our avatar was graciously provided to us, with permission, by @HetreaSky on Twitter. " )
2020-12-20 05:13:23 +00:00
2021-01-02 06:10:52 +00:00
class SwapPlayerCommand ( Command ) :
2021-01-02 09:02:57 +00:00
name = " swapsection "
template = """ m;swapsection
2021-01-02 06:10:52 +00:00
[ team name ]
[ player name ] """
description = " Swaps a player from lineup to rotation, or from rotation to lineup. Requires team ownership. "
async def execute ( self , msg , command ) :
team_name = command . split ( " \n " ) [ 1 ] . strip ( )
player_name = command . split ( " \n " ) [ 2 ] . strip ( )
team , owner_id = games . get_team_and_owner ( team_name )
if team is None :
await msg . channel . send ( " Can ' t find that team, boss. Typo? " )
return
2021-01-02 09:02:57 +00:00
elif owner_id != msg . author . id and msg . author . id not in config ( ) [ " owners " ] :
2021-01-02 06:10:52 +00:00
await msg . channel . send ( " You ' re not authorized to mess with this team. Sorry, boss. " )
return
elif not team . swap_player ( player_name ) :
2021-01-02 09:02:57 +00:00
await msg . channel . send ( " Either we can ' t find that player, you ' ve got no space on the other side, or they ' re your last member of that side of the roster. Can ' t field an empty lineup, and we *do* have rules, chief. " )
2021-01-02 06:10:52 +00:00
return
else :
await msg . channel . send ( embed = build_team_embed ( team ) )
games . update_team ( team )
2021-01-02 09:02:57 +00:00
await msg . channel . send ( " Paperwork signed, stamped, copied, and faxed up to the goddess. Xie ' s pretty quick with this stuff. " )
class MovePlayerCommand ( Command ) :
name = " move "
template = """ m;move
[ team name ]
[ player name ]
[ new lineup / rotation position number ] ( indexed with 1 being the top ) """
description = " Moves a player in your lineup or rotation. Requires team ownership. "
async def execute ( self , msg , command ) :
team_name = command . split ( " \n " ) [ 1 ] . strip ( )
player_name = command . split ( " \n " ) [ 2 ] . strip ( )
team , owner_id = games . get_team_and_owner ( team_name )
try :
new_pos = int ( command . split ( " \n " ) [ 3 ] . strip ( ) )
except ValueError :
await msg . channel . send ( " Hey, quit being cheeky. We ' re just trying to help. Third line has to be a natural number, boss. " )
return
if owner_id != msg . author . id and msg . author . id not in config ( ) [ " owners " ] :
await msg . channel . send ( " You ' re not authorized to mess with this team. Sorry, boss. " )
return
elif not team . slide_player ( player_name , new_pos ) :
await msg . channel . send ( " You either gave us a number that was bigger than your current roster, or we couldn ' t find the player on the team. Try again. " )
return
else :
await msg . channel . send ( embed = build_team_embed ( team ) )
games . update_team ( team )
await msg . channel . send ( " Paperwork signed, stamped, copied, and faxed up to the goddess. Xie ' s pretty quick with this stuff. " )
class AddPlayerCommand ( Command ) :
name = " addplayer "
template = """ m;addplayer pitcher (or m;addplayer batter)
[ team name ]
[ player name ] """
description = " Recruits a new player to your team, as either a pitcher or a batter. Requires team ownership. "
async def execute ( self , msg , command ) :
team_name = command . split ( " \n " ) [ 1 ] . strip ( )
player_name = command . split ( " \n " ) [ 2 ] . strip ( )
team , owner_id = games . get_team_and_owner ( team_name )
if owner_id != msg . author . id and msg . author . id not in config ( ) [ " owners " ] :
await msg . channel . send ( " You ' re not authorized to mess with this team. Sorry, boss. " )
return
new_player = games . player ( ono . get_stats ( player_name ) )
if " batter " in command . split ( " \n " ) [ 0 ] :
if not team . add_lineup ( new_player ) [ 0 ] :
await msg . channel . send ( " Too many batters 🎶 " )
return
elif " pitcher " in command . split ( " \n " ) [ 0 ] :
if not team . add_pitcher ( new_player ) :
await msg . channel . send ( " 8 pitchers is quite enough, we think. " )
return
await msg . channel . send ( embed = build_team_embed ( team ) )
games . update_team ( team )
await msg . channel . send ( " Paperwork signed, stamped, copied, and faxed up to the goddess. Xie ' s pretty quick with this stuff. " )
class DeletePlayerCommand ( Command ) :
name = " removeplayer "
template = """ m;removeplayer
[ team name ]
[ player name ] """
async def execute ( self , msg , command ) :
team_name = command . split ( " \n " ) [ 1 ] . strip ( )
player_name = command . split ( " \n " ) [ 2 ] . strip ( )
team , owner_id = games . get_team_and_owner ( team_name )
if owner_id != msg . author . id and msg . author . id not in config ( ) [ " owners " ] :
await msg . channel . send ( " You ' re not authorized to mess with this team. Sorry, boss. " )
return
if not team . delete_player ( player_name ) :
await msg . channel . send ( " We ' ve got bad news: that player isn ' t on your team. The good news is that... that player isn ' t on your team? " )
return
else :
await msg . channel . send ( embed = build_team_embed ( team ) )
games . update_team ( team )
await msg . channel . send ( " Paperwork signed, stamped, copied, and faxed up to the goddess. Xie ' s pretty quick with this stuff. " )
2021-01-02 06:10:52 +00:00
2020-12-27 08:42:09 +00:00
class HelpCommand ( Command ) :
name = " help "
template = " m;help [command] "
2020-12-28 16:05:19 +00:00
description = " Shows the instructions from the readme for a given command. If no command is provided, we will instead provide a list of all of the commands that instructions can be provided for. "
2020-12-27 08:42:09 +00:00
async def execute ( self , msg , command ) :
query = command . strip ( )
if query == " " :
2020-12-26 21:35:18 +00:00
text = " Here ' s everything we know how to do; use `m;help [command]` for more info: "
2020-12-27 08:42:09 +00:00
for comm in commands :
if comm . isauthorized ( msg . author ) :
text + = f " \n - { comm . name } "
2020-12-26 21:35:18 +00:00
else :
2020-12-27 08:42:09 +00:00
try :
comm = next ( c for c in commands if c . name == query and c . isauthorized ( msg . author ) )
text = f " ` { comm . template } ` \n { comm . description } "
except :
2020-12-26 21:35:18 +00:00
text = " Can ' t find that command, boss; try checking the list with `m;help`. "
await msg . channel . send ( text )
2020-12-28 00:05:49 +00:00
class DeleteTeamCommand ( Command ) :
name = " deleteteam "
template = " m;deleteteam [name] "
2020-12-28 16:05:19 +00:00
description = " Allows you to delete the team with the provided name if you are the owner of it, Gives a confirmation first to prevent accidental deletions. If it isn ' t letting you delete your team, you probably created it before teams having owners was a thing, contact xvi and xie can assign you as the owner. "
2020-12-28 00:05:49 +00:00
async def execute ( self , msg , command ) :
team_name = command . strip ( )
team , owner_id = games . get_team_and_owner ( team_name )
if owner_id != msg . author . id and msg . author . id not in config ( ) [ " owners " ] : #returns if person is not owner and not bot mod
await msg . channel . send ( " That team ain ' t yours, chief. If you think that ' s not right, bug xvi about deleting it for you. " )
return
elif team is not None :
delete_task = asyncio . create_task ( team_delete_confirm ( msg . channel , team , msg . author ) )
await delete_task
class AssignOwnerCommand ( Command ) :
name = " assignowner "
template = " m;assignowner [mention] [team] "
description = " assigns a discord user as the owner for a team. "
def isauthorized ( self , user ) :
return user . id in config ( ) [ " owners " ]
async def execute ( self , msg , command ) :
new_owner = msg . mentions [ 0 ]
team_name = command . strip ( ) . split ( new_owner . mention + " " ) [ 1 ]
print ( team_name )
if db . assign_owner ( team_name , new_owner . id ) :
await msg . channel . send ( f " { team_name } is now owned by { new_owner . display_name } . Don ' t break it. " )
else :
await msg . channel . send ( " We couldn ' t find that team. Typo? " )
2021-01-03 11:06:51 +00:00
class StartTournamentCommand ( Command ) :
name = " starttournament "
template = " m;starttournament "
description = " We ' ll DM you and get your own tournament set up. Just follow our instructions and we ' ll be right as rain. "
async def execute ( self , msg , command ) :
test_bracket = {
games . get_team ( " Milwaukee Lockpicks " ) : { " wins " : 0 , " rd " : 0 } ,
games . get_team ( " Madagascar Penguins " ) : { " wins " : 0 , " rd " : 0 } ,
games . get_team ( " Twin Cities Evening " ) : { " wins " : 0 , " rd " : 0 } ,
games . get_team ( " Washington State Houses " ) : { " wins " : 0 , " rd " : 0 } ,
games . get_team ( " Appalachian Underground " ) : { " wins " : 0 , " rd " : 0 } ,
games . get_team ( " Pacific2 Rams " ) : { " wins " : 0 , " rd " : 0 } ,
games . get_team ( " New Jersey Radio " ) : { " wins " : 0 , " rd " : 0 } ,
games . get_team ( " Moline Jolenes " ) : { " wins " : 0 , " rd " : 0 } ,
games . get_team ( " California Commissioners " ) : { " wins " : 0 , " rd " : 0 } ,
games . get_team ( " Pigeon’ s Reckoning " ) : { " wins " : 0 , " rd " : 0 } ,
games . get_team ( " Kernow Technologists " ) : { " wins " : 0 , " rd " : 0 }
}
2021-01-03 11:09:27 +00:00
tourney = leagues . tournament ( " Test Tourney " , test_bracket , max_innings = 3 )
2021-01-03 11:06:51 +00:00
tourney . build_bracket ( random_sort = True )
2021-01-03 11:30:07 +00:00
await start_tournament_round ( msg . channel , tourney )
2020-12-28 00:05:49 +00:00
2020-12-27 08:42:09 +00:00
commands = [
IntroduceCommand ( ) ,
2020-12-27 09:03:41 +00:00
CountActiveGamesCommand ( ) ,
2020-12-28 09:09:02 +00:00
AssignOwnerCommand ( ) ,
2020-12-27 08:42:09 +00:00
IdolizeCommand ( ) ,
ShowIdolCommand ( ) ,
ShowPlayerCommand ( ) ,
2020-12-28 03:22:40 +00:00
#SetupGameCommand(),
2020-12-27 08:42:09 +00:00
SaveTeamCommand ( ) ,
2020-12-28 09:09:02 +00:00
ImportCommand ( ) ,
2021-01-02 06:10:52 +00:00
SwapPlayerCommand ( ) ,
2021-01-02 09:02:57 +00:00
MovePlayerCommand ( ) ,
AddPlayerCommand ( ) ,
DeletePlayerCommand ( ) ,
2020-12-28 09:09:02 +00:00
DeleteTeamCommand ( ) ,
2020-12-27 08:42:09 +00:00
ShowTeamCommand ( ) ,
ShowAllTeamsCommand ( ) ,
SearchTeamsCommand ( ) ,
StartGameCommand ( ) ,
2021-01-03 11:06:51 +00:00
StartTournamentCommand ( ) ,
2020-12-27 08:42:09 +00:00
CreditCommand ( ) ,
RomanCommand ( ) ,
2020-12-28 00:05:49 +00:00
HelpCommand ( ) ,
2020-12-27 08:42:09 +00:00
]
2020-12-26 21:35:18 +00:00
2020-12-27 08:42:09 +00:00
client = discord . Client ( )
gamesarray = [ ]
2020-12-27 09:03:41 +00:00
gamesqueue = [ ]
2020-12-27 08:42:09 +00:00
setupmessages = { }
2020-12-26 21:35:18 +00:00
2020-12-28 08:21:47 +00:00
thread1 = threading . Thread ( target = main_controller . update_loop )
thread1 . start ( )
2020-12-27 08:42:09 +00:00
def config ( ) :
if not os . path . exists ( " config.json " ) :
#generate default config
config_dic = {
" token " : " " ,
" owners " : [
0000
] ,
" prefix " : [ " m; " , " m! " ] ,
2020-12-31 08:32:01 +00:00
" simmadome_url " : " " ,
2020-12-27 08:42:09 +00:00
" soulscream channel id " : 0 ,
" game_freeze " : 0
}
with open ( " config.json " , " w " ) as config_file :
json . dump ( config_dic , config_file , indent = 4 )
print ( " please fill in bot token and any bot admin discord ids to the new config.json file! " )
quit ( )
else :
with open ( " config.json " ) as config_file :
return json . load ( config_file )
2020-12-20 05:13:23 +00:00
2020-12-27 08:42:09 +00:00
@client.event
async def on_ready ( ) :
db . initialcheck ( )
print ( f " logged in as { client . user } with token { config ( ) [ ' token ' ] } " )
2020-12-31 08:32:01 +00:00
watch_task = asyncio . create_task ( game_watcher ( ) )
await watch_task
2020-12-27 08:42:09 +00:00
@client.event
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? " )
@client.event
async def on_message ( msg ) :
if msg . author == client . user :
return
command_b = False
for prefix in config ( ) [ " prefix " ] :
if msg . content . startswith ( prefix ) :
command_b = True
command = msg . content . split ( prefix , 1 ) [ 1 ]
if not command_b :
return
if msg . channel . id == config ( ) [ " soulscream channel id " ] :
2020-12-27 19:28:05 +00:00
await msg . channel . send ( ono . get_scream ( msg . author . display_name ) )
2020-12-27 08:42:09 +00:00
else :
try :
comm = next ( c for c in commands if command . startswith ( c . name ) )
await comm . execute ( msg , command [ len ( comm . name ) : ] )
except StopIteration :
2020-12-28 09:09:02 +00:00
await msg . channel . send ( " Can ' t find that command, boss; try checking the list with `m;help`. " )
except CommandError as ce :
await msg . channel . send ( str ( ce ) )
2020-12-20 00:08:09 +00:00
2020-12-23 01:20:58 +00:00
async def setup_game ( channel , owner , newgame ) :
newgame . owner = owner
2020-12-27 22:22:11 +00:00
await channel . send ( f " Game sucessfully created! \n Start any commands for this game with ` { newgame . name } ` so we know who ' s talking about what. " )
2020-12-23 01:20:58 +00:00
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
2020-12-27 01:18:27 +00:00
2020-12-23 01:20:58 +00:00
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! \n Now, 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!
2020-12-27 22:22:11 +00:00
team_join_message = await channel . send ( f """ Now, the lineups! We need somewhere between 1 and 12 batters. Cloning helps a lot with this sort of thing.
2020-12-23 01:20:58 +00:00
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 :
2020-12-27 01:09:19 +00:00
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? " )
2020-12-27 02:15:19 +00:00
del setupmessages [ team_join_message ]
del newgame
2020-12-27 01:09:19 +00:00
return
2020-12-23 01:20:58 +00:00
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 } ! \n Finally, 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
2020-12-31 18:41:32 +00:00
async def watch_game ( channel , newgame , user = None , league = None ) :
2021-01-03 11:06:51 +00:00
newgame , state_init = prepare_game ( newgame )
2020-12-27 01:18:27 +00:00
2021-01-03 11:06:51 +00:00
if league is not None :
discrim_string = league
state_init [ " is_league " ] = True
elif user is not None :
discrim_string = f " Started by { user . name } "
state_init [ " is_league " ] = False
else :
discrim_string = " Unclaimed game. "
state_init [ " is_league " ] = False
timestamp = str ( time . time ( ) * 1000.0 )
ext = " ?game= " + timestamp
if league is not None :
ext + = " &league= " + urllib . parse . quote_plus ( league )
await channel . send ( f " { newgame . teams [ ' away ' ] . name } vs. { newgame . teams [ ' home ' ] . name } , starting at { config ( ) [ ' simmadome_url ' ] + ext } " )
gamesarray . append ( ( newgame , channel , user , timestamp ) )
main_controller . master_games_dic [ timestamp ] = ( newgame , state_init , discrim_string )
def prepare_game ( newgame , league = None , weather_name = None ) :
if weather_name is None :
weathers = games . all_weathers ( )
newgame . weather = weathers [ random . choice ( list ( weathers . keys ( ) ) ) ]
2020-12-26 05:08:45 +00:00
2020-12-28 08:21:47 +00:00
state_init = {
" away_name " : newgame . teams [ ' away ' ] . name ,
" home_name " : newgame . teams [ ' home ' ] . name ,
" max_innings " : newgame . max_innings ,
" update_pause " : 0 ,
" top_of_inning " : True ,
" victory_lap " : False ,
" weather_emoji " : newgame . weather . emoji ,
" weather_text " : newgame . weather . name ,
2020-12-31 20:13:02 +00:00
" start_delay " : 5 ,
" end_delay " : 10
2020-12-28 08:21:47 +00:00
}
2021-01-03 11:06:51 +00:00
if league is None :
state_init [ " is_league " ] = False
else :
state_init [ " is_league " ] = True
2021-01-02 10:42:14 +00:00
if newgame . weather . name == " Heavy Snow " :
newgame . weather . counter_away = random . randint ( 0 , len ( newgame . teams [ ' away ' ] . lineup ) - 1 )
newgame . weather . counter_home = random . randint ( 0 , len ( newgame . teams [ ' home ' ] . lineup ) - 1 )
2021-01-03 11:06:51 +00:00
return newgame , state_init
2020-12-28 08:21:47 +00:00
2021-01-03 11:06:51 +00:00
async def start_tournament_round ( channel , tourney , seeding = None ) :
current_games = [ ]
if tourney . bracket is None :
if seeding is None :
tourney . build_bracket ( random_sort = True )
2020-12-26 05:08:45 +00:00
2021-01-03 11:06:51 +00:00
games_to_start = tourney . bracket . get_bottom_row ( )
2021-01-02 16:51:47 +00:00
2021-01-03 11:06:51 +00:00
for pair in games_to_start :
if pair [ 0 ] is not None and pair [ 1 ] is not None :
this_game = games . game ( pair [ 0 ] . finalize ( ) , pair [ 1 ] . finalize ( ) , length = tourney . game_length )
this_game , state_init = prepare_game ( this_game )
2021-01-02 16:51:47 +00:00
2021-01-03 11:06:51 +00:00
state_init [ " is_league " ] = True
discrim_string = tourney . name
print ( discrim_string )
2021-01-01 17:08:11 +00:00
2021-01-03 11:06:51 +00:00
timestamp = str ( time . time ( ) * 1000.0 + random . randint ( 0 , 3000 ) )
current_games . append ( ( this_game , timestamp ) )
main_controller . master_games_dic [ timestamp ] = ( this_game , state_init , discrim_string )
2020-12-26 05:08:45 +00:00
2021-01-03 11:06:51 +00:00
ext = " ?league= " + urllib . parse . quote_plus ( tourney . name )
await channel . send ( f " { len ( current_games ) } games started for the { tourney . name } tournament, at { config ( ) [ ' simmadome_url ' ] + ext } " )
2021-01-03 11:30:07 +00:00
await tourney_watcher ( channel , tourney , current_games ) )
2021-01-03 11:06:51 +00:00
async def tourney_watcher ( channel , tourney , games_list ) :
tourney . active = True
while len ( games_list ) > 0 :
2021-01-03 11:30:07 +00:00
try :
for i in range ( 0 , len ( games_list ) ) :
game , key = games_list [ i ]
if game . over and main_controller . master_games_dic [ key ] [ 1 ] [ " end_delay " ] < = 9 :
final_embed = game_over_embed ( game )
await channel . send ( f " A { tourney . name } game just ended! " )
await channel . send ( embed = final_embed )
games_list . pop ( i )
break
except :
print ( " something went wrong in tourney_watcher " )
await asyncio . sleep ( 4 )
2021-01-03 11:06:51 +00:00
tourney . active = False
await channel . send ( f " This round of games for { tourney . name } is now complete! " )
2020-12-27 05:52:34 +00:00
2020-12-28 00:05:49 +00:00
async def team_delete_confirm ( channel , team , owner ) :
team_msg = await channel . send ( embed = build_team_embed ( team ) )
checkmsg = await channel . send ( " Is this the team you want to axe, boss? " )
await checkmsg . add_reaction ( " 👍 " )
await checkmsg . add_reaction ( " 👎 " )
def react_check ( react , user ) :
return user == owner and react . message == checkmsg
try :
react , user = await client . wait_for ( ' reaction_add ' , timeout = 20.0 , check = react_check )
if react . emoji == " 👍 " :
await channel . send ( " Step back, this could get messy. " )
if db . delete_team ( team ) :
await asyncio . sleep ( 2 )
await channel . send ( " Job ' s done. We ' ll clean up on our way out, don ' t worry. " )
else :
await asyncio . sleep ( 2 )
await channel . send ( " Huh. Didn ' t quite work. Tell xvi next time you see xer. " )
return
elif react . emoji == " 👎 " :
await channel . send ( " Message received. Pumping brakes, turning this car around. " )
return
except asyncio . TimeoutError :
await channel . send ( " Guessing you got cold feet, so we ' re putting the axe away. Let us know if we need to fetch it again, aye? " )
return
2020-12-27 05:52:34 +00:00
2020-12-27 01:18:27 +00:00
2020-12-24 09:51:38 +00:00
def build_team_embed ( team ) :
embed = discord . Embed ( color = discord . Color . purple ( ) , title = team . name )
lineup_string = " "
for player in team . lineup :
lineup_string + = f " { player . name } { player . star_string ( ' batting_stars ' ) } \n "
2021-01-02 06:10:52 +00:00
rotation_string = " "
for player in team . rotation :
rotation_string + = f " { player . name } { player . star_string ( ' pitching_stars ' ) } \n "
embed . add_field ( name = " Rotation: " , value = rotation_string , inline = False )
2020-12-24 09:51:38 +00:00
embed . add_field ( name = " Lineup: " , value = lineup_string , inline = False )
embed . set_footer ( text = team . slogan )
return embed
2020-12-20 00:08:09 +00:00
2020-12-20 12:25:25 +00:00
def build_star_embed ( player_json ) :
starkeys = { " batting_stars " : " Batting " , " pitching_stars " : " Pitching " , " baserunning_stars " : " Baserunning " , " defense_stars " : " Defense " }
embed = discord . Embed ( color = discord . Color . purple ( ) , title = player_json [ " name " ] )
for key in starkeys . keys ( ) :
2020-12-20 19:05:31 +00:00
embedstring = " "
2020-12-20 12:25:25 +00:00
starstring = str ( player_json [ key ] )
2020-12-27 01:18:01 +00:00
starnum = int ( starstring [ 0 ] )
addhalf = " .5 " in starstring
2020-12-21 05:23:02 +00:00
embedstring + = " ⭐ " * starnum
2020-12-20 12:25:25 +00:00
if addhalf :
embedstring + = " ✨ "
2020-12-27 01:18:01 +00:00
elif starnum == 0 : # why check addhalf twice, amirite
embedstring + = " ⚪️ "
2020-12-20 12:25:25 +00:00
embed . add_field ( name = starkeys [ key ] , value = embedstring , inline = False )
return embed
2020-12-28 15:27:20 +00:00
def team_from_collection ( newteam_json ) :
2020-12-28 09:09:02 +00:00
# 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
2020-12-20 12:25:25 +00:00
2020-12-28 09:09:02 +00:00
def team_from_message ( command ) :
2020-12-24 09:51:38 +00:00
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
2021-01-02 06:10:52 +00:00
if not roster [ 2 ] . strip ( ) == " " :
raise CommandError ( " The third line should be blank. It wasn ' t, so just in case, we ' ve not done anything on our end. " )
pitchernum = len ( roster ) - 2
for rosternum in range ( 3 , len ( roster ) - 1 ) :
2020-12-24 09:51:38 +00:00
if roster [ rosternum ] != " " :
2020-12-26 05:27:14 +00:00
if len ( roster [ rosternum ] ) > 70 :
2020-12-28 09:09:02 +00:00
raise CommandError ( f " { roster [ rosternum ] } is too long, chief. 70 or less. " )
2020-12-26 23:12:38 +00:00
newteam . add_lineup ( games . player ( ono . get_stats ( roster [ rosternum ] . rstrip ( ) ) ) )
2021-01-02 06:10:52 +00:00
else :
pitchernum = rosternum + 1
break
for rosternum in range ( pitchernum , len ( roster ) ) :
if len ( roster [ rosternum ] ) > 70 :
raise CommandError ( f " { roster [ len ( roster ) - 1 ] } is too long, chief. 70 or less. " )
newteam . add_pitcher ( games . player ( ono . get_stats ( roster [ rosternum ] . rstrip ( ) ) ) )
2020-12-21 05:23:02 +00:00
2020-12-24 09:51:38 +00:00
if len ( newteam . name ) > 30 :
2020-12-28 09:09:02 +00:00
raise CommandError ( " Team names have to be less than 30 characters! Try again. " )
2020-12-24 09:51:38 +00:00
elif len ( newteam . slogan ) > 100 :
2020-12-28 09:09:02 +00:00
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. " )
2020-12-24 09:51:38 +00:00
2020-12-28 09:09:02 +00:00
return newteam
async def save_team_confirm ( message , newteam ) :
2020-12-24 09:51:38 +00:00
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. " )
2020-12-27 02:53:46 +00:00
games . save_team ( newteam , message . author . id )
2020-12-24 09:51:38 +00:00
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 :
2020-12-27 22:22:11 +00:00
await message . channel . send ( " Look, we don ' t have all day. 20 seconds is long enough, right? Try again. " )
2020-12-24 09:51:38 +00:00
return
2020-12-26 09:46:42 +00:00
2020-12-26 10:09:49 +00:00
async def team_pages ( msg , all_teams , search_term = None ) :
2020-12-26 09:46:42 +00:00
pages = [ ]
page_max = math . ceil ( len ( all_teams ) / 25 )
2020-12-26 10:09:49 +00:00
if search_term is not None :
2020-12-26 10:23:01 +00:00
title_text = f " All teams matching \" { search_term } \" : "
2020-12-26 10:09:49 +00:00
else :
title_text = " All Teams "
2020-12-26 09:46:42 +00:00
for page in range ( 0 , page_max ) :
2020-12-26 10:09:49 +00:00
embed = discord . Embed ( color = discord . Color . purple ( ) , title = title_text )
2020-12-26 09:46:42 +00:00
embed . set_footer ( text = f " Page { page + 1 } of { page_max } " )
for i in range ( 0 , 25 ) :
try :
embed . add_field ( name = all_teams [ i + 25 * page ] . name , value = all_teams [ i + 25 * page ] . slogan )
except :
break
pages . append ( embed )
teams_list = await msg . channel . send ( embed = pages [ 0 ] )
current_page = 0
if page_max > 1 :
await teams_list . add_reaction ( " ◀ " )
await teams_list . add_reaction ( " ▶ " )
def react_check ( react , user ) :
return user == msg . author and react . message == teams_list
while True :
try :
2020-12-27 01:09:19 +00:00
react , user = await client . wait_for ( ' reaction_add ' , timeout = 60.0 , check = react_check )
2020-12-26 09:46:42 +00:00
if react . emoji == " ◀ " and current_page > 0 :
current_page - = 1
2020-12-27 08:21:48 +00:00
await react . remove ( user )
2020-12-27 01:11:05 +00:00
elif react . emoji == " ▶ " and current_page < ( page_max - 1 ) :
2020-12-26 09:46:42 +00:00
current_page + = 1
2020-12-27 08:21:48 +00:00
await react . remove ( user )
2020-12-26 09:46:42 +00:00
await teams_list . edit ( embed = pages [ current_page ] )
except asyncio . TimeoutError :
return
2020-12-31 08:32:01 +00:00
async def game_watcher ( ) :
while True :
2021-01-01 21:24:05 +00:00
try :
this_array = gamesarray . copy ( )
for i in range ( 0 , len ( this_array ) ) :
game , channel , user , key = this_array [ i ]
2021-01-03 11:06:51 +00:00
if game . over and main_controller . master_games_dic [ key ] [ 1 ] [ " end_delay " ] < = 9 :
final_embed = game_over_embed ( game )
2021-01-01 21:24:05 +00:00
if user is not None :
await channel . send ( f " { user . mention } ' s game just ended. " )
else :
2021-01-03 11:06:51 +00:00
await channel . send ( " A game started from this channel just ended. " )
2021-01-01 21:24:05 +00:00
await channel . send ( embed = final_embed )
gamesarray . pop ( i )
break
except :
print ( " something broke in game_watcher " )
2021-01-03 11:06:51 +00:00
await asyncio . sleep ( 4 )
2020-12-31 08:32:01 +00:00
2021-01-03 11:06:51 +00:00
def game_over_embed ( game ) :
title_string = f " { game . teams [ ' away ' ] . name } at { game . teams [ ' home ' ] . name } ended after { game . inning - 1 } innings "
if ( game . inning - 1 ) > game . max_innings : #if extra innings
title_string + = f " with { game . inning - ( game . max_innings + 1 ) } extra innings. "
else :
title_string + = " . "
winning_team = game . teams [ ' home ' ] . name if game . teams [ ' home ' ] . score > game . teams [ ' away ' ] . score else game . teams [ ' away ' ] . name
winstring = f " { game . teams [ ' away ' ] . score } to { game . teams [ ' home ' ] . score } \n "
if game . victory_lap and winning_team == game . teams [ ' home ' ] . name :
winstring + = f " { winning_team } wins with a victory lap! "
elif winning_team == game . teams [ ' home ' ] . name :
winstring + = f " { winning_team } wins, shaming { game . teams [ ' away ' ] . name } ! "
else :
winstring + = f " { winning_team } wins! "
2020-12-26 09:46:42 +00:00
2021-01-03 11:06:51 +00:00
embed = discord . Embed ( color = discord . Color . dark_purple ( ) , title = title_string )
embed . add_field ( name = " Final score: " , value = winstring )
return embed
2020-12-31 08:32:01 +00:00
2020-12-26 10:23:01 +00:00
client . run ( config ( ) [ " token " ] )