async def test_disable(): mock_command = MockCommand("disable", "disable", context=MockMessage(channel=MockChannel(guild=MockGuild(_id=3)))) set_permission_filter(guild_id=3, command_wrapper=get_wrapper("disable"), permission_filter="user:<@0>") assert await receive_command(mock_command) assert "✓" in mock_command.response expected_embed = permissions_embed(guild_id=3, command_wrappers=[get_wrapper("disable")]) assert mock_command.response_embed.fields[0].name == expected_embed.fields[0].name assert mock_command.response_embed.fields[0].value == expected_embed.fields[0].value assert not get_permission_filter(guild_id=3, command_wrapper=get_wrapper("disable"))
def test_get_permission_filter(): command_wrapper = FunctionWrapper(category=None, names=["test1", "test2", "test3"], execute=None) set_permission_filter(guild_id=3, command_wrapper=command_wrapper, permission_filter="filter") permission_filter = get_permission_filter(guild_id=3, command_wrapper=command_wrapper) assert permission_filter == "filter"
def test_set_permission_filter(): command_wrapper = FunctionWrapper(category=None, names=["test1", "test2", "test3"], execute=None) set_permission_filter(guild_id=3, command_wrapper=command_wrapper, permission_filter="filter") assert "test1" in permissions.cache[3] assert "test2" not in permissions.cache[3] assert permissions.cache[3]["test1"] == "filter"
def test_can_execute_user_perm_fail(): command_wrapper = get_wrapper(name="test1") mock_message = MockMessage(channel=MockChannel(_id=44, guild=MockGuild(_id=3)), author=MockUser(_id=2, is_admin=False)) command = Command(name="test2", context=mock_message) set_permission_filter(guild_id=3, command_wrapper=command_wrapper, permission_filter="user:<@88>") assert not permissions.can_execute(command)
def test_set_permission_filter_none(): command_wrapper = FunctionWrapper(category=None, names=["test1", "test2", "test3"], execute=None) set_permission_filter(guild_id=3, command_wrapper=command_wrapper, permission_filter="filter") set_permission_filter(guild_id=3, command_wrapper=command_wrapper, permission_filter=None) # The guild no longer has any permission filters, so it should be discarded together with the filter. assert 3 not in permissions.cache
def test_can_execute_role_perm(): command_wrapper = get_wrapper(name="test1") mock_message = MockMessage(channel=MockChannel(_id=44, guild=MockGuild(_id=3)), author=MockUser(_id=2, roles=[MockRole(_id=66)], is_admin=False)) command = Command(name="test2", context=mock_message) set_permission_filter(guild_id=3, command_wrapper=command_wrapper, permission_filter="role:<@&66>") assert permissions.can_execute(command)
async def cmd_disable(command: Command, commands_str: str): if not command.guild_id(): await command.respond_err("Cannot change permissions in DM channels.") return command_wrappers = get_command_wrappers(commands_str) for command_wrapper in command_wrappers: permissions.set_permission_filter(guild_id=command.guild_id(), command_wrapper=command_wrapper, permission_filter=None) await command.respond(response="✓ Command permissions changed.", embed=permissions_embed( guild_id=command.guild_id(), command_wrappers=command_wrappers))
async def cmd_enable(command: Command, commands_str: str, perms_filter: Optional[str]=None): if not command.guild_id(): await command.respond_err("Cannot change permissions in DM channels.") return if perms_filter and not await validate_filter(command, perms_filter, filter_context): # `validate_filter` responds for us. return command_wrappers = get_command_wrappers(commands_str) for command_wrapper in command_wrappers: permissions.set_permission_filter( guild_id = command.guild_id(), command_wrapper = command_wrapper, permission_filter = perms_filter or f"role:<@&{command.context.guild.default_role.id}>" ) await command.respond( response = "✓ Command permissions changed.", embed = permissions_embed( guild_id = command.guild_id(), command_wrappers = command_wrappers ) )
def test_set_permission_filter_none_keep_guild(): command_wrapper = FunctionWrapper(category=None, names=["test1", "test2", "test3"], execute=None) command_wrapper2 = FunctionWrapper(category=None, names=["test4", "test5", "test6"], execute=None) set_permission_filter(guild_id=3, command_wrapper=command_wrapper, permission_filter="filter") set_permission_filter(guild_id=3, command_wrapper=command_wrapper2, permission_filter="filter2") set_permission_filter(guild_id=3, command_wrapper=command_wrapper, permission_filter=None) assert "test4" in permissions.cache[3] assert "test1" not in permissions.cache[3]