async def poll(self, ctx, *, question): """Starts a poll format: poll question? opt1, opt2, opt3 or opt4... poll stop|end """ if question.lower().strip() in ['end', 'stop']: if ctx.message.channel in self.polls: await self.polls[ctx.message.channel].stop() else: await self.bot.say('There is no poll active in this channel') return if ctx.message.channel in self.polls: await self.bot.say('There\'s already an active poll in this channel') return match = re.search(r'^(.*?\?)\s*(.*?)$', question) if not match: await self.bot.say('Question could not be found.') return options = split(match.group(2)) question = formatter.escape_mentions(match.group(1)) poll = Poll(self.bot, ctx.message.channel, question, options, self.conf['polls']['duration'], self.polls) self.polls[ctx.message.channel] = poll await poll.start()
async def _add(self, ctx, *, regex): """adds a new replacement Format `s/old/new/` """ if ctx.message.author.id in self.permissions['rep-blacklist']: await self.bot.say(formatter.error('No ') + ':poi:') return #Find requested replacement rep = get_match(regex) #ensure that replace was found before proceeding if not rep: await self.bot.say(formatter.error('Could not find valid regex')) return p1 = formatter.escape_mentions(rep.group(2)) p2 = formatter.escape_mentions(rep.group(4)) #check regex for validity if not comp(p1, p2): await self.bot.say(formatter.error('regex is invalid')) return #make sure that there are no similar regexes in db for i in self.replacements: if similar(p1, i): r = '\"{}\" -> \"{}\"'.format(i, self.replacements[i][0]) message = 'Similar regex already exists, delete or edit it\n{}'.format( formatter.inline(r)) await self.bot.say(formatter.error(message)) return #make sure regex is not too broad if bad_re(p1): await self.bot.say(formatter.error('regex is too broad')) return #check that regex does not already exist if p1 in self.replacements: await self.bot.say(formatter.error('regex already exists')) return self.replacements[p1] = [p2, ctx.message.author.id] await self.bot.say(formatter.ok())
async def start(self): message = 'Poll stated: \"{}\"\n{}'.format(self.question, '\n'.join(self.options)) await self.bot.say(formatter.escape_mentions(message)) self.ongoing = True await asyncio.sleep(self.sleep) if self.ongoing: await self.stop()
async def _edit(self, ctx, *, regex): """edits an existing replacement Format `s/old/new/` """ #Find requested replacement rep = get_match(regex) #ensure that replace was found before proceeding if not rep: await self.bot.say(formatter.error('Could not find valid regex')) return p1 = formatter.escape_mentions(rep.group(2)) p2 = formatter.escape_mentions(rep.group(4)) #check regex for validity if not comp(p1, p2): await self.bot.say(formatter.error('regex is invalid')) return #make sure regex is not too broad if bad_re(p1): await self.bot.say(formatter.error('regex is too broad')) return #ensure that replace was found before proceeding if p1 not in self.replacements: await self.bot.say(formatter.error('Regex not in replacements.')) return #check if they have correct permissions if ctx.message.author.id != self.replacements[p1][1] \ and not perms.is_owner_check(ctx.message): #will uncomment next line when reps are a per server thing #and not perms.check_permissions(ctx.message, manage_messages=True): raise commands.errors.CheckFailure('Cannot edit') self.replacements[p1] = [p2, ctx.message.author.id] await self.bot.say(formatter.ok())
async def _rm(self, ctx, *, pattern): """remove an existing replacement""" #pattern = re.sub('^(`)?\\(\\?[^\\)]*\\)', '\\1', pattern) pattern = formatter.escape_mentions(pattern) #ensure that replace was found before proceeding if pattern not in self.replacements: if re.search('^`.*`$', pattern) and pattern[1:-1] in self.replacements: pattern = pattern[1:-1] else: await self.bot.say( formatter.error('Regex not in replacements.')) return #check if they have correct permissions if ctx.message.author.id != self.replacements[pattern][1] \ and not perms.is_owner_check(ctx.message): raise commands.errors.CheckFailure('Cannot delete') self.replacements.pop(pattern) self.replacements.save() await self.bot.say(formatter.ok())
async def end(self, bot): chan = bot.get_channel(self.channel_id) await bot.send_message(chan, formatter.escape_mentions(self.results()))
async def begin(self, bot): message = 'Poll stated: \"{}\"\n{}'.format(self.question, '\n'.join(self.options)) await bot.say(formatter.escape_mentions(message))
async def stop(self): self.ongoing = False await self.bot.say(formatter.escape_mentions(self.results())) self.polls.pop(self.channel)