def test_sendHelp(self): commands = [ "hello", "help", "announce", "binary", "hex", "magic", "poll", "remind", "roll", "ask", "choose", "joke", "react", "rate", "cute" ] empty = ("Here are my commands:\n" + h.blockQuote( "**Information:** `!hello` `!help [[X]]`" ) + h.blockQuote( "**Useful:** `!announce [[X|...]]` `!binary [[X]]` `!hex [[X]]` `!magic [[X|...]]` `!poll [[X|Y|...]]` `!remind [[X|...]]` `!roll [[X]]` `!wiki [[X]]`" ) + h.blockQuote( "**Fun:** `!ask` `!choose [[X|Y|...]]` `!cute` `!joke` `!react` `!rate`" ) + "\n" + c.HELP_REMINDER) msg = m.MockMessage() msg.content = "" self.assertEqual(i.sendHelp(msg), empty) msg.content = "[[]]" self.assertRaises(err.EmptyArgumentException, i.sendHelp, msg) msg.content = "[[x|y]]" self.assertRaises(err.TooManyArgumentsException, i.sendHelp, msg) for o in commands: msg.content = "[[%s]]" % o self.assertIsNotNone(i.sendHelp(msg)) msg.content = "[[!%s]]" % o self.assertIsNotNone(i.sendHelp(msg)) msg.content = ("[[!%s]]" % o).upper() self.assertIsNotNone(i.sendHelp(msg)) msg.content = "[[fake_command]]" self.assertRaises(err.CustomCommandException, i.sendHelp, msg)
def finishPoll(message, poll): results = {} toReturn = c.POLL_END % (poll.author, c.LINE_BREAK + poll.question) for r in message.reactions: if r.emoji in poll.options: results[r.emoji] = r.count - 1 totalVotes = sum(results.values()) for r in results: if totalVotes == 0: percent = 0 else: percent = int((results[r] / totalVotes) * 100) bar = "\N{BLACK SQUARE}" * int(percent / 5) spaces = ' ' * (20 - len(bar)) toReturn = (toReturn + "\n" + h.blockQuote(poll.options[r]) + h.blockQuote("`[%s%s]` %s votes, %s%%" % (bar, spaces, results[r], percent))) toReturn = (toReturn + c.LINE_BREAK + "Poll link: " + message.jump_url) return toReturn
def confirmReminder(message, timer): if isinstance(timer, str): #Something went wrong, so an error was sent in return timer else: if timer.dt: formattedTime = h.formatTime(timer.duration - h.getTime()) else: formattedTime = h.formatTime(timer.duration) if isinstance(timer, reminder.Announcement): confirmation = c.ANNOUNCE_CONFIRM_1 % (h.escapeCodeBlock( message.author.display_name), formattedTime) reminderText = h.escapeCodeBlock( re.split("@everyone ", timer.message, maxsplit=1)[1]) else: confirmation = c.REMIND_CONFIRM_1 % (h.escapeCodeBlock( message.author.display_name), formattedTime) reminderText = h.escapeCodeBlock( re.split("\<*\> ", timer.message, maxsplit=1)[1]) if reminderText == "": reminderText = "*No message was given*" return (confirmation + h.blockQuote(reminderText) + "\n" + c.REMIND_CONFIRM_2)
def sendHelp(message): cmdList = h.getXmlTree('commands') toReturn = "" #No attempted arguments sent, so send list of commands if re.search("\[|\]", message.content) == None: toReturn = "Here are my commands:\n" info = {"name": "**Information:**", "inline": "true", "value": ""} useful = {"name": "**Useful:**", "inline": "true", "value": ""} fun = {"name": "**Fun:**", "inline": "true", "value": ""} for cmd in cmdList.findall("./function"): if not h.isDisabled(cmd.find("command").text): if cmd.get("category") == "information": info['value'] = info['value'] + " `{}`\n".format( cmd.find("format").text) elif cmd.get("category") == "useful": useful['value'] = useful['value'] + " `{}`\n".format( cmd.find("format").text) elif cmd.get("category") == "fun": fun['value'] = fun['value'] + " `{}`\n".format( cmd.find("format").text) toReturn = embeddable.Embeddable() toReturn.addField(info, useful, fun) toReturn.setFooter(c.HELP_REMINDER) else: try: #Specific request arg = h.parseArgs(message.content, "help", 1, 1) for i in set(arg): arg = i.lower() #Change back to string if (arg.startswith("!")): arg = arg[1:] cmd = cmdList.find("./function/[command='{}']".format(arg)) if cmd == None: raise errors.CustomCommandException("help", "bad_command") elif h.isDisabled(cmd.find("command").text): toReturn = "Sorry, the command you're trying to find help for is disabled." else: toReturn = "Here's how to use `!{}`:\n".format( cmd.find("command").text) for b in cmd.findall("body"): toReturn = toReturn + "\n" + b.find( "description").text + "\n" for i in b.findall("hint"): toReturn = toReturn + h.blockQuote( "\N{BULLET} " + h.formatProps(cmd.find("command").text, i.text)) toReturn = embeddable.empty(toReturn) except errors.Error as e: raise e return toReturn
def test_sayHello(self): sender = type("sender", (), {"display_name": ""}) uptime = h.getTime() sender.display_name = "Test Name" time = h.formatTime(0) msg = ((c.HELLO % sender.display_name) + "\n" + h.blockQuote("**Current version:** %s" % c.VERSION) + h.blockQuote("**Current uptime:** %s" % h.formatTime(h.getTime(), offset=uptime))) self.assertEqual(i.sayHello(sender, h.getTime()), msg) sender.display_name = "" time = h.formatTime(0) msg = ((c.HELLO % sender.display_name) + "\n" + h.blockQuote("**Current version:** %s" % c.VERSION) + h.blockQuote("**Current uptime:** %s" % h.formatTime(h.getTime(), offset=uptime))) self.assertEqual(i.sayHello(sender, h.getTime()), msg)
def test_blockQuote(self): msg = "> test\n" self.assertEqual(h.blockQuote("test"), msg) msg = "> \n" self.assertEqual(h.blockQuote(""), msg)