# Economy Admin Tools Module # Contains administrative and moderation commands from the original economy.py import logging from discord.ext import commands from discord import app_commands import discord import datetime from typing import Optional # Import specific utilities needed by this module from api_calls import db_get, db_post, db_delete from helpers.constants import IMAGES from helpers.discord_utils import get_channel, get_team_embed, send_to_channel from helpers import ( give_cards_to_team, get_team_by_owner, refresh_sheet, display_cards, get_test_pack ) logger = logging.getLogger('discord_app') class AdminTools(commands.Cog): """Administrative and moderation commands for Paper Dynasty.""" def __init__(self, bot): self.bot = bot @commands.command(name='mlbteam', help='Mod: Load MLB team data') @commands.is_owner() async def mlb_team_command( self, ctx: commands.Context, abbrev: str, sname: str, lname: str, gmid: int, gmname: str, gsheet: str, logo: str, color: str, ranking: int): # Check for duplicate team data dupes = await db_get('teams', params=[('abbrev', abbrev)]) if dupes['count']: await ctx.send( f'Yikes! {abbrev.upper()} is a popular abbreviation - it\'s already in use by the ' f'{dupes["teams"][0]["sname"]}. No worries, though, you can run the `/newteam` command again to get ' f'started!' ) return # Check for duplicate team data dupes = await db_get('teams', params=[('lname', lname)]) if dupes['count']: await ctx.send( f'Yikes! {lname.title()} is a popular name - it\'s already in use by ' f'{dupes["teams"][0]["abbrev"]}. No worries, though, you can run the `/newteam` command again to get ' f'started!' ) return current = await db_get('current') team = await db_post('teams', payload={ 'abbrev': abbrev.upper(), 'sname': sname, 'lname': lname, 'gmid': gmid, 'gmname': gmname, 'gsheet': gsheet, 'season': current['season'], 'wallet': 100, 'ranking': ranking, 'color': color if color else 'a6ce39', 'logo': logo if logo else None, 'is_ai': True }) p_query = await db_get('players', params=[('franchise', lname)]) this_pack = await db_post( 'packs/one', payload={'team_id': team['id'], 'pack_type_id': 2, 'open_time': datetime.datetime.timestamp(datetime.datetime.now())*1000} ) team_players = p_query['players'] + p_query['players'] await db_post('cards', payload={'cards': [ {'player_id': x['player_id'], 'team_id': team['id'], 'pack_id': this_pack['id']} for x in team_players] }, timeout=10) embed = get_team_embed(f'{team["lname"]}', team) await ctx.send(content=None, embed=embed) @commands.hybrid_command(name='mlb-update', help='Distribute MLB cards to AI teams') @commands.is_owner() async def mlb_update_command(self, ctx: commands.Context): ai_teams = await db_get('teams', params=[('is_ai', True)]) if ai_teams['count'] == 0: await ctx.send(f'I could not find any AI teams.') return total_cards = 0 total_teams = 0 for team in ai_teams['teams']: all_players = await db_get('players', params=[('franchise', team['lname'])]) new_players = [] if all_players: for player in all_players['players']: owned_by_team_ids = [entry['team'] for entry in player['paperdex']['paperdex']] if team['id'] not in owned_by_team_ids: new_players.append(player) if new_players: await ctx.send(f'Posting {len(new_players)} new cards for {team["gmname"]}\'s {team["sname"]}...') total_cards += len(new_players) total_teams += 1 this_pack = await db_post( 'packs/one', payload={'team_id': team['id'], 'pack_type_id': 2, 'open_time': datetime.datetime.timestamp(datetime.datetime.now()) * 1000} ) await db_post('cards', payload={'cards': [ {'player_id': x['player_id'], 'team_id': team['id'], 'pack_id': this_pack['id']} for x in new_players ]}, timeout=10) await refresh_sheet(team, self.bot) await ctx.send(f'All done! I added {total_cards} across {total_teams} teams.') @commands.hybrid_command(name='give-card', help='Mod: Give free card to team') # @commands.is_owner() @commands.has_any_role("PD Gift Players") async def give_card_command(self, ctx, player_ids: str, team_abbrev: str): if ctx.channel.name in ['paper-dynasty-chat', 'pd-news-ticker', 'pd-network-news']: await ctx.send(f'Please head to down to {get_channel(ctx, "pd-bot-hole")} to run this command.') return question = await ctx.send(f'I\'ll go put that card on their roster...') all_player_ids = player_ids.split(" ") t_query = await db_get('teams', params=[('abbrev', team_abbrev)]) if not t_query['count']: await ctx.send(f'I could not find {team_abbrev}') return team = t_query['teams'][0] this_pack = await db_post( 'packs/one', payload={ 'team_id': team['id'], 'pack_type_id': 4, 'open_time': datetime.datetime.timestamp(datetime.datetime.now()) * 1000} ) try: await give_cards_to_team(team, player_ids=all_player_ids, pack_id=this_pack['id']) except Exception as e: logger.error(f'failed to create cards: {e}') raise ConnectionError(f'Failed to distribute these cards.') await question.edit(content=f'Alrighty, now I\'ll refresh their sheet...') await refresh_sheet(team, self.bot) await question.edit(content=f'All done!') await send_to_channel( self.bot, channel_name='commissioners-office', content=f'Just sent {len(all_player_ids)} players to {ctx.message.author.mention}:\n{all_player_ids}' ) @commands.command(name='cleartest', hidden=True) @commands.is_owner() async def clear_test_command(self, ctx): team = await get_team_by_owner(ctx.author.id) msg = await ctx.send('Alright, let\'s go find your cards...') all_cards = await db_get( 'cards', params=[('team_id', team['id'])] ) if all_cards: await msg.edit(content=f'I found {len(all_cards["cards"])} cards; deleting now...') for x in all_cards['cards']: await db_delete( 'cards', object_id=x['id'] ) await msg.edit(content=f'All done with cards. Now I\'ll wipe out your packs...') p_query = await db_get('packs', params=[('team_id', team['id'])]) if p_query['count']: for x in p_query['packs']: await db_delete('packs', object_id=x['id']) await msg.edit(content=f'All done with packs. Now I\'ll wipe out your paperdex...') p_query = await db_get('paperdex', params=[('team_id', team['id'])]) if p_query['count']: for x in p_query['paperdex']: await db_delete('paperdex', object_id=x['id']) await msg.edit(content=f'All done with paperdex. Now I\'ll wipe out your team...') if db_delete('teams', object_id=team['id']): await msg.edit(content=f'All done!') @commands.command(name='packtest', hidden=True) @commands.is_owner() async def pack_test_command(self, ctx): team = await get_team_by_owner(ctx.author.id) await display_cards( await get_test_pack(ctx, team), team, ctx.channel, ctx.author, self.bot, pack_cover=IMAGES['pack-sta'], pack_name='Standard Pack' ) async def setup(bot): """Setup function for the AdminTools cog.""" await bot.add_cog(AdminTools(bot))