diff --git a/.idea/dictionaries/Dustin_Pianalto.xml b/.idea/dictionaries/Dustin_Pianalto.xml index 21a19af..3787925 100644 --- a/.idea/dictionaries/Dustin_Pianalto.xml +++ b/.idea/dictionaries/Dustin_Pianalto.xml @@ -2,15 +2,21 @@ aiohttp + aren asctime chans coro customsearch + dinos emojis exts levelname lockdown msecs + shootergame + shootergamemode + singleplayer + strftime \ No newline at end of file diff --git a/exts/imports/process_files.py b/exts/imports/process_files.py index 081127b..bac47d1 100644 --- a/exts/imports/process_files.py +++ b/exts/imports/process_files.py @@ -112,3 +112,13 @@ def generate_dino_files(dino_data, directory): int(dino['Dino Data']['DinoID2'])) with open(f'{directory}/{filename}', 'w') as f: dino.write(f, space_around_delimiters=False) + + +def generate_files(storage_dir, ctx, filename, game_ini, dinos_data, mods): + if not os.path.isdir(f'{storage_dir}/{ctx.author.id}'): + os.mkdir(f'{storage_dir}/{ctx.author.id}') + directory = f'{storage_dir}/{ctx.author.id}/{filename}_' \ + f'{ctx.message.created_at.strftime("%Y%m%dT%H%M%S")}' + os.mkdir(directory) + generate_game_ini(game_ini, mods, directory) + generate_dino_files(dinos_data, directory) diff --git a/exts/imports/utils.py b/exts/imports/utils.py index 2ca3c86..b6ec8f4 100644 --- a/exts/imports/utils.py +++ b/exts/imports/utils.py @@ -82,3 +82,21 @@ async def run_command(args): stdout, stderr = await process.communicate() # Return stdout return stdout.decode().strip() + + +async def git_add(loop, directory, file): + return await asyncio.wait_for(loop.create_task(run_command(f'git -C {directory} add {file}')), 120) + + +async def git_commit(loop, directory, message): + return await asyncio.wait_for(loop.create_task(run_command(f'git -C {directory} commit -m {message}')), 120) + + +async def git_push(loop, directory): + result = await asyncio.wait_for(loop.create_task(run_command(f'git -C {directory} push')), 240) + return result if 'error: failed' in result else 'Completed' + + +async def git_pull(loop, directory): + result = await asyncio.wait_for(loop.create_task(run_command(f'git -C {directory} pull')), 240) + return result if 'CONFLICT' in result else 'Completed' diff --git a/exts/uploader.py b/exts/uploader.py index e474b22..54c7ad6 100644 --- a/exts/uploader.py +++ b/exts/uploader.py @@ -1,11 +1,12 @@ import discord from discord.ext import commands from io import BytesIO -from .imports import process_files +from .imports import process_files, utils from configparser import ConfigParser import os storage_dir = '../ASB_dino_submissions' +owner_id = 351794468870946827 class Uploader: @@ -14,40 +15,64 @@ class Uploader: @commands.command(name='upload', aliases=['submit']) async def upload_dino(self, ctx, official: str='unofficial', singleplayer: bool=False): + msg = await ctx.send('Processing... Please Wait') if ctx.message.attachments: attachment = ctx.message.attachments[0] if attachment.filename.endswith('.zip'): - with BytesIO() as file: - await attachment.save(file) - unzipped = process_files.load_zip(file) - game_ini, dinos_data, mods = process_files.process_files(unzipped) - if dinos_data == dict(): - await ctx.send('There aren\'t any DinoExport files in the zip file attached.\n' - 'Please make sure the files have not been renamed.') - else: - if official == 'unofficial' and game_ini == ConfigParser(): - await ctx.send('Game.ini is missing or is not valid.') - return - elif official == 'official' and game_ini == ConfigParser(): - if singleplayer: - game_ini.add_section('/script/shootergame.shootergamemode') - game_ini.set('/script/shootergame.shootergamemode', 'bUseSingleplayerSettings', True) - elif official not in ['official', 'unofficial']: - await ctx.send(f'{official} is not a valid option. Please specify "official" or ' - f'"unofficial" or leave it blank to default to "unofficial"') - return - if not os.path.isdir(f'{storage_dir}/{ctx.author.id}'): - os.mkdir(f'{storage_dir}/{ctx.author.id}') - directory = f'{storage_dir}/{ctx.author.id}/{attachment.filename}_' \ - f'{ctx.message.created_at.strftime("%Y%m%dT%H%M%S")}' - os.mkdir(directory) - process_files.generate_game_ini(game_ini, mods, directory) - process_files.generate_dino_files(dinos_data, directory) - await ctx.send('Upload complete.') + async with ctx.typing(): + with BytesIO() as file: + await attachment.save(file) + unzipped = process_files.load_zip(file) + game_ini, dinos_data, mods = process_files.process_files(unzipped) + if dinos_data == dict(): + await msg.edit(content='There aren\'t any DinoExport files in the zip file attached.\n' + 'Please make sure the files have not been renamed.') + else: + if official == 'unofficial' and game_ini == ConfigParser(): + await msg.edit(content='Game.ini is missing or is not valid.') + return + elif official == 'official' and game_ini == ConfigParser(): + if singleplayer: + game_ini.add_section('/script/shootergame.shootergamemode') + game_ini.set('/script/shootergame.shootergamemode', + 'bUseSingleplayerSettings', + True) + elif official not in ['official', 'unofficial']: + await msg.edit(content=f'{official} is not a valid option. Please specify "official" ' + f'or "unofficial" or leave it blank to default to "unofficial"') + return + await msg.edit('Processing... Syncing with GitHub') + pull_status = utils.git_pull(self.bot.loop, storage_dir) + if pull_status == 'Completed': + await msg.edit('Processing... Sync complete... Generating new files') + process_files.generate_files(storage_dir, ctx, attachment.filename, + game_ini, dinos_data, mods) + await msg.edit('Processing... Files generated... Committing changes') + utils.git_add(self.bot.loop, storage_dir, '*') + utils.git_commit(self.bot.loop, + storage_dir, + f'Uploaded {len(dinos_data)} dinos {official}') + await msg.edit('Processing... Committed... Pushing files to GitHub') + push_status = utils.git_push(self.bot.loop, storage_dir) + if push_status == 'Completed': + await msg.edit(content=f'{ctx.author.mention} Upload complete.') + else: + await self.bot.get_user(owner_id).send(f'There was an error with git push' + f'\n{push_status}') + await msg.edit(content='There was an error pushing the files to GitHub\n' + 'Dusty.P has been notified and will get this fixed') + else: + await self.bot.get_user(owner_id).send(f'There was an error with git pull\n' + f'{pull_status}') + process_files.generate_files('submissions_temp', ctx, attachment.filename, + game_ini, dinos_data, mods) + await msg.edit(content='Could not sync with GitHub.\n' + 'Dusty.P has been notified and your files are stored in a ' + 'temporary location') else: - await ctx.send('Please attach a zip file to the command.') + await msg.edit(content='Please attach a zip file to the command.') else: - await ctx.send('Please attach a zip file to the command.') + await msg.edit(content='Please attach a zip file to the command.') def setup(bot):