def test_long(self): bot = Bot("app_name", "version") bot.register_command("test1", None, None, "long desc") message = AsyncMock() self._await(bot.help(None, message, "help", "test1")) message.channel.send.assert_awaited_once_with("long desc", reference=message, mention_author=False)
def test_add_regex(self): bot = Bot("app_name", "version") self.assertEqual(2, len(bot._Bot__commands)) callback = AsyncMock() bot.register_command("^t[eo]a?st$", callback, "short", "long") self.assertEqual(3, len(bot._Bot__commands)) cmd = bot._Bot__commands[0] self.assertEqual("^t[eo]a?st$", cmd.regex) self.assertEqual(callback, cmd.compute) self.assertEqual("short", cmd.help_short) self.assertEqual("long", cmd.help_long)
def test_no_mention(self): bot = Bot("app_name", "version") bot.enforce_write_permission = False simple_callback = AsyncMock() bot.register_command("test", simple_callback, "short", "long") watcher_callback = AsyncMock() bot.register_watcher(watcher_callback) fallback_callback = AsyncMock() bot.register_fallback(fallback_callback) message = AsyncMock() message.content = "hello there" self._await(bot.on_message(message)) simple_callback.assert_not_awaited() watcher_callback.assert_awaited_once_with(bot.client, message) fallback_callback.assert_not_awaited()
def test_list_functions(self): bot = Bot("app_name", "version") bot.register_command("", None, "test1: desc1", None) bot.register_command("", None, "test2: desc2", None) message = AsyncMock() self._await(bot.help(None, message, "help")) message.channel.send.assert_awaited_once_with( f"```\n" f"List of available commands:\n" f"* test2: desc2\n" f"* test1: desc1\n" f"* info: show description\n" f"* help: show this help\n" f"```", reference=message, mention_author=False, )
def test_mention_self(self): bot = Bot("app_name", "version") bot.enforce_write_permission = False bot.client.user.id = "12345" simple_callback = AsyncMock() bot.register_command("test", simple_callback, "short", "long") watcher_callback = AsyncMock() bot.register_watcher(watcher_callback) fallback_callback = AsyncMock() bot.register_fallback(fallback_callback) message = AsyncMock() message.content = "<@12345> test arg0 arg1" message.author = bot.client.user self._await(bot.on_message(message)) simple_callback.assert_not_awaited() watcher_callback.assert_not_awaited() fallback_callback.assert_not_awaited()
def test_mention_command_regex(self): bot = Bot("app_name", "version") bot.enforce_write_permission = False bot.client.user.id = "12345" regex_callback = AsyncMock() bot.register_command("^t[eo]a?st$", regex_callback, "short", "long") watcher_callback = AsyncMock() bot.register_watcher(watcher_callback) fallback_callback = AsyncMock() bot.register_fallback(fallback_callback) message = AsyncMock() message.content = "<@12345> toast hey" self._await(bot.on_message(message)) regex_callback.assert_awaited_once_with(bot.client, message, "toast", "hey") watcher_callback.assert_awaited_once_with(bot.client, message) fallback_callback.assert_not_awaited()
def test_lower_command_names_off(self): bot = Bot("app_name", "version") bot.enforce_write_permission = False bot.lower_command_names = False bot.client.user.id = "12345" simple_callback = AsyncMock() bot.register_command("test", simple_callback, "short", "long") watcher_callback = AsyncMock() bot.register_watcher(watcher_callback) fallback_callback = AsyncMock() bot.register_fallback(fallback_callback) message = AsyncMock() message.content = "<@12345> Test arg0 arg1" self._await(bot.on_message(message)) simple_callback.assert_not_awaited() watcher_callback.assert_awaited_once_with(bot.client, message) fallback_callback.assert_awaited_once_with(bot.client, message, "Test", "arg0", "arg1")
def test_mention_direct(self): bot = Bot("app_name", "version") bot.enforce_write_permission = False bot.client.user.id = "12345" simple_callback = AsyncMock() bot.register_command("test", simple_callback, "short", "long") watcher_callback = AsyncMock() bot.register_watcher(watcher_callback) fallback_callback = AsyncMock() bot.register_fallback(fallback_callback) message = AsyncMock() message.content = "<@12345> test arg0 arg1" message.channel.type == discord.ChannelType.private self._await(bot.on_message(message)) simple_callback.assert_awaited_once_with(bot.client, message, "test", "arg0", "arg1") watcher_callback.assert_awaited_once_with(bot.client, message) fallback_callback.assert_not_awaited()
def test_mention_no_permission(self): bot = Bot("app_name", "version") bot.client.user.id = "12345" simple_callback = AsyncMock() bot.register_command("test", simple_callback, "short", "long") watcher_callback = AsyncMock() bot.register_watcher(watcher_callback) fallback_callback = AsyncMock() bot.register_fallback(fallback_callback) message = AsyncMock() message.content = "<@12345> test hey" message.channel.__repr__ = lambda *a: "test_channel" message.guild.__repr__ = lambda *a: "test_guild" permissions = AsyncMock() permissions.send_messages = False message.channel.permissions_for = lambda u: permissions self._await(bot.on_message(message)) simple_callback.assert_not_awaited() watcher_callback.assert_awaited_once_with(bot.client, message) fallback_callback.assert_not_awaited() message.author.create_dm.assert_awaited_once() message.author.dm_channel.send.assert_awaited_once_with( f"Hi, this bot doesn't have the permission to send a message to" f" #test_channel in server 'test_guild'")
def test_add_simple(self): bot = Bot("app_name", "version") bot.register_command("test", None, None, None) cmd = bot._Bot__commands[0] self.assertEqual("^test$", cmd.regex)