From 8bc3abdf4dbf3112603fa5c63af87105c980d303 Mon Sep 17 00:00:00 2001 From: Elijah Steres Date: Mon, 28 Dec 2020 04:09:02 -0500 Subject: [PATCH 1/5] add m;import - no cache for collections currently --- onomancer.py | 9 +++++- the_prestige.py | 78 ++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 69 insertions(+), 18 deletions(-) diff --git a/onomancer.py b/onomancer.py index 7b5e380..bbd00fa 100644 --- a/onomancer.py +++ b/onomancer.py @@ -6,6 +6,7 @@ import database as db onomancer_url = "https://onomancer.sibr.dev/api/" name_stats_hook = "generateStats2?name=" +collection_hook = "getCollection?token=" def get_stats(name): player = db.get_stats(name) @@ -26,4 +27,10 @@ def get_scream(username): else: scream = json.loads(get_stats(username))["soulscream"] db.cache_soulscream(username, scream) - return scream \ No newline at end of file + return scream + +def get_collection(collection_url): + # TODO: should probably add collection cache + response = requests.get(onomancer_url + collection_hook + urllib.parse.quote(collection_url)) + if response.status_code == 200: + return json.dumps(response.json()) \ No newline at end of file diff --git a/the_prestige.py b/the_prestige.py index 913e664..49b8e15 100644 --- a/the_prestige.py +++ b/the_prestige.py @@ -9,6 +9,9 @@ class Command: async def execute(self, msg, command): return +class CommandError(Exception): + pass + class IntroduceCommand(Command): name = "introduce" template = "" @@ -185,13 +188,31 @@ class SaveTeamCommand(Command): if you did it correctly, you'll get a team embed with a prompt to confirm. Hit the πŸ‘ and it'll be saved.""" async def execute(self, msg, command): - if db.get_team(command.split("\n")[0]) == None: - save_task = asyncio.create_task(save_team_batch(msg, command)) + if db.get_team(command.split('\n',1)[1].split("\n")[0]) == None: + team = team_from_message(command) + save_task = asyncio.create_task(save_team_confirm(msg, team)) 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?") +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?") + class ShowTeamCommand(Command): name = "showteam" template = "m;showteam [name]" @@ -295,16 +316,17 @@ class AssignOwnerCommand(Command): #await msg.channel.send("We hit a snag. Tell xvi.") - - commands = [ IntroduceCommand(), CountActiveGamesCommand(), + AssignOwnerCommand(), IdolizeCommand(), ShowIdolCommand(), ShowPlayerCommand(), #SetupGameCommand(), SaveTeamCommand(), + ImportCommand(), + DeleteTeamCommand(), ShowTeamCommand(), ShowAllTeamsCommand(), SearchTeamsCommand(), @@ -312,8 +334,6 @@ commands = [ CreditCommand(), RomanCommand(), HelpCommand(), - DeleteTeamCommand(), - AssignOwnerCommand() ] client = discord.Client() @@ -383,7 +403,9 @@ async def on_message(msg): comm = next(c for c in commands if command.startswith(c.name)) await comm.execute(msg, command[len(comm.name):]) except StopIteration: - return + 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)) async def start_game(channel): msg = await channel.send("Play ball!") @@ -727,8 +749,32 @@ def build_star_embed(player_json): embed.add_field(name=starkeys[key], value=embedstring, inline=False) return embed +def team_from_collection(collection_raw): + newteam_json = json.loads(collection_raw) + # 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.") -async def save_team_batch(message, command): + #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 + +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 @@ -736,21 +782,20 @@ async def save_team_batch(message, command): for rosternum in range(2,len(roster)-1): if roster[rosternum] != "": if len(roster[rosternum]) > 70: - await message.channel.send(f"{roster[rosternum]} is too long, chief. 70 or less.") - return + 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: - await message.channel.send(f"{roster[len(roster)-1]} is too long, chief. 70 or less.") - return + 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: - await message.channel.send("Team names have to be less than 30 characters! Try again.") - return + raise CommandError("Team names have to be less than 30 characters! Try again.") 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 + 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 + +async def save_team_confirm(message, newteam): 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("πŸ‘") @@ -773,7 +818,6 @@ async def save_team_batch(message, command): await message.channel.send("Look, we don't have all day. 20 seconds is long enough, right? Try again.") return - async def team_pages(msg, all_teams, search_term=None): pages = [] page_max = math.ceil(len(all_teams)/25) From 1812553c9b8839b2e69b452ae19ffddcce5cdb84 Mon Sep 17 00:00:00 2001 From: Elijah Steres Date: Mon, 28 Dec 2020 10:27:20 -0500 Subject: [PATCH 2/5] cache player retrieved from collection url --- onomancer.py | 4 +++- the_prestige.py | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/onomancer.py b/onomancer.py index bbd00fa..c5fbe14 100644 --- a/onomancer.py +++ b/onomancer.py @@ -30,7 +30,9 @@ def get_scream(username): return scream def get_collection(collection_url): - # TODO: should probably add collection cache response = requests.get(onomancer_url + collection_hook + urllib.parse.quote(collection_url)) if response.status_code == 200: + for player in response.json()['lineup'] + response.json()['rotation']: + db.cache_stats(player['name'], json.dumps(player)) + return json.dumps(response.json()) \ No newline at end of file diff --git a/the_prestige.py b/the_prestige.py index 49b8e15..256d5ce 100644 --- a/the_prestige.py +++ b/the_prestige.py @@ -749,8 +749,7 @@ def build_star_embed(player_json): embed.add_field(name=starkeys[key], value=embedstring, inline=False) return embed -def team_from_collection(collection_raw): - newteam_json = json.loads(collection_raw) +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.") From 96a0a8a38e7407db5163a2195595f9ef12fa0dfc Mon Sep 17 00:00:00 2001 From: Genderdruid Date: Mon, 28 Dec 2020 08:05:19 -0800 Subject: [PATCH 3/5] Updated README and help text in the_presige Updated all of the help text for the commands to match the text in the readme and updated the readme, fixing a few spelling/grammar errors and removing the section about and all references to the now disabled pickup game funtionality. --- README.md | 12 +++++------- the_prestige.py | 36 ++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index c1f0d96..85f75e7 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ blaseball, blaseball, is back! in an unofficial capacity. custom players, custom teams, custom leagues (that last one is coming soonβ„’) all in discord! -we've also got things like player idolization, custom team creation, easy setup for your teams to play against each other, and quick pick-up games featuring any players you like, all powered by this bot and onomancer. +we've also got things like custom team creation, easy setup for your teams to play against each other, and player idolization, all powered by this bot and onomancer. accepting pull requests, check the issues for to-dos. @@ -14,7 +14,7 @@ accepting pull requests, check the issues for to-dos. ### team commands: - m;saveteam - - saves a team to the database allowing it to be used for games. send this command at the top of a list, with entries seperated by new lines (shift+enter in discord, or copy+paste from notepad). + - 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. - 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. @@ -27,13 +27,13 @@ accepting pull requests, check the issues for to-dos. - m;searchteams [searchterm] - shows a paginated list of all teams whose names contain the given search term. - m;deleteteam [teamname] - - allows you to delete the team with the provided name if you are the owner of it, gives a conformation 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. + - 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. ### player commands: - m;showplayer [name] - displays any name's stars, 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 - m;idolize [name] - - records any name as your idol, mostly for fun but also can be used for pickup games. + - records any name as your idol, mostly for fun. - m;showidol - displays your idol's name and stars. @@ -43,12 +43,10 @@ accepting pull requests, check the issues for to-dos. - 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. -- m;setupgame - - begins setting up a 3-inning pickup game. pitchers, lineups, and team names are given during the setup process by anyone. idols are easily signed up via emoji during the process. the game will start automatically after the setup is completed. ### other commands: - m;help [command] - - shows the instuctions from here for given command. if no command is provided, it will instead provide a list of all of the commands that instructions can be provided for. + - shows the instructions from here for given command. if no command is provided, it will instead provide a list of all of the commands that instructions can be provided for. - m;credit - shows artist credit for matteo's avatar. - m;roman [number] diff --git a/the_prestige.py b/the_prestige.py index 913e664..49ca223 100644 --- a/the_prestige.py +++ b/the_prestige.py @@ -53,7 +53,7 @@ class RomanCommand(Command): class IdolizeCommand(Command): name = "idolize" template = "m;idolize [name]" - description = "Records any name as your idol, used elsewhere. There's a limit of 70 characters. That should be *plenty*." + description = "Records any name as your idol, mostly for fun. There's a limit of 70 characters. That should be *plenty*." async def execute(self, msg, command): if (command.startswith("meme")): @@ -94,7 +94,7 @@ class ShowIdolCommand(Command): class ShowPlayerCommand(Command): name = "showplayer" template = "m;showplayer [name]" - description = "Displays any name's stars in a nice discord embed." + 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" async def execute(self, msg, command): player_name = json.loads(ono.get_stats(command.split(" ",1)[1])) @@ -103,10 +103,10 @@ class ShowPlayerCommand(Command): class StartGameCommand(Command): name = "startgame" template = "m;startgame [away] [home] [innings]" - description ="""To start a game with premade teams, use this command at the top of a list, with lines seperated by newlines (shift+enter in discord, or copy+paste from notepad) - - the first line is the away team's name - - the second is the home team's name - - optionally, the third is the number of innings, which must be greater than 2.""" + 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.""" async def execute(self, msg, command): if config()["game_freeze"]: @@ -177,13 +177,13 @@ class SetupGameCommand(Command): class SaveTeamCommand(Command): name = "saveteam" template = "m;saveteam [name] [slogan] [players]" - description = """To save an entire team, send this command at the top of a list, with lines seperated by newlines (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 is your team's slogan - - the rest of the lines are your players' names - - the last player is designated your pitcher -if you did it correctly, you'll get a team embed with a prompt to confirm. Hit the πŸ‘ and it'll be saved.""" - + 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. + - 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. + - the final line is your pitcher's name. +if you did it correctly, you'll get a team embed with a prompt to confirm. hit the πŸ‘ and it'll be saved.""" + async def execute(self, msg, command): if db.get_team(command.split("\n")[0]) == None: save_task = asyncio.create_task(save_team_batch(msg, command)) @@ -195,7 +195,7 @@ if you did it correctly, you'll get a team embed with a prompt to confirm. Hit t class ShowTeamCommand(Command): name = "showteam" template = "m;showteam [name]" - description = "You can view any saved team with this command" + description = "Shows information about any saved team." async def execute(self, msg, command): team_name = command.strip() @@ -212,7 +212,7 @@ class ShowTeamCommand(Command): class ShowAllTeamsCommand(Command): name = "showallteams" template = "m;showallteams" - description = "This displays a paginated list of all teams available for `startgame`" + description = "Shows a paginated list of all teams available for games which can be scrolled through." async def execute(self, msg, command): list_task = asyncio.create_task(team_pages(msg, games.get_all_teams())) @@ -221,7 +221,7 @@ class ShowAllTeamsCommand(Command): class SearchTeamsCommand(Command): name = "searchteams" template = "m;searchteams [searchterm]" - description = "Displays paginated list of all teams whose names contain `searchterm`" + description = "Shows a paginated list of all teams whose names contain the given search term." async def execute(self, msg, command): search_term = command.strip() @@ -242,7 +242,7 @@ class CreditCommand(Command): class HelpCommand(Command): name = "help" template = "m;help [command]" - description = "Displays a list of all commands, or the description of the given command if one is present." + 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." async def execute(self, msg, command): query = command.strip() @@ -262,7 +262,7 @@ class HelpCommand(Command): class DeleteTeamCommand(Command): name = "deleteteam" template = "m;deleteteam [name]" - description = "Deletes a team. Only works if you're the one who made it in the first place, and yes, we do check." + 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." async def execute(self, msg, command): team_name = command.strip() From c059992ec852e3c23f02dd17c9d3ef32dec87012 Mon Sep 17 00:00:00 2001 From: Danny Ben-David Date: Mon, 28 Dec 2020 18:06:50 -0500 Subject: [PATCH 4/5] [tweak] "Game for {user.mention}:" `user.display_name` doesn't tag the user who requested the game. Using `user.mention` makes at tags, which also means users can search for games using Discord search (`Game for mentions:@foobar`) --- the_prestige.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/the_prestige.py b/the_prestige.py index 0596ab0..a1703ae 100644 --- a/the_prestige.py +++ b/the_prestige.py @@ -553,7 +553,7 @@ async def watch_game(channel, newgame, user = None): in_emoji = discord.utils.get(client.emojis, id = 791578957244792832) if user is not None: - await channel.send(f"Game for {user.display_name}:") + await channel.send(f"Game for {user.mention}:") embed = await channel.send("Starting...") await asyncio.sleep(1) await embed.pin() From 5c63ff600c454434c1c5e70713cf0b7376d2d1b4 Mon Sep 17 00:00:00 2001 From: Sakimori Date: Tue, 29 Dec 2020 00:02:22 -0500 Subject: [PATCH 5/5] edited gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3ce9b2f..1722461 100644 --- a/.gitignore +++ b/.gitignore @@ -346,4 +346,5 @@ weather_config.json ids # database -matteo.db \ No newline at end of file +matteo.db +/matteo_env/Lib/site-packages/flask_socketio/__init__.py