async def debug(self, ctx, *, code : str): """Evaluates code.""" code = code.strip('` ') result = None env = { 'bot': self.bot, 'ctx': ctx, 'message': ctx.message, 'server': ctx.message.server, 'channel': ctx.message.channel, 'author': ctx.message.author } env.update(globals()) try: result = eval(code, env) if inspect.isawaitable(result): result = await result except Exception as e: await self.bot.say(formatter.code(type(e).__name__ + ': ' + str(e))) return await self.bot.say(formatter.code(result, 'py'))
async def _ls(self): """list existing replacements""" msg = '' for rep in self.replacements: msg += '\"{}\" -> \"{}\"\n'.format(rep, self.replacements[rep][0]) msg = msg[:-1] await self.bot.say(formatter.code(msg))
def results(self): out = '' formatting = '{{:<{}}} - {{:>{}}}\n' longest = [0, 0] for i in self.options: if len(i) > longest[0]: longest[0] = len(i) if len(str(len(self.options[i]))) > longest[1]: longest[1] = len(str(len(self.options[i]))) formatting = formatting.format(*longest) for i in self.options: out += formatting.format(i, len(self.options[i])) return '**{}**:\n'.format(self.question) + formatter.code(out[:-1])
async def _roll(self, ctx, *dice): """rolls dice given pattern [Nd]S[(+|-)C] N: number of dice to roll S: side on the dice C: constant to add or subtract from each die roll """ loop = asyncio.get_event_loop() roll = '\n'.join(await loop.run_in_executor(None, self.rolls, dice)) message = ctx.message.author.mention + ':\n' if '\n' in roll: message += formatter.code(roll) else: message += formatter.inline(roll) await self.bot.say(message)
async def quotes(self, ctx): """Manage quotes""" if ctx.invoked_subcommand is None: message = ctx.message.content try: index = int(ctx.subcommand_passed) if index >= len(self.quotes_dict['quotes']): await self.bot.say( formatter.error( 'Quote {} does not exist'.format(index))) else: quote = self.quotes_dict['quotes'][index] message = 'On {}:\n{}'.format( quote['date'], formatter.code(quote['quote'])) await self.bot.say(message) except: await self.bot.say(self._random(message))
def _random(self, message): quotes = self.quotes_dict['quotes'] message = message.split()[1:] if message: quotes = [] for quote in self.quotes_dict['quotes']: ok = True for w in message: if w.lower() not in quote['quote'].lower(): ok = False break if ok: quotes.append(quote) if not quotes: return formatter.error('No quotes found') quote = random.choice(quotes) return 'On {}:\n{}'.format(quote['date'], formatter.code(quote['quote']))