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 snippet_deleteall(self, ctx): """Deletes ALL user snippets.""" await ctx.send("This will delete **ALL** of your user snippets. " "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.content == "Yes, I am sure": return await ctx.send("Unconfirmed. Aborting.") await self.bot.mdb.snippets.delete_many({"owner": str(ctx.author.id)}) return await ctx.send("OK. I have deleted all your snippets.")
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 _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 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))