async def character_delete(self, ctx, *, name): """Deletes a character.""" user_characters = await self.bot.mdb.characters.find({"owner": str(ctx.author.id)}).to_list(None) if not user_characters: return await ctx.send('You have no characters.') _character = await search_and_select(ctx, user_characters, name, lambda e: e.get('stats', {}).get('name', ''), selectkey=lambda e: f"{e['stats'].get('name', '')} (`{e['upstream']}`)") name = _character.get('stats', {}).get('name', 'Unnamed') char_url = _character['upstream'] await ctx.send('Are you sure you want to delete {}? (Reply with yes/no)'.format(name)) try: reply = await self.bot.wait_for('message', timeout=30, check=auth_and_chan(ctx)) except asyncio.TimeoutError: reply = None reply = get_positivity(reply.content) if reply is not None else None if reply is None: return await ctx.send('Timed out waiting for a response or invalid response.') elif reply: # _character = Character(_character, char_url) # if _character.get_combat_id() is not None: # combat = await Combat.from_id(_character.get_combat_id(), ctx) # me = next((c for c in combat.get_combatants() if getattr(c, 'character_id', None) == char_url), # None) # if me: # combat.remove_combatant(me, True) # await combat.commit() await self.bot.mdb.characters.delete_one({"owner": str(ctx.author.id), "upstream": char_url}) return await ctx.send('{} has been deleted.'.format(name)) else: return await ctx.send("OK, cancelling.")
async def character_delete(self, ctx, *, name): """Deletes a character.""" user_characters = await self.bot.mdb.characters.find( { "owner": str(ctx.author.id) }, ['name', 'upstream']).to_list(None) if not user_characters: return await ctx.send('You have no characters.') selected_char = await search_and_select( ctx, user_characters, name, lambda e: e['name'], selectkey=lambda e: f"{e['name']} (`{e['upstream']}`)") await ctx.send( f"Are you sure you want to delete {selected_char['name']}? (Reply with yes/no)" ) try: reply = await self.bot.wait_for('message', timeout=30, check=auth_and_chan(ctx)) except asyncio.TimeoutError: reply = None reply = get_positivity(reply.content) if reply is not None else None if reply is None: return await ctx.send( 'Timed out waiting for a response or invalid response.') elif reply: await Character.delete(ctx, str(ctx.author.id), selected_char['upstream']) return await ctx.send(f"{selected_char['name']} has been deleted.") else: return await ctx.send("OK, cancelling.")
async def uvar_deleteall(self, ctx): """Deletes ALL user variables.""" await ctx.send("This will delete **ALL** of your user variables (uvars). " "Are you *absolutely sure* you want to continue?\n" "Type `Yes, I am sure` to confirm.") reply = await self.bot.wait_for('message', timeout=30, check=lambda m: auth_and_chan(ctx)(m)) if (not reply) or (not reply.content == "Yes, I am sure"): return await ctx.send("Unconfirmed. Aborting.") await self.bot.mdb.uvars.delete_many({"owner": str(ctx.author.id)}) return await ctx.send("OK. I have deleted all your uvars.")
async def alias_deleteall(self, ctx): """Deletes ALL user aliases.""" await ctx.send("This will delete **ALL** of your personal user aliases " "(it will not affect workshop subscriptions). " "Are you *absolutely sure* you want to continue?\n" "Type `Yes, I am sure` to confirm.") reply = await self.bot.wait_for('message', timeout=30, check=auth_and_chan(ctx)) if not reply.content == "Yes, I am sure": return await ctx.send("Unconfirmed. Aborting.") await self.bot.mdb.aliases.delete_many({"owner": str(ctx.author.id)}) return await ctx.send("OK. I have deleted all your aliases.")
async def _confirm_overwrite(self, ctx, _id): """Prompts the user if command would overwrite another character. Returns True to overwrite, False or None otherwise.""" conflict = await self.bot.mdb.characters.find_one({"owner": str(ctx.author.id), "upstream": _id}) if conflict: await ctx.channel.send( "Warning: This will overwrite a character with the same ID. Do you wish to continue (reply yes/no)?\n" f"If you only wanted to update your character, run `{ctx.prefix}update` instead.") try: reply = await self.bot.wait_for('message', timeout=30, check=auth_and_chan(ctx)) except asyncio.TimeoutError: reply = None replyBool = get_positivity(reply.content) if reply is not None else None return replyBool return True
async def cvar_deleteall(self, ctx): """Deletes ALL character variables for the active character.""" char: Character = await Character.from_ctx(ctx) await ctx.send(f"This will delete **ALL** of your character variables for {char.name}. " "Are you *absolutely sure* you want to continue?\n" "Type `Yes, I am sure` to confirm.") try: reply = await self.bot.wait_for('message', timeout=30, check=auth_and_chan(ctx)) except asyncio.TimeoutError: reply = None if (not reply) or (not reply.content == "Yes, I am sure"): return await ctx.send("Unconfirmed. Aborting.") char.cvars = {} await char.commit(ctx) return await ctx.send(f"OK. I have deleted all of {char.name}'s cvars.")
async def repl(self, ctx): msg = ctx.message variables = { 'ctx': ctx, 'bot': self.bot, 'message': msg, 'guild': msg.guild, 'channel': msg.channel, 'author': msg.author } if msg.channel.id in self.sessions: await ctx.send('Already running a REPL session in this channel. Exit it with `quit`.') return self.sessions.add(msg.channel.id) await ctx.send('Enter code to execute or evaluate. `exit()` or `quit` to exit.') while True: response = await self.bot.wait_for('message', check=lambda m: m.content.startswith('`') and auth_and_chan(ctx)(m)) cleaned = self.cleanup_code(response.content) if cleaned in ('quit', 'exit', 'exit()'): await ctx.send('Exiting.') self.sessions.remove(msg.channel.id) return executor = exec if cleaned.count('\n') == 0: # single statement, potentially 'eval' try: code = compile(cleaned, '<repl session>', 'eval') except SyntaxError: pass else: executor = eval if executor is exec: try: code = compile(cleaned, '<repl session>', 'exec') except SyntaxError as e: await ctx.send(self.get_syntax_error(e)) continue variables['message'] = response fmt = None stdout = io.StringIO() try: with redirect_stdout(stdout): result = executor(code, variables) if inspect.isawaitable(result): result = await result except Exception as e: value = stdout.getvalue() fmt = '```py\n{}{}\n```'.format(value, traceback.format_exc()) else: value = stdout.getvalue() if result is not None: fmt = '```py\n{}{}\n```'.format(value, result) variables['_'] = result elif value: fmt = '```py\n{}\n```'.format(value) try: if fmt is not None: if len(fmt) > 2000: await msg.channel.send('Content too big to be printed.') else: await msg.channel.send(fmt) except discord.Forbidden: pass except discord.HTTPException as e: await msg.channel.send('Unexpected error: `{}`'.format(e))