diff --git a/database.py b/database.py index e8b1ca2..79b5e49 100644 --- a/database.py +++ b/database.py @@ -207,21 +207,51 @@ def save_team(name, team_json_string, user_id): except: return False -def get_team(name): +def get_team(name, owner=False): conn = create_connection() if conn is not None: c = conn.cursor() - c.execute("SELECT team_json_string FROM teams WHERE name=?", (re.sub('[^A-Za-z0-9 ]+', '', name),)) #see above note re: regex + if not owner: + c.execute("SELECT team_json_string FROM teams WHERE name=?", (re.sub('[^A-Za-z0-9 ]+', '', name),)) #see above note re: regex + else: + c.execute("SELECT * FROM teams WHERE name=?", (re.sub('[^A-Za-z0-9 ]+', '', name),)) #see above note re: regex team = c.fetchone() conn.close() - return team #returns a json string + return team #returns a json string if owner is false, otherwise returns (counter, name, team_json_string, timestamp, owner_id) + conn.close() return None +def delete_team(team): + conn = create_connection() + if conn is not None: + try: + c = conn.cursor() + c.execute("DELETE FROM teams WHERE name=?", (re.sub('[^A-Za-z0-9 ]+', '', team.name),)) + conn.commit() + conn.close() + return True + except: + conn.close() + return False + +def assign_owner(team_name, owner_id): + conn = create_connection() + if conn is not None: + try: + c = conn.cursor() + c.execute("UPDATE teams SET owner_id = ? WHERE name = ?",(owner_id, re.sub('[^A-Za-z0-9 ]+', '', team_name))) + conn.commit() + conn.close() + return True + except: + conn.close() + return False + def get_all_teams(): conn = create_connection() if conn is not None: diff --git a/games.py b/games.py index ec99196..0bf07ee 100644 --- a/games.py +++ b/games.py @@ -610,6 +610,16 @@ def get_team(name): except: return None +def get_team_and_owner(name): + #try: + counter, name, team_json_string, timestamp, owner_id = db.get_team(name, owner=True) + team_json = jsonpickle.decode(team_json_string, keys=True, classes=team) + if team_json is not None: + return (team_json, owner_id) + return None + #except: + #return None + def save_team(this_team, user_id): try: this_team.prepare_for_save() diff --git a/the_prestige.py b/the_prestige.py index 33831f0..fb5ee44 100644 --- a/the_prestige.py +++ b/the_prestige.py @@ -259,6 +259,44 @@ class HelpCommand(Command): text = "Can't find that command, boss; try checking the list with `m;help`." await msg.channel.send(text) +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." + + 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): + #try: + 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?") + #except: + #await msg.channel.send("We hit a snag. Tell xvi.") + + + + commands = [ IntroduceCommand(), CountActiveGamesCommand(), @@ -273,7 +311,9 @@ commands = [ StartGameCommand(), CreditCommand(), RomanCommand(), - HelpCommand() + HelpCommand(), + DeleteTeamCommand(), + AssignOwnerCommand() ] client = discord.Client() @@ -632,6 +672,32 @@ async def play_from_queue(channel, game, user_mention): game_task = asyncio.create_task(watch_game(channel, game)) await game_task +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 def build_team_embed(team): @@ -664,7 +730,6 @@ def build_star_embed(player_json): 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 @@ -707,8 +772,6 @@ async def save_team_batch(message, command): except asyncio.TimeoutError: await message.channel.send("Look, we don't have all day. 20 seconds is long enough, right? Try again.") return - #except: - #await message.channel.send("uh.") async def team_pages(msg, all_teams, search_term=None):