async def register_player(self, ctx): """Registers a player""" try: Player.get(ctx.author.id) except: new_player = Player(ctx.author.id) await ctx.send("You in") return db.update(new_player) raise commands.UserInputError("Already registered")
async def battleship(self, ctx): """Starts up a game of battleship.""" authorize(ctx, "mentions") # check for a mentioned user. p1 = Player.get(ctx.author.id) p2 = Player.get(ctx.message.mentions[0].id) if p1 == p2: raise UserInputError("Can't play against yourself") game = BattleShip(ctx.channel, p1, p2)
async def sub(self, ctx, args): captain = None if not roles.is_admin(ctx.author): if self.match.status is MatchStatus.IS_CAPTAIN: await disp.SUB_ONLY_ADMIN.send(ctx) return captain = await get_check_captain(ctx, self.match, check_turn=False) if not captain: return if len(ctx.message.mentions) not in (1, 2): await disp.RM_MENTION_ONE.send(ctx) return subbed = Player.get(ctx.message.mentions[0].id) if not subbed: await disp.RM_NOT_IN_DB.send(ctx) return if not (subbed.match and subbed.match is self.match.proxy): await disp.SUB_NO.send(ctx) return if subbed.active and subbed.active.is_playing: subbed.active.team.captain.is_turn = True subbed.active.team.on_team_ready(False) # Can't have a swap command running at the same time self.factory.swap.on_clean() self.factory.bench.on_clean() if roles.is_admin(ctx.author): player = None if len(ctx.message.mentions) == 2: player = Player.get(ctx.message.mentions[1].id) if not player: await disp.RM_NOT_IN_DB.send(ctx) return elif player.match: await disp.SUB_NO.send(ctx) return await self.validator.force_confirm(ctx, subbed=subbed, force_player=player) return else: other_captain = self.match.teams[captain.team.id - 1].captain ctx = self.validator.arm(self.match.channel, captain, subbed=subbed) await disp.SUB_OK_CONFIRM.send(ctx, subbed.mention, other_captain.mention)
async def tictactoe(self, ctx): """Starts up a game of tic tac toe with another user""" authorize(ctx, "mentions") # check for a mentioned user p1 = Player.get(ctx.author.id) p2 = Player.get(ctx.message.mentions[0].id) if p1 == p2: raise UserInputError("You can't play against yourself") # Create new game game = TicTacToe(ctx.channel, p1, p2) # draw and send board await game.update()
async def on_rule_accept(player, interaction_id, interaction, interaction_values): user = interaction.user if modules.loader.is_all_locked(): raise modules.interactions.InteractionNotAllowed # reaction to the rule message? p = Player.get(user.id) if not p: # if new player # create a new profile p = Player(user.id, user.name) await modules.roles.role_update(p) await modules.database.async_db_call(modules.database.set_element, "users", p.id, p.get_data()) await disp.REG_RULES.send( ContextWrapper.channel(cfg.channels["register"]), user.mention) elif p.is_away: p.is_away = False await modules.roles.role_update(p) await p.db_update("away") await disp.AWAY_BACK.send( ContextWrapper.channel(cfg.channels["register"]), p.mention) else: i_ctx = InteractionContext(interaction) await disp.REG_RULES_ALREADY.send(i_ctx) await modules.roles.role_update(p)
async def join(self, ctx): """ Join queue """ if lobby.get_lobby_len() > cfg.general["lobby_size"]: # This should not happen EVER await disp.UNKNOWN_ERROR.send(ctx, "Lobby Overflow") return player = Player.get(ctx.message.author.id) if not player: await disp.EXT_NOT_REGISTERED.send(ctx, cfg.channels["register"]) return if not player.is_registered: await disp.EXT_NOT_REGISTERED.send(ctx, cfg.channels["register"]) return accs = player.accounts_flipped if len(accs) != 0: await disp.CHECK_ACCOUNT.send(ctx, cfg.channels["register"], account_names=accs) return if player.is_lobbied: await disp.LB_ALREADY_IN.send(ctx) return if player.match: await disp.LB_IN_MATCH.send(ctx) return if lobby.is_lobby_stuck(): await disp.LB_STUCK_JOIN.send(ctx) return names = lobby.add_to_lobby(player) await disp.LB_ADDED.send(ctx, names_in_lobby=names)
async def show_player_titles(self, ctx): """Show a player's collected titles.""" player = Player.get(ctx.author.id) title_embed = discord.Embed(title=f"{player.name}'s Titles", description='\n'.join(player.titles)) await ctx.send(embed=title_embed)
async def lobby(self, ctx, *args): if ctx.channel.id != cfg.channels["lobby"]: await disp.WRONG_CHANNEL.send(ctx, ctx.command.name, f'<#{cfg.channels["lobby"]}>') return if len(args) > 0 and args[0] == "restore": for mention in ctx.message.mentions: try: p_id = mention.id player = Player.get(int(p_id)) if player and not lobby.is_lobby_stuck( ) and player.is_registered and not player.is_lobbied: lobby.add_to_lobby(player) except ValueError: pass await disp.LB_QUEUE.send( ctx, names_in_lobby=lobby.get_all_names_in_lobby()) return if len(args) > 0 and args[0] == "save": lb = lobby.get_all_ids_in_lobby() await db.async_db_call(db.set_field, "restart_data", 0, {"last_lobby": lb}) await disp.LB_SAVE.send(ctx) return if len(args) > 0 and args[0] == "get": lb = lobby.get_all_ids_in_lobby() await disp.LB_GET.send(ctx, " ".join([str(p_id) for p_id in lb])) return await disp.WRONG_USAGE.send(ctx, ctx.command.name)
async def timeout(self, ctx, *args): if len(args) == 1 and args[0] == "help": await disp.RM_TIMEOUT_HELP.send(ctx) return if len(ctx.message.mentions) != 1: await disp.RM_MENTION_ONE.send(ctx) return player = Player.get(ctx.message.mentions[0].id) if not player: # player isn't even registered in the system... player = Player(ctx.message.mentions[0].id, ctx.message.mentions[0].name) await db.async_db_call(db.set_element, "users", player.id, player.get_data()) if player.is_lobbied: lobby.remove_from_lobby(player) await disp.RM_LOBBY.send( ContextWrapper.channel(cfg.channels["lobby"]), player.mention, names_in_lobby=lobby.get_all_names_in_lobby()) if player.match: await disp.RM_IN_MATCH.send(ctx) return if len(args) == 0: if player.is_timeout: await disp.RM_TIMEOUT_INFO.send( ctx, dt.utcfromtimestamp( player.timeout).strftime("%Y-%m-%d %H:%M UTC")) return await roles.role_update(player) await roles.perms_muted(False, player.id) await disp.RM_TIMEOUT_NO.send(ctx) return # =timeout @player remove if len(args) == 1 and args[0] == 'remove': player.timeout = 0 await player.db_update("timeout") await disp.RM_TIMEOUT_FREE.send(ctx, player.mention) await roles.role_update(player) await roles.perms_muted(False, player.id) return # Check if command is correct (=timeout @player 12 d) time = tools.time_calculator(" ".join(args)) if time == 0: await disp.RM_TIMEOUT_INVALID.send(ctx) return end_time = tools.timestamp_now() + time player.timeout = end_time await roles.role_update(player) await player.db_update("timeout") await roles.perms_muted(True, player.id) await disp.RM_TIMEOUT.send( ctx, player.mention, dt.utcfromtimestamp(end_time).strftime("%Y-%m-%d %H:%M UTC"))
async def reset(self, ctx): """ Join queue """ player = Player.get(ctx.message.author.id) if not player or (player and not player.is_lobbied): await disp.LB_NOT_IN.send(ctx) return lobby.reset_timeout(player) await disp.LB_REFRESHED.send(ctx)
async def rock_paper_scissors(self, ctx): """Starts up a game of rock paper scissors with another user""" authorize(ctx, "mentions") # check for a mentioned user p1 = Player.get(ctx.author.id) p2 = Player.get(ctx.message.mentions[0].id) # Ensure player is someone else if p1 == p2: raise UserInputError("You can't play against yourself") # Create new game embed = discord.Embed( title="Rock Paper Scissors", description= f"{p1.name} **VS** {p2.name}\n\nCheck DMs for how to play") await ctx.send(embed=embed) game = RPS(ctx.channel, p1, p2) await game.send_dms()
async def get_check_player(ctx): if len(ctx.message.mentions) != 1: await disp.RM_MENTION_ONE.send(ctx) return player = Player.get(ctx.message.mentions[0].id) if not player: # player isn't even registered in the system... await disp.RM_NOT_IN_DB.send(ctx) return return player
async def leave(self, ctx): """ Leave queue """ player = Player.get(ctx.message.author.id) if not player: await disp.LB_NOT_IN.send(ctx) return if player.is_lobbied: lobby.remove_from_lobby(player) await disp.LB_REMOVED.send(ctx, names_in_lobby=lobby.get_all_names_in_lobby()) return await disp.LB_NOT_IN.send(ctx)
async def on_stats(user): player = Player.get(user.id) if not player: await disp.NO_RULE.send(user, "stats", cfg.channels["rules"]) return log.info( f"Stats request from player id: [{player.id}], name: [{player.name}]") stat_player = await PlayerStat.get_from_database(player.id, player.name) recent_stats = await stat_processor.get_new_stats(Match, stat_player) await disp.DISPLAY_STATS.send(user, stats=stat_player, recent_stats=recent_stats)
async def shop_home(self, ctx): """Displays the home page of the shop""" p = ctx.prefix player = Player.get(ctx.author.id) home_embed = discord.Embed( title="Pandora's Shop", description=f"Balance: {player.pbucks}") home_embed.add_field( name="Banners", value=f"`{p}shop banners`", inline=False) home_embed.add_field( name="Titles", value=f"`{p}shop titles`", inline=False) home_embed.add_field( name="Cards", value=f"`{p}shop cards`", inline=False) await ctx.send(embed=home_embed)
async def on_ready(): # Initialise matches channels Match.init_channels(client, cfg.channels["matches"]) modules.roles.init(client) # Init signal handler modules.signal.init() # fetch rule message, remove all reaction but the bot's channel = client.get_channel(cfg.channels["rules"]) msg = await channel.fetch_message(channel.last_message_id) if msg.author.id == client.user.id: ctx = _interactions_handler.get_new_context(msg) await disp.RULES.edit(ctx) else: ctx = _interactions_handler.get_new_context(channel) await disp.RULES.send(ctx) # Update all players roles for p in Player.get_all_players_list(): await modules.roles.role_update(p) _add_main_handlers(client) if not modules.lobby.get_all_names_in_lobby(): try: last_lobby = modules.database.get_field( "restart_data", 0, "last_lobby") except KeyError: pass else: for p_id in last_lobby: try: player = Player.get(int(p_id)) if player and not modules.lobby.is_lobby_stuck( ) and player.is_registered: modules.lobby.add_to_lobby(player) except ValueError: pass modules.database.set_field("restart_data", 0, {"last_lobby": list()}) names = modules.lobby.get_all_names_in_lobby() if names: await disp.LB_QUEUE.send( ContextWrapper.channel(cfg.channels["lobby"]), names_in_lobby=modules.lobby.get_all_names_in_lobby()) modules.loader.unlock_all(client) log.info('Client is ready!') await disp.RDY.send(ContextWrapper.channel(cfg.channels["spam"]), cfg.VERSION)
async def escape(self, ctx): player = Player.get(ctx.author.id) if not player: await perms_muted(False, ctx.author.id) await remove_roles(ctx.author.id) return if player.is_timeout: await display.MUTE_SHOW.send( ctx, dt.utcfromtimestamp( player.timeout).strftime("%Y-%m-%d %H:%M UTC")) return await role_update(player) await display.MUTE_FREED.send(ctx) await perms_muted(False, player.id)
async def show_player_card(self, ctx, *, pid=None): """Shows a player's game card""" # Check message mentions if ctx.message.mentions: pid = ctx.message.mentions[0].id # Check user input elif pid and pid.isdigit(): pid = int(pid) else: pid = ctx.author.id # Get player and draw card of desired id card = Player.get(pid).draw_card() await ctx.send(file=to_discord_file(card))
async def swap(self, ctx, args): captain = None if not roles.is_admin(ctx.author): captain = await get_check_captain(ctx, self.match, check_turn=False) if not captain: return if len(ctx.message.mentions) != 2: await disp.SWAP_MENTION_2.send(ctx) return players = list() for mention in ctx.message.mentions: p = Player.get(mention.id) if not p: await disp.RM_NOT_IN_DB.send(ctx) return if not (p.match and p.active and p.match is self.match.proxy): await disp.SWAP_NO.send(ctx, p.mention) return if p.active.is_playing: p.active.team.captain.is_turn = True p.active.team.on_team_ready(False) players.append(p.active) if players[0].team is players[1].team: await disp.SWAP_SAME_TEAM.send(ctx) return # Can't have a sub command running at the same time self.factory.sub.on_clean() self.factory.bench.on_clean() if roles.is_admin(ctx.author): await self.validator.force_confirm(ctx, p_1=players[0], p_2=players[1]) return else: other_captain = self.match.teams[captain.team.id - 1].captain ctx = self.validator.arm(self.match.channel, captain, p_1=players[0], p_2=players[1]) await disp.SWAP_OK_CONFIRM.send(ctx, other_captain.mention)
async def set_player_banner(self, ctx, *, banner): """Set a players active banner.""" player = Player.get(ctx.author.id) all_banners = (f[0:-4] for f in os.listdir(f"assets{sep}banners")) banner = process.extractOne(banner, all_banners, score_cutoff=80) if not banner: raise commands.UserInputError( f"That banner doesn't exist. Check for banners with the `{ctx.prefix}shop banners` command." ) elif banner[0] not in player.banners: raise commands.UserInputError("You don't own that banner.") else: player.banner = banner[0] await ctx.send(content=f"Success:", file=to_discord_file(player.draw_banner())) db.update(player)
async def bench(self, ctx, args, bench): captain = None if not roles.is_admin(ctx.author): captain = await get_check_captain(ctx, self.match, check_turn=False) if not captain: return if len(ctx.message.mentions) != 1: await disp.BENCH_MENTION.send(ctx) return p = Player.get(ctx.message.mentions[0].id) if not p: await disp.RM_NOT_IN_DB.send(ctx) return if not (p.match and p.active and p.match is self.match.proxy): await disp.BENCH_NO.send(ctx, p.mention) return if bench and p.active.is_benched: await disp.BENCH_ALREADY.send(ctx) return if not bench and not p.active.is_benched: await disp.BENCH_NOT.send(ctx) return if p.active.is_playing: p.active.team.captain.is_turn = True p.active.team.on_team_ready(False) player = p.active # Can't have another command running at the same time self.factory.sub.on_clean() self.factory.swap.on_clean() if roles.is_admin(ctx.author): await self.validator.force_confirm(ctx, player=player, bench=bench) return else: other_captain = self.match.teams[captain.team.id - 1].captain ctx = self.validator.arm(self.match.channel, captain, player=player, bench=bench) if bench: await disp.BENCH_OK_CONFIRM.send(ctx, other_captain.mention) else: await disp.UNBENCH_OK_CONFIRM.send(ctx, other_captain.mention)
async def set_player_title(self, ctx, *, title): """Set a players active title""" player = Player.get(ctx.author.id) # Match an available title all_titles = Player.EXTRA_TITLES + Player.TITLES title = process.extractOne(title, all_titles, score_cutoff=80) if not title: raise commands.UserInputError( f"That title doesn't exist. You can set this as a custom title with X command" ) elif title[0] not in player.titles: raise commands.UserInputError("You don't own that title") else: player.title = title[0] await ctx.send(content=f"Success:", file=to_discord_file(player.draw_banner())) return db.update(player)
async def on_dm(message): # Check if too many requests from this user: if await spam_checker.is_spam(message.author, message.channel): return if message.content[:1] == "=": message.content = message.content[1:] if message.content.lower().startswith(("stat", "stats", "s")): await on_stats(message.author) elif message.content.lower().startswith(("modmail ", "dm ", "staff ")): i = message.content.index(' ') message.content = message.content[i + 1:] player = Player.get(message.author.id) await disp.BOT_DM.send(ContextWrapper.channel(cfg.channels["staff"]), player=player, msg=message) await disp.BOT_DM_RECEIVED.send(message.author) elif message.content.lower().startswith(("help", "h")): await disp.HELP.send(message.author, is_dm=True) spam_checker.unlock(message.author.id)
async def spam(self, ctx, *args): if len(args) == 1: arg = args[0] else: await disp.WRONG_USAGE.send(ctx, ctx.command.name) return if arg == "clear": spam_checker.clear_spam_list() await disp.SPAM_CLEARED.send(ctx) return if arg == "debug": all_spammers = spam_checker.debug() giga_string = "" for k in all_spammers.keys(): p = Player.get(k) if p: giga_string += f"\nSpammer: {p.mention}, id[{p.id}], name: [{p.name}], " \ f"spam value: [{all_spammers[k]}]" else: giga_string += f"\nSpammer: id[{k}], spam value: [{all_spammers[k]}]" await disp.SPAM_DEBUG.send(ctx, giga_string) return await disp.WRONG_USAGE.send(ctx, ctx.command.name)
async def psb(self, ctx, *args): if len(ctx.message.mentions) == 1: p_id = ctx.message.mentions[0].id else: await disp.RM_MENTION_ONE.send(ctx) return player = Player.get(p_id) if player: name = player.name else: name = "Unknown" stat_player = await PlayerStat.get_from_database(p_id, name) if stat_player.nb_matches_played == 0: await disp.NO_DATA.send(ctx) return req_date, usages = stat_processor.format_for_psb(stat_player, args) await disp.PSB_USAGE.send(ctx, stat_player.mention, req_date, player=stat_player, usages=usages)
async def show_player_banners(self, ctx): """Show a player's collected banners.""" player = Player.get(ctx.author.id) file = to_discord_file(player.draw_banners()) await ctx.send(content=f"{player.name}'s Banners", file=file)
async def launch(ctx, id_list, tier): print("TIER 1") players = list() for p_id in id_list: player = Player.get(p_id) if not player: print(f"user {p_id}") user = await bot.fetch_user(p_id) player = Player(user.id, user.name) await db.async_db_call(db.set_element, "users", player.id, player.get_data()) await player.register(None) players.append(player) for p in players: lobby.add_to_lobby(p) if tier == 1: return print("TIER 2") await asyncio.sleep(1) match = players[0].match while match.status is not MatchStatus.IS_CAPTAIN: await asyncio.sleep(1) cap_1_ctx = ContextWrapper.wrap(ctx.channel) cap_1_ctx.message = ctx.message cap_1_ctx.author = ctx.guild.get_member(players[0].id) await match.on_volunteer(players[0]) cap_2_ctx = ContextWrapper.wrap(ctx.channel) cap_2_ctx.message = ctx.message cap_2_ctx.author = ctx.guild.get_member(players[1].id) await match.on_volunteer(players[1]) if tier == 2: return print("TIER 3") while match.status is not MatchStatus.IS_PICKING: await asyncio.sleep(1) picked = ContextWrapper.user(players[2].id) cap_1_ctx.message.mentions.clear() cap_1_ctx.message.mentions.append(picked.author) await match.command.pick(cap_1_ctx, [""]) if tier == 3: return print("TIER 4") while match.status is not MatchStatus.IS_FACTION: await asyncio.sleep(1) cap_2_ctx.message.mentions.clear() cap_1_ctx.message.mentions.clear() await match.command.pick(cap_2_ctx, ["VS"]) await match.command.pick(cap_1_ctx, ["TR"]) if tier == 4: return print("TIER 5") while match.status is not MatchStatus.IS_BASING: await asyncio.sleep(1) # We assume tester is an admin await match.command.base(ctx, ["ceres"]) if tier == 5: return print("TIER 6") while match.status is not MatchStatus.IS_WAITING: await asyncio.sleep(1) match.change_check("online") match.change_check("account") await match.command.ready(cap_1_ctx) await match.command.ready(cap_2_ctx)
def __init__(self, id, net, match_id): self.player = Player.get(id) self.net = net self.match = match_id
async def on_status_update(user): player = Player.get(user.id) if not player: return await modules.roles.role_update(player)
async def on_member_join(member): player = Player.get(member.id) if not player: return await modules.roles.role_update(player)